Debian Codegraph @

cli/tests/test_alias.rs

raw
1 // Copyright 2022 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use crate::common::TestEnvironment;
16
17 #[test]
18 fn test_alias_basic() {
19 let test_env = TestEnvironment::default();
20 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
21 let work_dir = test_env.work_dir("repo");
22
23 test_env.add_config(r#"aliases.bk = ["log", "-r", "@", "-T", "bookmarks"]"#);
24 work_dir
25 .run_jj(["bookmark", "create", "my-bookmark", "-r", "@"])
26 .success();
27 let output = work_dir.run_jj(["bk"]);
28 insta::assert_snapshot!(output, @"
29 @ my-bookmark
30 │
31 ~
32 [EOF]
33 ");
34 }
35
36 #[test]
37 fn test_alias_bad_name() {
38 let test_env = TestEnvironment::default();
39 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
40 let work_dir = test_env.work_dir("repo");
41
42 let output = work_dir.run_jj(["foo."]);
43 insta::assert_snapshot!(output, @"
44 ------- stderr -------
45 error: unrecognized subcommand 'foo.'
46
47 Usage: jj [OPTIONS] <COMMAND>
48
49 For more information, try '--help'.
50 [EOF]
51 [exit status: 2]
52 ");
53 }
54
55 #[test]
56 fn test_alias_calls_empty_command() {
57 let test_env = TestEnvironment::default();
58 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
59 let work_dir = test_env.work_dir("repo");
60
61 test_env.add_config(
62 r#"
63 ui.default-command = []
64 aliases.empty = []
65 aliases.empty_command_with_opts = ["--no-pager"]
66 "#,
67 );
68
69 let output = work_dir.run_jj(["empty"]);
70 insta::assert_snapshot!(
71 output.normalize_stderr_with(|s| s.split_inclusive('\n').take(3).collect()), @"
72 ------- stderr -------
73 Jujutsu (An experimental VCS)
74
75 Usage: jj [OPTIONS] <COMMAND>
76 [EOF]
77 [exit status: 2]
78 ");
79 let output = work_dir.run_jj(["empty", "--no-pager"]);
80 insta::assert_snapshot!(
81 output.normalize_stderr_with(|s| s.split_inclusive('\n').take(1).collect()), @"
82 ------- stderr -------
83 error: 'jj' requires a subcommand but one was not provided
84 [EOF]
85 [exit status: 2]
86 ");
87 let output = work_dir.run_jj(["empty_command_with_opts"]);
88 insta::assert_snapshot!(
89 output.normalize_stderr_with(|s| s.split_inclusive('\n').take(1).collect()), @"
90 ------- stderr -------
91 error: 'jj' requires a subcommand but one was not provided
92 [EOF]
93 [exit status: 2]
94 ");
95 }
96
97 #[test]
98 fn test_alias_calls_unknown_command() {
99 let test_env = TestEnvironment::default();
100 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
101 let work_dir = test_env.work_dir("repo");
102
103 test_env.add_config(r#"aliases.foo = ["nonexistent"]"#);
104 let output = work_dir.run_jj(["foo"]);
105 insta::assert_snapshot!(output, @"
106 ------- stderr -------
107 error: unrecognized subcommand 'nonexistent'
108
109 tip: a similar subcommand exists: 'next'
110
111 Usage: jj [OPTIONS] <COMMAND>
112
113 For more information, try '--help'.
114 [EOF]
115 [exit status: 2]
116 ");
117 }
118
119 #[test]
120 fn test_alias_calls_command_with_invalid_option() {
121 let test_env = TestEnvironment::default();
122 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
123 let work_dir = test_env.work_dir("repo");
124
125 test_env.add_config(r#"aliases.foo = ["log", "--nonexistent"]"#);
126 let output = work_dir.run_jj(["foo"]);
127 insta::assert_snapshot!(output, @"
128 ------- stderr -------
129 error: unexpected argument '--nonexistent' found
130
131 tip: to pass '--nonexistent' as a value, use '-- --nonexistent'
132
133 Usage: jj log [OPTIONS] [FILESETS]...
134
135 For more information, try '--help'.
136 [EOF]
137 [exit status: 2]
138 ");
139 }
140
141 #[test]
142 fn test_alias_calls_help() {
143 let test_env = TestEnvironment::default();
144 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
145 let work_dir = test_env.work_dir("repo");
146 test_env.add_config(r#"aliases.h = ["--help"]"#);
147 let output = work_dir.run_jj(&["h"]);
148 insta::assert_snapshot!(
149 output.normalize_stdout_with(|s| s.split_inclusive('\n').take(7).collect()), @"
150 Jujutsu (An experimental VCS)
151
152 To get started, see the tutorial [`jj help -k tutorial`].
153
154 [`jj help -k tutorial`]: https://docs.jj-vcs.dev/latest/tutorial/
155
156 Usage: jj [OPTIONS] <COMMAND>
157 [EOF]
158 ");
159 }
160
161 #[test]
162 fn test_alias_cannot_override_builtin() {
163 let test_env = TestEnvironment::default();
164 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
165 let work_dir = test_env.work_dir("repo");
166
167 test_env.add_config(r#"aliases.log = ["rebase"]"#);
168 // Alias should give a warning
169 let output = work_dir.run_jj(["log", "-r", "root()"]);
170 insta::assert_snapshot!(output, @r"
171 ◆ zzzzzzzz root() 00000000
172 [EOF]
173 ------- stderr -------
174 Warning: Cannot define an alias that overrides the built-in command 'log'.
175 [EOF]
176 ");
177 }
178
179 #[test]
180 fn test_alias_recursive() {
181 let test_env = TestEnvironment::default();
182 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
183 let work_dir = test_env.work_dir("repo");
184
185 test_env.add_config(
186 r#"[aliases]
187 foo = ["foo"]
188 bar = ["baz"]
189 baz = ["bar"]
190 "#,
191 );
192 // Alias should not cause infinite recursion or hang
193 let output = work_dir.run_jj(["foo"]);
194 insta::assert_snapshot!(output, @"
195 ------- stderr -------
196 Error: Recursive alias definition involving `foo`
197 [EOF]
198 [exit status: 1]
199 ");
200 // Also test with mutual recursion
201 let output = work_dir.run_jj(["bar"]);
202 insta::assert_snapshot!(output, @"
203 ------- stderr -------
204 Error: Recursive alias definition involving `bar`
205 [EOF]
206 [exit status: 1]
207 ");
208 }
209
210 #[test]
211 fn test_alias_global_args_before_and_after() {
212 let test_env = TestEnvironment::default();
213 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
214 let work_dir = test_env.work_dir("repo");
215 test_env.add_config(r#"aliases.l = ["log", "-T", "commit_id", "-r", "all()"]"#);
216 // Test the setup
217 let output = work_dir.run_jj(["l"]);
218 insta::assert_snapshot!(output, @"
219 @ e8849ae12c709f2321908879bc724fdb2ab8a781
220 ◆ 0000000000000000000000000000000000000000
221 [EOF]
222 ");
223
224 // Can pass global args before
225 let output = work_dir.run_jj(["l", "--at-op", "@-"]);
226 insta::assert_snapshot!(output, @"
227 ◆ 0000000000000000000000000000000000000000
228 [EOF]
229 ");
230 // Can pass global args after
231 let output = work_dir.run_jj(["--at-op", "@-", "l"]);
232 insta::assert_snapshot!(output, @"
233 ◆ 0000000000000000000000000000000000000000
234 [EOF]
235 ");
236 // Test passing global args both before and after
237 let output = work_dir.run_jj(["--at-op", "abc123", "l", "--at-op", "@-"]);
238 insta::assert_snapshot!(output, @"
239 ◆ 0000000000000000000000000000000000000000
240 [EOF]
241 ");
242 let output = work_dir.run_jj(["-R", "../nonexistent", "l", "-R", "."]);
243 insta::assert_snapshot!(output, @"
244 @ e8849ae12c709f2321908879bc724fdb2ab8a781
245 ◆ 0000000000000000000000000000000000000000
246 [EOF]
247 ");
248 }
249
250 #[test]
251 fn test_alias_global_args_in_definition() {
252 let test_env = TestEnvironment::default();
253 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
254 let work_dir = test_env.work_dir("repo");
255 test_env.add_config(
256 r#"aliases.l = ["log", "-T", "commit_id", "--at-op", "@-", "-r", "all()", "--color=always"]"#,
257 );
258
259 // The global argument in the alias is respected
260 let output = work_dir.run_jj(["l"]);
261 insta::assert_snapshot!(output, @"
262 ◆ 0000000000000000000000000000000000000000
263 [EOF]
264 ");
265 }
266
267 #[test]
268 fn test_alias_invalid_definition() {
269 let test_env = TestEnvironment::default();
270
271 test_env.add_config(
272 r#"[aliases]
273 non-list = 5
274 non-string-list = [0]
275 "#,
276 );
277 let output = test_env.run_jj_in(".", ["non-list"]);
278 insta::assert_snapshot!(output.normalize_backslash(), @"
279 ------- stderr -------
280 Config error: Invalid type or value for aliases.non-list
281 Caused by: invalid type: integer `5`, expected a sequence
282
283 Hint: Check the config file: $TEST_ENV/config/config0002.toml
284 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
285 [EOF]
286 [exit status: 1]
287 ");
288 let output = test_env.run_jj_in(".", ["non-string-list"]);
289 insta::assert_snapshot!(output, @"
290 ------- stderr -------
291 Config error: Invalid type or value for aliases.non-string-list
292 Caused by: invalid type: integer `0`, expected a string
293
294 Hint: Check the config file: $TEST_ENV/config/config0002.toml
295 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
296 [EOF]
297 [exit status: 1]
298 ");
299 }
300
301 #[test]
302 fn test_alias_in_repo_config() {
303 let test_env = TestEnvironment::default();
304 test_env.run_jj_in(".", ["git", "init", "repo1"]).success();
305 let work_dir1 = test_env.work_dir("repo1");
306 work_dir1.create_dir("sub");
307 test_env.run_jj_in(".", ["git", "init", "repo2"]).success();
308 let work_dir2 = test_env.work_dir("repo2");
309 work_dir2.create_dir("sub");
310
311 test_env.add_config(r#"aliases.l = ['log', '-r@', '--no-graph', '-T"user alias\n"']"#);
312 work_dir1
313 .run_jj([
314 "config",
315 "set",
316 "--repo",
317 "aliases.l",
318 r#"['log', '-r@', '--no-graph', '-T"repo1 alias\n"']"#,
319 ])
320 .success();
321
322 // In repo1 sub directory, aliases can be loaded from the repo1 config.
323 let output = test_env.run_jj_in(work_dir1.root().join("sub"), ["l"]);
324 insta::assert_snapshot!(output, @"
325 repo1 alias
326 [EOF]
327 ");
328
329 // In repo2 directory, no repo-local aliases exist.
330 let output = work_dir2.run_jj(["l"]);
331 insta::assert_snapshot!(output, @"
332 user alias
333 [EOF]
334 ");
335
336 // Aliases can't be loaded from the -R path due to chicken and egg problem.
337 let output = work_dir2.run_jj(["l", "-R", work_dir1.root().to_str().unwrap()]);
338 insta::assert_snapshot!(output, @"
339 user alias
340 [EOF]
341 ------- stderr -------
342 Warning: Command aliases cannot be loaded from -R/--repository path or --config/--config-file arguments.
343 [EOF]
344 ");
345
346 // Aliases are loaded from the cwd-relative workspace even with -R.
347 let output = work_dir1.run_jj(["l", "-R", work_dir2.root().to_str().unwrap()]);
348 insta::assert_snapshot!(output, @"
349 repo1 alias
350 [EOF]
351 ------- stderr -------
352 Warning: Command aliases cannot be loaded from -R/--repository path or --config/--config-file arguments.
353 [EOF]
354 ");
355
356 // No warning if the expanded command is identical.
357 let output = work_dir1.run_jj(["file", "list", "-R", work_dir2.root().to_str().unwrap()]);
358 insta::assert_snapshot!(output, @"");
359
360 // Config loaded from the cwd-relative workspace shouldn't persist. It's
361 // used only for command arguments expansion.
362 let output = work_dir1.run_jj([
363 "config",
364 "list",
365 "aliases",
366 "-R",
367 work_dir2.root().to_str().unwrap(),
368 ]);
369 insta::assert_snapshot!(output, @r#"
370 aliases.l = ['log', '-r@', '--no-graph', '-T"user alias\n"']
371 [EOF]
372 "#);
373 }
374
375 #[test]
376 fn test_alias_in_config_arg() {
377 let test_env = TestEnvironment::default();
378 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
379 let work_dir = test_env.work_dir("repo");
380 test_env.add_config(r#"aliases.l = ['log', '-r@', '--no-graph', '-T"user alias\n"']"#);
381
382 let output = work_dir.run_jj(["l"]);
383 insta::assert_snapshot!(output, @"
384 user alias
385 [EOF]
386 ");
387
388 let alias_arg = r#"--config=aliases.l=['log', '-r@', '--no-graph', '-T"arg alias\n"']"#;
389 let output = work_dir.run_jj([alias_arg, "l"]);
390 insta::assert_snapshot!(output, @"
391 user alias
392 [EOF]
393 ------- stderr -------
394 Warning: Command aliases cannot be loaded from -R/--repository path or --config/--config-file arguments.
395 [EOF]
396 ");
397 // should print warning about aliases even if cli parsing fails
398 let alias_arg = r#"--config=aliases.this-command-not-exist=['log', '-r@', '--no-graph', '-T"arg alias\n"']"#;
399 let output = work_dir.run_jj([alias_arg, "this-command-not-exist"]);
400 insta::assert_snapshot!(output, @"
401 ------- stderr -------
402 Warning: Command aliases cannot be loaded from -R/--repository path or --config/--config-file arguments.
403 error: unrecognized subcommand 'this-command-not-exist'
404
405 Usage: jj [OPTIONS] <COMMAND>
406
407 For more information, try '--help'.
408 [EOF]
409 [exit status: 2]
410 ");
411 }
412
413 #[test]
414 fn test_aliases_overriding_friendly_errors() {
415 let test_env = TestEnvironment::default();
416 // Test with color
417 let output = test_env.run_jj_in(".", ["--color=always", "init", "repo"]);
418 insta::assert_snapshot!(output, @r#"
419 ------- stderr -------
420 error: unrecognized subcommand 'init'
421
422 For more information, try '--help'.
423 Hint: You probably want `jj git init`. See also `jj help git`.
424 Hint: You can configure `aliases.init = ["git", "init"]` if you want `jj init` to work and always use the Git backend.
425 [EOF]
426 [exit status: 2]
427 "#);
428
429 let output = test_env.run_jj_in(".", ["init", "repo"]);
430 insta::assert_snapshot!(output, @r#"
431 ------- stderr -------
432 error: unrecognized subcommand 'init'
433
434 For more information, try '--help'.
435 Hint: You probably want `jj git init`. See also `jj help git`.
436 Hint: You can configure `aliases.init = ["git", "init"]` if you want `jj init` to work and always use the Git backend.
437 [EOF]
438 [exit status: 2]
439 "#);
440 let output = test_env.run_jj_in(".", ["clone", "https://example.org/repo"]);
441 insta::assert_snapshot!(output, @r#"
442 ------- stderr -------
443 error: unrecognized subcommand 'clone'
444
445 For more information, try '--help'.
446 Hint: You probably want `jj git clone`. See also `jj help git`.
447 Hint: You can configure `aliases.clone = ["git", "clone"]` if you want `jj clone` to work and always use the Git backend.
448 [EOF]
449 [exit status: 2]
450 "#);
451 let output = test_env.run_jj_in(".", ["init", "--help"]);
452 insta::assert_snapshot!(output, @r#"
453 ------- stderr -------
454 error: unrecognized subcommand 'init'
455
456 For more information, try '--help'.
457 Hint: You probably want `jj git init`. See also `jj help git`.
458 Hint: You can configure `aliases.init = ["git", "init"]` if you want `jj init` to work and always use the Git backend.
459 [EOF]
460 [exit status: 2]
461 "#);
462
463 // Test that `init` can be overridden as an alias. (We use `jj config get`
464 // as a command with a predictable output)
465 test_env.add_config(r#"aliases.init=["config", "get", "user.name"]"#);
466 let output = test_env.run_jj_in(".", ["init"]);
467 insta::assert_snapshot!(output, @"
468 Test User
469 [EOF]
470 ");
471 }
472
473 #[test]
474 fn test_default_command_is_alias() {
475 let test_env = TestEnvironment::default();
476 test_env.add_config(
477 r#"
478 ui.default-command = ["my-version"]
479 aliases.my-version = ["version"]
480 "#,
481 );
482 test_env.run_jj_in(".", [""; 0]).success();
483 }
484
485 #[test]
486 fn test_alias_recursion_check() {
487 let test_env = TestEnvironment::default();
488 test_env.add_config(
489 r#"
490 [aliases]
491 a = ["--config=user.name=x"]
492 b = ["a", "a"]
493 c = ["a", "d"]
494 d = ["a", "c"]
495 "#,
496 );
497 let work_dir = test_env.work_dir(".");
498
499 work_dir
500 .run_jj(["a", "b", "a", "b", "a", "version"])
501 .success();
502
503 let output = work_dir.run_jj(["c"]);
504 insta::assert_snapshot!(output, @"
505 ------- stderr -------
506 Error: Recursive alias definition involving `c`
507 [EOF]
508 [exit status: 1]
509 ");
510 }
511
512 #[test]
513 fn test_alias_falls_back_to_default_command() {
514 let test_env = TestEnvironment::default();
515 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
516 let work_dir = test_env.work_dir("repo");
517 test_env.add_config(
518 r#"
519 ui.default-command = ["log", "-Tbuiltin_log_oneline"]
520 aliases.jj = []
521 aliases.log-root = ["-r=root()"]
522 "#,
523 );
524 let output = work_dir.run_jj(["jj"]);
525 insta::assert_snapshot!(output, @"
526 @ qpvuntsm test.user 2001-02-03 08:05:07 e8849ae1 (empty) (no description set)
527 ◆ zzzzzzzz root() 00000000
528 [EOF]
529 ");
530 let output = work_dir.run_jj(["log-root"]);
531 insta::assert_snapshot!(output, @"
532 ◆ zzzzzzzz root() 00000000
533 [EOF]
534 ");
535 }