Debian Codegraph @

cli/tests/test_completion.rs

raw
1 // Copyright 2024 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 std::ffi::OsStr;
16
17 use clap_complete::Shell;
18 use indoc::indoc;
19 use itertools::Itertools as _;
20 use test_case::test_case;
21
22 use crate::common::CommandOutput;
23 use crate::common::TestEnvironment;
24 use crate::common::TestWorkDir;
25
26 impl TestEnvironment {
27 /// Runs `jj` as if shell completion had been triggered for the argument of
28 /// the given index.
29 #[must_use = "either snapshot the output or assert the exit status with .success()"]
30 fn complete_at<I>(&self, shell: Shell, index: usize, args: I) -> CommandOutput
31 where
32 I: IntoIterator,
33 I::IntoIter: ExactSizeIterator,
34 I::Item: AsRef<OsStr>,
35 {
36 self.work_dir("").complete_at(shell, index, args)
37 }
38
39 /// Run `jj` as if fish shell completion had been triggered at the last item
40 /// in `args`.
41 #[must_use = "either snapshot the output or assert the exit status with .success()"]
42 fn complete_fish<I>(&self, args: I) -> CommandOutput
43 where
44 I: IntoIterator,
45 I::IntoIter: ExactSizeIterator,
46 I::Item: AsRef<OsStr>,
47 {
48 self.work_dir("").complete_fish(args)
49 }
50 }
51
52 impl TestWorkDir<'_> {
53 /// Runs `jj` as if shell completion had been triggered for the argument of
54 /// the given index.
55 #[must_use = "either snapshot the output or assert the exit status with .success()"]
56 fn complete_at<I>(&self, shell: Shell, index: usize, args: I) -> CommandOutput
57 where
58 I: IntoIterator,
59 I::IntoIter: ExactSizeIterator,
60 I::Item: AsRef<OsStr>,
61 {
62 let args = args.into_iter();
63 assert!(
64 index <= args.len(),
65 "index out of bounds: append empty string to complete after the last argument"
66 );
67 self.run_jj_with(|cmd| {
68 let cmd = cmd.env("COMPLETE", shell.to_string()).args(["--", "jj"]);
69
70 match shell {
71 // Bash passes the whole command line except for intermediate empty tokens but
72 // preserves trailing empty tokens:
73 // `jj log <CURSOR>` => ["--", "jj", "log", ""]
74 // with _CLAP_COMPLETE_INDEX=2
75 // `jj log <CURSOR> --no-graph` => ["--", "jj", "log", "--no-graph"]
76 // with _CLAP_COMPLETE_INDEX=2
77 // `jj log --no-graph<CURSOR>` => ["--", "jj", "log", "--no-graph"]
78 // with _CLAP_COMPLETE_INDEX=2
79 // (indistinguishable from the above)
80 // `jj log --no-graph <CURSOR>` => ["--", "jj", "log", "--no-graph", ""]
81 // with _CLAP_COMPLETE_INDEX=3
82 Shell::Bash => {
83 cmd.env("_CLAP_COMPLETE_INDEX", index.to_string())
84 .args(args.coalesce(|a, b| {
85 if a.as_ref().is_empty() {
86 Ok(b)
87 } else {
88 Err((a, b))
89 }
90 }))
91 }
92
93 // Zsh passes the whole command line except for empty tokens, including trailing
94 // empty tokens:
95 // `jj log <CURSOR>` => ["--", "jj", "log"]
96 // with _CLAP_COMPLETE_INDEX=2
97 // `jj log <CURSOR> --no-graph` => ["--", "jj", "log", "--no-graph"]
98 // with _CLAP_COMPLETE_INDEX=2
99 // `jj log --no-graph<CURSOR>` => ["--", "jj", "log", "--no-graph"]
100 // with _CLAP_COMPLETE_INDEX=2
101 // (indistinguishable from the above)
102 // `jj log --no-graph <CURSOR>` => ["--", "jj", "log", "--no-graph"]
103 // with _CLAP_COMPLETE_INDEX=3
104 Shell::Zsh => cmd
105 .env("_CLAP_COMPLETE_INDEX", index.to_string())
106 .args(args.filter(|a| !a.as_ref().is_empty())),
107
108 // Fish truncates the command line at the cursor; empty tokens are preserved:
109 // `jj log <CURSOR>` => ["--", "jj", "log", ""]
110 // `jj log <CURSOR> --no-graph` => ["--", "jj", "log", ""]
111 // `jj log --no-graph<CURSOR>` => ["--", "jj", "log", "--no-graph"]
112 // `jj log --no-graph <CURSOR>` => ["--", "jj", "log", "--no-graph", ""]
113 Shell::Fish => cmd.args(args.take(index)),
114
115 _ => todo!("{shell} completion behavior not implemented yet"),
116 }
117 })
118 }
119
120 /// Run `jj` as if fish shell completion had been triggered at the last item
121 /// in `args`.
122 #[must_use = "either snapshot the output or assert the exit status with .success()"]
123 fn complete_fish<I>(&self, args: I) -> CommandOutput
124 where
125 I: IntoIterator,
126 I::IntoIter: ExactSizeIterator,
127 I::Item: AsRef<OsStr>,
128 {
129 let args = args.into_iter();
130 self.complete_at(Shell::Fish, args.len(), args)
131 }
132 }
133
134 #[test]
135 fn test_bookmark_names() {
136 let test_env = TestEnvironment::default();
137 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
138 let work_dir = test_env.work_dir("repo");
139
140 test_env
141 .run_jj_in(".", ["git", "init", "--colocate", "origin"])
142 .success();
143 let origin_dir = test_env.work_dir("origin");
144 test_env
145 .run_jj_in(".", ["git", "init", "--colocate", "upstream"])
146 .success();
147 let _upstream_dir = test_env.work_dir("upstream");
148
149 work_dir
150 .run_jj(["bookmark", "create", "-r@", "aaa-local"])
151 .success();
152 work_dir
153 .run_jj(["bookmark", "create", "-r@", "bbb-local"])
154 .success();
155
156 // add various remote branches
157 work_dir
158 .run_jj(["git", "remote", "add", "origin", "../origin"])
159 .success();
160 work_dir
161 .run_jj(["git", "remote", "add", "upstream", "../upstream"])
162 .success();
163 work_dir
164 .run_jj(["bookmark", "create", "-r@", "aaa-tracked"])
165 .success();
166 work_dir
167 .run_jj(["desc", "-r", "aaa-tracked", "-m", "x"])
168 .success();
169 work_dir
170 .run_jj(["bookmark", "create", "-r@", "bbb-tracked"])
171 .success();
172 work_dir
173 .run_jj(["desc", "-r", "bbb-tracked", "-m", "x"])
174 .success();
175
176 work_dir
177 .run_jj(["bookmark", "track", "--remote=origin", "*-tracked"])
178 .success();
179 work_dir
180 .run_jj(["bookmark", "track", "--remote=upstream", "aaa-tracked"])
181 .success();
182 work_dir
183 .run_jj(["git", "push", "--remote=origin", "--tracked"])
184 .success();
185 work_dir
186 .run_jj(["git", "push", "--remote=upstream", "--tracked"])
187 .success();
188
189 origin_dir
190 .run_jj(["bookmark", "create", "-r@", "aaa-untracked"])
191 .success();
192 origin_dir
193 .run_jj(["desc", "-r", "aaa-untracked", "-m", "x"])
194 .success();
195 origin_dir
196 .run_jj(["bookmark", "create", "-r@", "bbb-untracked"])
197 .success();
198 origin_dir
199 .run_jj(["desc", "-r", "bbb-untracked", "-m", "x"])
200 .success();
201 work_dir.run_jj(["git", "fetch", "--all-remotes"]).success();
202
203 insta::assert_snapshot!(work_dir.run_jj(["bookmark", "list", "--all"]), @"
204 aaa-local: qpvuntsm fe38a82d (empty) x
205 aaa-tracked: qpvuntsm fe38a82d (empty) x
206 @origin: qpvuntsm fe38a82d (empty) x
207 @upstream: qpvuntsm fe38a82d (empty) x
208 aaa-untracked@origin: rlvkpnrz 434ae005 (empty) x
209 bbb-local: qpvuntsm fe38a82d (empty) x
210 bbb-tracked: qpvuntsm fe38a82d (empty) x
211 @origin: qpvuntsm fe38a82d (empty) x
212 bbb-untracked@origin: rlvkpnrz 434ae005 (empty) x
213 [EOF]
214 ");
215
216 // Every shell hook is a little different, e.g. the zsh hooks add some
217 // additional environment variables. But this is irrelevant for the purpose
218 // of testing our own logic, so it's fine to test a single shell only.
219 let work_dir = test_env.work_dir("repo");
220
221 let output = work_dir.complete_fish(["bookmark", "rename", ""]);
222 insta::assert_snapshot!(output, @"
223 aaa-local x
224 aaa-tracked x
225 bbb-local x
226 bbb-tracked x
227 --overwrite-existing Allow renaming even if the new bookmark name already exists
228 --help Print help (see more with '--help')
229 --repository Path to repository to operate on
230 --ignore-working-copy Don't snapshot the working copy, and don't update it
231 --no-integrate-operation Run the command as usual but don't integrate any operations
232 --ignore-immutable Allow rewriting immutable commits
233 --at-operation Operation to load the repo at
234 --debug Enable debug logging
235 --color When to colorize output
236 --quiet Silence non-primary command output
237 --no-pager Disable the pager
238 --config Additional configuration options (can be repeated)
239 --config-file Additional configuration files (can be repeated)
240 [EOF]
241 ");
242
243 let output = work_dir.complete_fish(["bookmark", "rename", "a"]);
244 insta::assert_snapshot!(output, @"
245 aaa-local x
246 aaa-tracked x
247 [EOF]
248 ");
249
250 let output = work_dir.complete_fish(["bookmark", "delete", "a"]);
251 insta::assert_snapshot!(output, @"
252 aaa-local x
253 aaa-tracked x
254 [EOF]
255 ");
256
257 let output = work_dir.complete_fish(["bookmark", "forget", "a"]);
258 insta::assert_snapshot!(output, @"
259 aaa-local x
260 aaa-tracked x
261 aaa-untracked
262 [EOF]
263 ");
264
265 let output = work_dir.complete_fish(["bookmark", "list", "--bookmark", "a"]);
266 insta::assert_snapshot!(output, @"
267 aaa-local x
268 aaa-tracked x
269 aaa-untracked
270 [EOF]
271 ");
272
273 let output = work_dir.complete_fish(["bookmark", "move", "a"]);
274 insta::assert_snapshot!(output, @"
275 aaa-local x
276 aaa-tracked x
277 [EOF]
278 ");
279
280 let output = work_dir.complete_fish(["bookmark", "set", "a"]);
281 insta::assert_snapshot!(output, @"
282 aaa-local x
283 aaa-tracked x
284 [EOF]
285 ");
286
287 let output = work_dir.complete_fish(["bookmark", "track", "a"]);
288 insta::assert_snapshot!(output, @"
289 aaa-local x
290 aaa-untracked x
291 [EOF]
292 ");
293
294 let output = work_dir.complete_fish(["bookmark", "untrack", "a"]);
295 insta::assert_snapshot!(output, @"
296 aaa-tracked x
297 [EOF]
298 ");
299
300 // TODO: Make it so this only lists untracked remotes
301 let output = work_dir.complete_fish(["bookmark", "track", "a", "--remote", ""]);
302 insta::assert_snapshot!(output, @"
303 origin
304 upstream
305 [EOF]
306 ");
307
308 // TODO: Make it so this only lists tracked remotes
309 let output = work_dir.complete_fish(["bookmark", "untrack", "a", "--remote", ""]);
310 insta::assert_snapshot!(output, @"
311 origin
312 upstream
313 [EOF]
314 ");
315
316 let output = work_dir.complete_fish(["git", "push", "-b", "a"]);
317 insta::assert_snapshot!(output, @"
318 aaa-local x
319 aaa-tracked x
320 [EOF]
321 ");
322
323 let output = work_dir.complete_fish(["git", "fetch", "-b", "a"]);
324 insta::assert_snapshot!(output, @"
325 aaa-local x
326 aaa-tracked x
327 aaa-untracked
328 [EOF]
329 ");
330 }
331
332 #[test]
333 fn test_tag_names() {
334 let test_env = TestEnvironment::default();
335 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
336 let work_dir = test_env.work_dir("repo");
337
338 work_dir.run_jj(["commit", "-mcommit1"]).success();
339 work_dir
340 .run_jj(["tag", "set", "-r@-", "aaa-local"])
341 .success();
342 work_dir
343 .run_jj(["tag", "set", "-r@-", "bbb-local"])
344 .success();
345
346 let output = work_dir.complete_fish(["tag", "set", "a"]);
347 insta::assert_snapshot!(output, @"
348 aaa-local commit1
349 [EOF]
350 ");
351
352 let output = work_dir.complete_fish(["tag", "delete", "b"]);
353 insta::assert_snapshot!(output, @"
354 bbb-local commit1
355 [EOF]
356 ");
357 }
358
359 #[test]
360 fn test_global_arg_repository_is_respected() {
361 let test_env = TestEnvironment::default();
362 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
363 let work_dir = test_env.work_dir("repo");
364
365 work_dir
366 .run_jj(["bookmark", "create", "-r@", "aaa"])
367 .success();
368
369 let output = test_env.complete_fish(["--repository", "repo", "bookmark", "rename", "a"]);
370 insta::assert_snapshot!(output, @"
371 aaa (no description set)
372 [EOF]
373 ");
374 }
375
376 #[test_case(Shell::Bash; "bash")]
377 #[test_case(Shell::Zsh; "zsh")]
378 #[test_case(Shell::Fish; "fish")]
379 fn test_aliases_are_resolved(shell: Shell) {
380 let test_env = TestEnvironment::default();
381 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
382 let work_dir = test_env.work_dir("repo");
383
384 work_dir
385 .run_jj(["bookmark", "create", "-r@", "aaa"])
386 .success();
387
388 // user config alias
389 test_env.add_config(r#"aliases.b = ["bookmark"]"#);
390 test_env.add_config(r#"aliases.rlog = ["log", "--reversed"]"#);
391 // repo config alias
392 work_dir
393 .run_jj(["config", "set", "--repo", "aliases.b2", "['bookmark']"])
394 .success();
395
396 let output = work_dir.complete_at(shell, 3, ["b", "rename", "a"]);
397 match shell {
398 Shell::Bash => {
399 insta::assert_snapshot!(output, @"aaa[EOF]");
400 }
401 Shell::Zsh => {
402 insta::assert_snapshot!(output, @"aaa:(no description set)[EOF]");
403 }
404 Shell::Fish => {
405 insta::assert_snapshot!(output, @"
406 aaa (no description set)
407 [EOF]
408 ");
409 }
410 _ => unimplemented!("unexpected shell '{shell}'"),
411 }
412
413 let output = work_dir.complete_at(shell, 3, ["b2", "rename", "a"]);
414 match shell {
415 Shell::Bash => {
416 insta::assert_snapshot!(output, @"aaa[EOF]");
417 }
418 Shell::Zsh => {
419 insta::assert_snapshot!(output, @"aaa:(no description set)[EOF]");
420 }
421 Shell::Fish => {
422 insta::assert_snapshot!(output, @"
423 aaa (no description set)
424 [EOF]
425 ");
426 }
427 _ => unimplemented!("unexpected shell '{shell}'"),
428 }
429
430 let output = work_dir.complete_at(shell, 2, ["rlog", "--rev"]);
431 match shell {
432 Shell::Bash => {
433 insta::assert_snapshot!(output, @"
434 --revision
435 --reversed[EOF]
436 ");
437 }
438 Shell::Zsh => {
439 insta::assert_snapshot!(output, @"
440 --revision:Which revisions to show
441 --reversed:Show revisions in the opposite order (older revisions first)[EOF]
442 ");
443 }
444 Shell::Fish => {
445 insta::assert_snapshot!(output, @"
446 --revision Which revisions to show
447 --reversed Show revisions in the opposite order (older revisions first)
448 [EOF]
449 ");
450 }
451 _ => unimplemented!("unexpected shell '{shell}'"),
452 }
453 }
454
455 #[test]
456 fn test_completions_are_generated() {
457 let mut test_env = TestEnvironment::default();
458 test_env.add_env_var("COMPLETE", "fish");
459 let mut insta_settings = insta::Settings::clone_current();
460 insta_settings.add_filter(r"(--arguments) .*", "$1 .."); // omit path to jj binary
461 let _guard = insta_settings.bind_to_scope();
462
463 let output = test_env.run_jj_in(".", [""; 0]);
464 insta::assert_snapshot!(output, @"
465 complete --keep-order --exclusive --command jj --arguments ..
466 [EOF]
467 ");
468 let output = test_env.run_jj_in(".", ["--"]);
469 insta::assert_snapshot!(output, @"
470 complete --keep-order --exclusive --command jj --arguments ..
471 [EOF]
472 ");
473 }
474
475 #[test]
476 fn test_bad_complete_env() {
477 let mut test_env = TestEnvironment::default();
478
479 test_env.add_env_var("COMPLETE", "badshell");
480 let output = test_env.run_jj_in(".", [""; 0]);
481 insta::assert_snapshot!(output, @"
482 ------- stderr -------
483 error: unknown shell `badshell`, expected one of `bash`, `elvish`, `fish`, `powershell`, `zsh`[EOF]
484 [exit status: 2]
485 ");
486
487 // Empty value of COMPLETE is ignored as is the value of "0". This could
488 // change if `clap` changes the way it interprets an empty COMPLETE env var.
489 //
490 // In other words, jj runs normally instead of returning completions. We get
491 // an error because the default jj command needs to be run in a jj repo.
492 test_env.add_env_var("COMPLETE", "");
493 let output = test_env.run_jj_in(".", [""; 0]);
494 insta::assert_snapshot!(output, @r#"
495 ------- stderr -------
496 Hint: Use `jj -h` for a list of available commands.
497 Run `jj config set --user ui.default-command log` to disable this message.
498 Error: There is no jj repo in "."
499 [EOF]
500 [exit status: 1]
501 "#);
502 // Same thing (normal execution) happens for a sub-command and "0"
503 test_env.add_env_var("COMPLETE", "0");
504 let output = test_env.run_jj_in(".", ["config", "list", "user.name"]);
505 insta::assert_snapshot!(output, @r#"
506 user.name = "Test User"
507 [EOF]
508 "#);
509 }
510
511 #[test_case(Shell::Bash; "bash")]
512 #[test_case(Shell::Zsh; "zsh")]
513 #[test_case(Shell::Fish; "fish")]
514 fn test_default_command_is_resolved(shell: Shell) {
515 let test_env = TestEnvironment::default();
516
517 let output = test_env
518 .complete_at(shell, 1, ["--"])
519 .take_stdout_n_lines(2);
520 match shell {
521 Shell::Bash => {
522 insta::assert_snapshot!(output, @"
523 --revision
524 --limit
525 [EOF]
526 ");
527 }
528 Shell::Zsh => {
529 insta::assert_snapshot!(output, @"
530 --revision:Which revisions to show
531 --limit:Limit number of revisions to show
532 [EOF]
533 ");
534 }
535 Shell::Fish => {
536 insta::assert_snapshot!(output, @"
537 --revision Which revisions to show
538 --limit Limit number of revisions to show
539 [EOF]
540 ");
541 }
542 _ => unimplemented!("unexpected shell '{shell}'"),
543 }
544
545 test_env.add_config("ui.default-command = ['abandon']");
546 let output = test_env
547 .complete_at(shell, 1, ["--"])
548 .take_stdout_n_lines(2);
549 match shell {
550 Shell::Bash => {
551 insta::assert_snapshot!(output, @"
552 --retain-bookmarks
553 --restore-descendants
554 [EOF]
555 ");
556 }
557 Shell::Zsh => {
558 insta::assert_snapshot!(output, @"
559 --retain-bookmarks:Do not delete bookmarks pointing to the revisions to abandon
560 --restore-descendants:Do not modify the content of the children of the abandoned commits
561 [EOF]
562 ");
563 }
564 Shell::Fish => {
565 insta::assert_snapshot!(output, @"
566 --retain-bookmarks Do not delete bookmarks pointing to the revisions to abandon
567 --restore-descendants Do not modify the content of the children of the abandoned commits
568 [EOF]
569 ");
570 }
571 _ => unimplemented!("unexpected shell '{shell}'"),
572 }
573
574 test_env.add_config("ui.default-command = ['bookmark', 'move']");
575 let output = test_env
576 .complete_at(shell, 1, ["--"])
577 .take_stdout_n_lines(2);
578 match shell {
579 Shell::Bash => {
580 insta::assert_snapshot!(output, @"
581 --from
582 --to
583 [EOF]
584 ");
585 }
586 Shell::Zsh => {
587 insta::assert_snapshot!(output, @"
588 --from:Move bookmarks from the given revisions
589 --to:Move bookmarks to this revision
590 [EOF]
591 ");
592 }
593 Shell::Fish => {
594 insta::assert_snapshot!(output, @"
595 --from Move bookmarks from the given revisions
596 --to Move bookmarks to this revision
597 [EOF]
598 ");
599 }
600 _ => unimplemented!("unexpected shell '{shell}'"),
601 }
602 }
603
604 #[test_case(Shell::Bash; "bash")]
605 #[test_case(Shell::Zsh; "zsh")]
606 #[test_case(Shell::Fish; "fish")]
607 fn test_command_completion(shell: Shell) {
608 let test_env = TestEnvironment::default();
609
610 // Command names should be suggested. If the default command were expanded,
611 // only "log" would be listed.
612 let output = test_env.complete_at(shell, 1, [""]).take_stdout_n_lines(2);
613 match shell {
614 Shell::Bash => {
615 insta::assert_snapshot!(output, @"
616 abandon
617 absorb
618 [EOF]
619 ");
620 }
621 Shell::Zsh => {
622 insta::assert_snapshot!(output, @"
623 abandon:Abandon a revision
624 absorb:Move changes from a revision into the stack of mutable revisions
625 [EOF]
626 ");
627 }
628 Shell::Fish => {
629 insta::assert_snapshot!(output, @"
630 abandon Abandon a revision
631 absorb Move changes from a revision into the stack of mutable revisions
632 [EOF]
633 ");
634 }
635 _ => unimplemented!("unexpected shell '{shell}'"),
636 }
637
638 let output = test_env
639 .complete_at(shell, 2, ["--no-pager", ""])
640 .take_stdout_n_lines(2);
641 match shell {
642 Shell::Bash => {
643 insta::assert_snapshot!(output, @"
644 abandon
645 absorb
646 [EOF]
647 ");
648 }
649 Shell::Zsh => {
650 insta::assert_snapshot!(output, @"
651 abandon:Abandon a revision
652 absorb:Move changes from a revision into the stack of mutable revisions
653 [EOF]
654 ");
655 }
656 Shell::Fish => {
657 insta::assert_snapshot!(output, @"
658 abandon Abandon a revision
659 absorb Move changes from a revision into the stack of mutable revisions
660 [EOF]
661 ");
662 }
663 _ => unimplemented!("unexpected shell '{shell}'"),
664 }
665
666 let output = test_env.complete_at(shell, 1, ["b"]);
667 match shell {
668 Shell::Zsh => {
669 insta::assert_snapshot!(output, @"
670 bisect:Find a bad revision by bisection
671 bookmark:Manage bookmarks [default alias: b][EOF]
672 ");
673 }
674 Shell::Bash => {
675 insta::assert_snapshot!(output, @"
676 bisect
677 bookmark[EOF]
678 ");
679 }
680 Shell::Fish => {
681 insta::assert_snapshot!(output, @"
682 bisect Find a bad revision by bisection
683 bookmark Manage bookmarks [default alias: b]
684 [EOF]
685 ");
686 }
687 _ => unimplemented!("unexpected shell '{shell}'"),
688 }
689
690 let output = test_env.complete_at(shell, 1, ["aban", "-r", "@"]);
691 match shell {
692 Shell::Bash => {
693 insta::assert_snapshot!(output, @"abandon[EOF]");
694 }
695 Shell::Zsh => {
696 insta::assert_snapshot!(output, @"abandon:Abandon a revision[EOF]");
697 }
698 Shell::Fish => {
699 insta::assert_snapshot!(output, @"
700 abandon Abandon a revision
701 [EOF]
702 ");
703 }
704 _ => unimplemented!("unexpected shell '{shell}'"),
705 }
706 }
707
708 #[test]
709 fn test_command_completion_short_name() {
710 let test_env = TestEnvironment::default();
711
712 // Short command names should be omitted
713 let output = test_env.complete_fish(["config", ""]);
714 insta::assert_snapshot!(output, @"
715 edit Start an editor on a jj config file
716 gc Find and optionally delete repo-level config directories whose repo path no longer exists
717 get Get the value of a given config option.
718 list List variables set in config files, along with their values
719 path Print the paths to the config files
720 set Update a config file to set the given option to a given value
721 unset Update a config file to unset the given option
722 --repository Path to repository to operate on
723 --ignore-working-copy Don't snapshot the working copy, and don't update it
724 --no-integrate-operation Run the command as usual but don't integrate any operations
725 --ignore-immutable Allow rewriting immutable commits
726 --at-operation Operation to load the repo at
727 --debug Enable debug logging
728 --color When to colorize output
729 --quiet Silence non-primary command output
730 --no-pager Disable the pager
731 --config Additional configuration options (can be repeated)
732 --config-file Additional configuration files (can be repeated)
733 --help Print help (see more with '--help')
734 [EOF]
735 ");
736
737 // Long command name should be suggested
738 let output = test_env.complete_fish(["config", "e"]);
739 insta::assert_snapshot!(output, @"
740 edit Start an editor on a jj config file
741 [EOF]
742 ");
743
744 // Command arguments should be suggested for the short name
745 let output = test_env.complete_fish(["config", "e", "--u"]);
746 insta::assert_snapshot!(output, @"
747 --user Target the user-level config
748 [EOF]
749 ");
750
751 // Command arguments should be suggested for the long name
752 let output = test_env.complete_fish(["config", "edit", "--u"]);
753 insta::assert_snapshot!(output, @"
754 --user Target the user-level config
755 [EOF]
756 ");
757 }
758
759 #[test]
760 fn test_remote_names() {
761 let test_env = TestEnvironment::default();
762 test_env.run_jj_in(".", ["git", "init"]).success();
763
764 test_env
765 .run_jj_in(
766 ".",
767 ["git", "remote", "add", "origin", "[email protected]:user/repo"],
768 )
769 .success();
770
771 let output = test_env.complete_fish(["git", "remote", "remove", "o"]);
772 insta::assert_snapshot!(output, @"
773 origin
774 [EOF]
775 ");
776
777 let output = test_env.complete_fish(["git", "remote", "rename", "o"]);
778 insta::assert_snapshot!(output, @"
779 origin
780 [EOF]
781 ");
782
783 let output = test_env.complete_fish(["git", "remote", "set-url", "o"]);
784 insta::assert_snapshot!(output, @"
785 origin
786 [EOF]
787 ");
788
789 let output = test_env.complete_fish(["git", "push", "--remote", "o"]);
790 insta::assert_snapshot!(output, @"
791 origin
792 [EOF]
793 ");
794
795 let output = test_env.complete_fish(["git", "fetch", "--remote", "o"]);
796 insta::assert_snapshot!(output, @"
797 origin
798 [EOF]
799 ");
800
801 let output = test_env.complete_fish(["bookmark", "list", "--remote", "o"]);
802 insta::assert_snapshot!(output, @"
803 origin
804 [EOF]
805 ");
806 }
807
808 #[test_case(Shell::Bash; "bash")]
809 #[test_case(Shell::Zsh; "zsh")]
810 #[test_case(Shell::Fish; "fish")]
811 fn test_aliases_are_completed(shell: Shell) {
812 let test_env = TestEnvironment::default();
813 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
814 let work_dir = test_env.work_dir("repo");
815 let repo_path = work_dir.root().to_str().unwrap();
816
817 // user config alias
818 test_env.add_config(r#"aliases.user-alias = ["bookmark"]"#);
819 // repo config alias
820 work_dir
821 .run_jj([
822 "config",
823 "set",
824 "--repo",
825 "aliases.repo-alias",
826 "['bookmark']",
827 ])
828 .success();
829
830 // completion of incomplete alias
831 let output = work_dir.complete_at(shell, 1, ["user-al"]);
832 match shell {
833 Shell::Bash => {
834 insta::assert_snapshot!(output, @"user-alias[EOF]");
835 }
836 Shell::Zsh => {
837 insta::assert_snapshot!(output, @"user-alias[EOF]");
838 }
839 Shell::Fish => {
840 insta::assert_snapshot!(output, @"
841 user-alias
842 [EOF]
843 ");
844 }
845 _ => unimplemented!("unexpected shell '{shell}'"),
846 }
847
848 // completion of complete alias confirms it
849 let output = work_dir.complete_at(shell, 1, ["user-alias"]);
850 match shell {
851 Shell::Bash => {
852 insta::assert_snapshot!(output, @"user-alias[EOF]");
853 }
854 Shell::Zsh => {
855 insta::assert_snapshot!(output, @"user-alias[EOF]");
856 }
857 Shell::Fish => {
858 insta::assert_snapshot!(output, @"
859 user-alias
860 [EOF]
861 ");
862 }
863 _ => unimplemented!("unexpected shell '{shell}'"),
864 }
865
866 // completion after alias is based on resolved alias
867 let output = work_dir
868 .complete_at(shell, 2, ["user-alias", ""])
869 .take_stdout_n_lines(2);
870 match shell {
871 Shell::Bash => {
872 insta::assert_snapshot!(output, @r"
873 advance
874 create
875 [EOF]
876 ");
877 }
878 Shell::Zsh => {
879 insta::assert_snapshot!(output, @r"
880 advance:Advance the closest bookmarks to a target revision
881 create:Create a new bookmark
882 [EOF]
883 ");
884 }
885 Shell::Fish => {
886 insta::assert_snapshot!(output, @r"
887 advance Advance the closest bookmarks to a target revision
888 create Create a new bookmark
889 [EOF]
890 ");
891 }
892 _ => unimplemented!("unexpected shell '{shell}'"),
893 }
894
895 // make sure --repository flag is respected
896 let output = test_env.complete_at(shell, 3, ["--repository", repo_path, "repo-al"]);
897 match shell {
898 Shell::Bash => {
899 insta::assert_snapshot!(output, @"repo-alias[EOF]");
900 }
901 Shell::Zsh => {
902 insta::assert_snapshot!(output, @"repo-alias[EOF]");
903 }
904 Shell::Fish => {
905 insta::assert_snapshot!(output, @"
906 repo-alias
907 [EOF]
908 ");
909 }
910 _ => unimplemented!("unexpected shell '{shell}'"),
911 }
912
913 // cannot load aliases from --config flag
914 let output = test_env.complete_at(
915 shell,
916 2,
917 ["--config=aliases.cli-alias=['bookmark']", "cli-al"],
918 );
919 assert!(
920 output.status.success() && output.stdout.is_empty(),
921 "completion expected to come back empty, but got: {output}"
922 );
923 }
924
925 #[test]
926 fn test_alias_descriptions_in_completions() {
927 let test_env = TestEnvironment::default();
928 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
929 let work_dir = test_env.work_dir("repo");
930
931 // alias with a description
932 test_env.add_config(indoc! {r#"
933 [aliases]
934 'compact-log'.doc = 'Show the log in a compact format'
935 'compact-log'.definition = ["log", "--no-graph"]
936 "#});
937
938 // fish displays help text after a tab
939 let output = work_dir.complete_fish(["compact-l"]);
940 insta::assert_snapshot!(output, @"
941 compact-log Show the log in a compact format
942 [EOF]
943 ");
944
945 // alias with multi-line string description
946 test_env.add_config(indoc! {r#"
947 [aliases]
948 'status-short'.doc = 'Show the status of the working copy in short format'
949 'status-short'.definition = ["status", "--format=summary"]
950 "#});
951
952 let output = work_dir.complete_fish(["status-shor"]);
953 insta::assert_snapshot!(output, @"
954 status-short Show the status of the working copy in short format
955 [EOF]
956 ");
957
958 // alias without a .doc property should have no description
959 test_env.add_config(indoc! {r#"
960 [aliases]
961 plain-alias = ["status"]
962 "#});
963
964 let output = work_dir.complete_fish(["plain-alia"]);
965 insta::assert_snapshot!(output, @"
966 plain-alias
967 [EOF]
968 ");
969
970 // revset alias with doc
971 test_env.add_config(indoc! {r#"
972 [revset-aliases]
973 'mine'.doc = 'All my work'
974 'mine'.definition = "author(foo)"
975 "#});
976
977 let output = work_dir.complete_fish(["log", "-r", "min"]);
978 insta::assert_snapshot!(output, @"
979 mine All my work
980 [EOF]
981 ");
982
983 // template alias with doc
984 test_env.add_config(indoc! {r#"
985 [template-aliases]
986 'sh'.doc = 'Short hash'
987 'sh'.definition = "commit_id.short()"
988 "#});
989
990 let output = work_dir.complete_fish(["log", "-T", "s"]);
991 insta::assert_snapshot!(output, @"
992 sh Short hash
993 [EOF]
994 ");
995
996 // fileset alias with doc
997 test_env.add_config(indoc! {r#"
998 [fileset-aliases]
999 'LOCK'.doc = 'Lockfiles'
1000 'LOCK'.definition = '**/Cargo.lock'
1001 "#});
1002
1003 let output = work_dir.complete_fish(["log", "-r", "all()", "L"]);
1004 insta::assert_snapshot!(output, @"");
1005 }
1006
1007 #[test]
1008 fn test_revisions() {
1009 let test_env = TestEnvironment::default();
1010 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1011 let work_dir = test_env.work_dir("repo");
1012
1013 // create remote to test remote branches
1014 test_env.run_jj_in(".", ["git", "init", "origin"]).success();
1015 let origin_dir = test_env.work_dir("origin");
1016 let origin_git_repo_path = origin_dir
1017 .root()
1018 .join(".jj")
1019 .join("repo")
1020 .join("store")
1021 .join("git");
1022 work_dir
1023 .run_jj([
1024 "git",
1025 "remote",
1026 "add",
1027 "origin",
1028 origin_git_repo_path.to_str().unwrap(),
1029 ])
1030 .success();
1031 origin_dir
1032 .run_jj(["bookmark", "create", "-r@", "remote_bookmark"])
1033 .success();
1034 origin_dir
1035 .run_jj(["commit", "-m", "remote_commit"])
1036 .success();
1037 origin_dir
1038 .run_jj(["bookmark", "create", "-r@", "deleted_bookmark"])
1039 .success();
1040 origin_dir
1041 .run_jj(["commit", "-m", "deleted_remote_commit"])
1042 .success();
1043 origin_dir.run_jj(["git", "export"]).success();
1044 work_dir.run_jj(["git", "fetch"]).success();
1045
1046 work_dir
1047 .run_jj(["bookmark", "track", "deleted_bookmark"])
1048 .success();
1049 work_dir
1050 .run_jj(["bookmark", "delete", "deleted_bookmark"])
1051 .success();
1052 work_dir
1053 .run_jj(["bookmark", "create", "-r@", "immutable_bookmark"])
1054 .success();
1055 work_dir.run_jj(["commit", "-m", "immutable"]).success();
1056 test_env.add_config(r#"revset-aliases."immutable_heads()" = "immutable_bookmark""#);
1057 test_env.add_config(r#"revset-aliases."siblings" = "@-+ ~@""#);
1058 test_env.add_config(
1059 r#"revset-aliases."alias_with_newline" = '''
1060 roots(
1061 conflicts()
1062 )
1063 '''"#,
1064 );
1065
1066 work_dir.write_file("file", "A");
1067 work_dir.run_jj(["describe", "-m", "mutable 1"]).success();
1068 work_dir.run_jj(["describe", "-m", "mutable 2"]).success();
1069 // Create divergent change
1070 work_dir
1071 .run_jj([
1072 "bookmark",
1073 "create",
1074 "-r=at_operation(@-, @)",
1075 "mutable_bookmark",
1076 ])
1077 .success();
1078
1079 work_dir.run_jj(["new", "immutable_bookmark"]).success();
1080 work_dir.write_file("file", "B");
1081 work_dir.run_jj(["describe", "-m", "mutable 3"]).success();
1082 work_dir.run_jj(["new", "@", "mutable_bookmark"]).success();
1083 work_dir
1084 .run_jj(["bookmark", "create", "-r@", "conflicted_bookmark"])
1085 .success();
1086 work_dir.run_jj(["describe", "-m", "conflicted"]).success();
1087
1088 work_dir.run_jj(["new", "mutable_bookmark"]).success();
1089 work_dir
1090 .run_jj(["describe", "-m", "working_copy"])
1091 .success();
1092
1093 let work_dir = test_env.work_dir("repo");
1094
1095 // There are _a lot_ of commands and arguments accepting revisions.
1096 // Let's not test all of them. Having at least one test per variation of
1097 // completion function should be sufficient.
1098
1099 // complete all revisions
1100 let output = work_dir.complete_fish(["diff", "--from", ""]);
1101 insta::assert_snapshot!(output, @"
1102 conflicted_bookmark conflicted
1103 deleted_bookmark (deleted bookmark)
1104 immutable_bookmark immutable
1105 mutable_bookmark mutable 1
1106 wv working_copy
1107 x conflicted
1108 u mutable 3
1109 wq/0 mutable 2
1110 wq/1 mutable 1
1111 q immutable
1112 m deleted_remote_commit
1113 r remote_commit
1114 z (no description set)
1115 deleted_bookmark@origin deleted_remote_commit
1116 remote_bookmark@origin remote_commit
1117 alias_with_newline roots(
1118 siblings @-+ ~@
1119 [EOF]
1120 ");
1121
1122 // complete all revisions in a revset expression
1123 let output = work_dir.complete_fish(["log", "-r", ".."]);
1124 insta::assert_snapshot!(output, @"
1125 ..conflicted_bookmark conflicted
1126 ..deleted_bookmark (deleted bookmark)
1127 ..immutable_bookmark immutable
1128 ..mutable_bookmark mutable 1
1129 ..wv working_copy
1130 ..x conflicted
1131 ..u mutable 3
1132 ..wq/0 mutable 2
1133 ..wq/1 mutable 1
1134 ..q immutable
1135 ..m deleted_remote_commit
1136 ..r remote_commit
1137 ..z (no description set)
1138 ..deleted_bookmark@origin deleted_remote_commit
1139 ..remote_bookmark@origin remote_commit
1140 ..alias_with_newline roots(
1141 ..siblings @-+ ~@
1142 [EOF]
1143 ");
1144
1145 // complete only mutable revisions
1146 let output = work_dir.complete_fish(["squash", "--into", ""]);
1147 insta::assert_snapshot!(output, @"
1148 conflicted_bookmark conflicted
1149 mutable_bookmark mutable 1
1150 wv working_copy
1151 x conflicted
1152 u mutable 3
1153 wq/0 mutable 2
1154 wq/1 mutable 1
1155 m deleted_remote_commit
1156 r remote_commit
1157 alias_with_newline roots(
1158 siblings @-+ ~@
1159 [EOF]
1160 ");
1161
1162 // complete only mutable revisions in a revset expression
1163 let output = work_dir.complete_fish(["abandon", "y::"]);
1164 insta::assert_snapshot!(output, @"
1165 y::conflicted_bookmark conflicted
1166 y::mutable_bookmark mutable 1
1167 y::wv working_copy
1168 y::x conflicted
1169 y::u mutable 3
1170 y::wq/0 mutable 2
1171 y::wq/1 mutable 1
1172 y::m deleted_remote_commit
1173 y::r remote_commit
1174 y::alias_with_newline roots(
1175 y::siblings @-+ ~@
1176 [EOF]
1177 ");
1178
1179 // complete remote bookmarks in a revset expression
1180 let output = work_dir.complete_fish(["log", "-r", "remote_bookmark@"]);
1181 insta::assert_snapshot!(output, @"
1182 remote_bookmark@origin remote_commit
1183 [EOF]
1184 ");
1185
1186 // complete conflicted revisions in a revset expression
1187 let output = work_dir.complete_fish(["resolve", "-r", ""]);
1188 insta::assert_snapshot!(output, @"
1189 conflicted_bookmark conflicted
1190 x conflicted
1191 alias_with_newline roots(
1192 siblings @-+ ~@
1193 [EOF]
1194 ");
1195
1196 // complete args of the default command
1197 test_env.add_config("ui.default-command = 'log'");
1198 let output = work_dir.complete_fish(["-r", ""]);
1199 insta::assert_snapshot!(output, @"
1200 conflicted_bookmark conflicted
1201 deleted_bookmark (deleted bookmark)
1202 immutable_bookmark immutable
1203 mutable_bookmark mutable 1
1204 wv working_copy
1205 x conflicted
1206 u mutable 3
1207 wq/0 mutable 2
1208 wq/1 mutable 1
1209 q immutable
1210 m deleted_remote_commit
1211 r remote_commit
1212 z (no description set)
1213 deleted_bookmark@origin deleted_remote_commit
1214 remote_bookmark@origin remote_commit
1215 alias_with_newline roots(
1216 siblings @-+ ~@
1217 [EOF]
1218 ");
1219
1220 // Begin testing `jj git push --named`
1221
1222 // The name of a bookmark does not get completed, since we want to create a new
1223 // bookmark
1224 let output = work_dir.complete_fish(["git", "push", "--named", ""]);
1225 insta::assert_snapshot!(output, @"");
1226 let output = work_dir.complete_fish(["git", "push", "--named", "a"]);
1227 insta::assert_snapshot!(output, @"");
1228
1229 let output = work_dir.complete_fish(["git", "push", "--named", "a="]);
1230 insta::assert_snapshot!(output, @"
1231 a=conflicted_bookmark conflicted
1232 a=deleted_bookmark (deleted bookmark)
1233 a=immutable_bookmark immutable
1234 a=mutable_bookmark mutable 1
1235 a=wv working_copy
1236 a=x conflicted
1237 a=u mutable 3
1238 a=wq/0 mutable 2
1239 a=wq/1 mutable 1
1240 a=q immutable
1241 a=m deleted_remote_commit
1242 a=r remote_commit
1243 a=z (no description set)
1244 a=deleted_bookmark@origin deleted_remote_commit
1245 a=remote_bookmark@origin remote_commit
1246 a=alias_with_newline roots(
1247 a=siblings @-+ ~@
1248 [EOF]
1249 ");
1250
1251 let output = work_dir.complete_fish(["git", "push", "--named", "a=a"]);
1252 insta::assert_snapshot!(output, @"
1253 a=alias_with_newline roots(
1254 [EOF]
1255 ");
1256 }
1257
1258 #[test]
1259 fn test_revisions_workspace_symbols() {
1260 let test_env = TestEnvironment::default();
1261 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1262 let work_dir = test_env.work_dir("repo");
1263
1264 work_dir
1265 .run_jj(["describe", "-m", "main workspace"])
1266 .success();
1267
1268 // workspace working-copy symbols are not completed when there is only one
1269 // workspace
1270 let output = work_dir.complete_fish(["show", "def"]);
1271 insta::assert_snapshot!(output, @"
1272 ");
1273
1274 work_dir
1275 .run_jj(["workspace", "add", "--name", "secondary", "../secondary"])
1276 .success();
1277
1278 // workspace working-copy symbols are completed
1279 let output = work_dir.complete_fish(["show", "def"]);
1280 insta::assert_snapshot!(output, @"
1281 default@ The working copy for workspace `default`
1282 [EOF]
1283 ");
1284
1285 // they are also completed within revset expressions and respect the
1286 // mutable-revisions filter
1287 let output = work_dir.complete_fish(["abandon", "..def"]);
1288 insta::assert_snapshot!(output, @"
1289 ..default@ The working copy for workspace `default`
1290 [EOF]
1291 ");
1292 }
1293
1294 #[test]
1295 fn test_operations() {
1296 let test_env = TestEnvironment::default();
1297
1298 // suppress warnings on stderr of completions for invalid args
1299 test_env.add_config("ui.default-command = 'log'");
1300
1301 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1302 let work_dir = test_env.work_dir("repo");
1303 let num_ops = 9;
1304 for i in 0..num_ops {
1305 work_dir
1306 .run_jj(["describe", "-m", &format!("description {i}")])
1307 .success();
1308 }
1309
1310 let work_dir = test_env.work_dir("repo");
1311
1312 let output = work_dir.complete_fish(["op", "show", ""]).success();
1313 insta::assert_snapshot!(output.take_stdout_n_lines(num_ops + 2), @"
1314 c5fcd0d80cb7 (2001-02-03 08:05:16) describe commit e0e6c0a964c024a49605805925672044dfae4181
1315 5bdb507f19ba (2001-02-03 08:05:15) describe commit 37df8a6c1874ff45621dee0f2b7a77169b65d257
1316 8a4a92e5ecff (2001-02-03 08:05:14) describe commit c3588cff852e44b68297f51705d6e61888806ddd
1317 aac41ddc75bf (2001-02-03 08:05:13) describe commit aa0b3230e3787076f232a08c8b1c7f54948a2d7a
1318 c915cd6b4c8d (2001-02-03 08:05:12) describe commit 96157804fd41363cb2ff8ff957ff1df1a2a1109a
1319 26d5ef64718f (2001-02-03 08:05:11) describe commit 3725536d0ae06d69e46911258cee591dbdb66478
1320 1c97a622e394 (2001-02-03 08:05:10) describe commit dd7390802e3ca4467ffa43f2e0c0374463d056f3
1321 3ff36c586388 (2001-02-03 08:05:09) describe commit 3ae22e7f50a15d393e412cca72d09a61165d0c84
1322 7749eb4df93f (2001-02-03 08:05:08) describe commit e8849ae12c709f2321908879bc724fdb2ab8a781
1323 f63ee16f9553 (2001-02-03 08:05:07) add workspace 'default'
1324 000000000000 (1970-01-01 11:00:00)
1325 [EOF]
1326 ");
1327
1328 let output = work_dir.complete_fish(["op", "show", "c"]);
1329 insta::assert_snapshot!(output, @"
1330 c5fcd0d80cb7 (2001-02-03 08:05:16) describe commit e0e6c0a964c024a49605805925672044dfae4181
1331 c915cd6b4c8d (2001-02-03 08:05:12) describe commit 96157804fd41363cb2ff8ff957ff1df1a2a1109a
1332 [EOF]
1333 ");
1334 // make sure global --at-op flag is respected (should not include later
1335 // operations)
1336 let output = work_dir.complete_fish(["--at-op", "f63ee16f9553", "op", "show", "f"]);
1337 insta::assert_snapshot!(output, @"
1338 f63ee16f9553 (2001-02-03 08:05:07) add workspace 'default'
1339 [EOF]
1340 ");
1341
1342 let output = work_dir.complete_fish(["--at-op", "c5"]);
1343 insta::assert_snapshot!(output, @"
1344 c5fcd0d80cb7 (2001-02-03 08:05:16) describe commit e0e6c0a964c024a49605805925672044dfae4181
1345 [EOF]
1346 ");
1347
1348 let output = work_dir.complete_fish(["op", "abandon", "c5"]);
1349 insta::assert_snapshot!(output, @"
1350 c5fcd0d80cb7 (2001-02-03 08:05:16) describe commit e0e6c0a964c024a49605805925672044dfae4181
1351 [EOF]
1352 ");
1353
1354 let output = work_dir.complete_fish(["op", "diff", "--op", "c5"]);
1355 insta::assert_snapshot!(output, @"
1356 c5fcd0d80cb7 (2001-02-03 08:05:16) describe commit e0e6c0a964c024a49605805925672044dfae4181
1357 [EOF]
1358 ");
1359 let output = work_dir.complete_fish(["op", "diff", "--from", "c5"]);
1360 insta::assert_snapshot!(output, @"
1361 c5fcd0d80cb7 (2001-02-03 08:05:16) describe commit e0e6c0a964c024a49605805925672044dfae4181
1362 [EOF]
1363 ");
1364 let output = work_dir.complete_fish(["op", "diff", "--to", "c5"]);
1365 insta::assert_snapshot!(output, @"
1366 c5fcd0d80cb7 (2001-02-03 08:05:16) describe commit e0e6c0a964c024a49605805925672044dfae4181
1367 [EOF]
1368 ");
1369
1370 let output = work_dir.complete_fish(["op", "restore", "c5"]);
1371 insta::assert_snapshot!(output, @"
1372 c5fcd0d80cb7 (2001-02-03 08:05:16) describe commit e0e6c0a964c024a49605805925672044dfae4181
1373 [EOF]
1374 ");
1375
1376 let output = work_dir.complete_fish(["op", "revert", "c5"]);
1377 insta::assert_snapshot!(output, @"
1378 c5fcd0d80cb7 (2001-02-03 08:05:16) describe commit e0e6c0a964c024a49605805925672044dfae4181
1379 [EOF]
1380 ");
1381 }
1382
1383 #[test]
1384 fn test_workspaces() {
1385 let test_env = TestEnvironment::default();
1386 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1387 let main_dir = test_env.work_dir("main");
1388
1389 main_dir.write_file("file", "contents");
1390 main_dir.run_jj(["describe", "-m", "initial"]).success();
1391
1392 // same prefix as "default" workspace
1393 main_dir
1394 .run_jj(["workspace", "add", "--name", "def-second", "../secondary"])
1395 .success();
1396
1397 let main_dir = test_env.work_dir("main");
1398
1399 let output = main_dir.complete_fish(["workspace", "forget", "def"]);
1400 insta::assert_snapshot!(output, @"
1401 def-second (no description set)
1402 default initial
1403 [EOF]
1404 ");
1405 }
1406
1407 #[test]
1408 fn test_config() {
1409 let test_env = TestEnvironment::default();
1410
1411 let output = test_env.complete_fish(["config", "get", "f"]);
1412 insta::assert_snapshot!(output, @"
1413 fsmonitor.backend Whether to use an external filesystem monitor, useful for large repos
1414 fsmonitor.watchman.register-snapshot-trigger Whether to use triggers to monitor for changes in the background.
1415 [EOF]
1416 ");
1417
1418 let output = test_env.complete_fish(["config", "list", "fs"]);
1419 insta::assert_snapshot!(output, @"
1420 fsmonitor External filesystem monitor settings, useful for large repos
1421 fsmonitor.backend Whether to use an external filesystem monitor, useful for large repos
1422 fsmonitor.watchman
1423 fsmonitor.watchman.register-snapshot-trigger Whether to use triggers to monitor for changes in the background.
1424 [EOF]
1425 ");
1426
1427 let output = test_env.complete_fish(["log", "--config", "f"]);
1428 insta::assert_snapshot!(output, @"
1429 fsmonitor.backend= Whether to use an external filesystem monitor, useful for large repos
1430 fsmonitor.watchman.register-snapshot-trigger= Whether to use triggers to monitor for changes in the background.
1431 [EOF]
1432 ");
1433
1434 let output = test_env.complete_fish(["log", "--config", "ui.conflict-marker-style="]);
1435 insta::assert_snapshot!(output, @"
1436 ui.conflict-marker-style=diff
1437 ui.conflict-marker-style=diff-experimental
1438 ui.conflict-marker-style=snapshot
1439 ui.conflict-marker-style=git
1440 [EOF]
1441 ");
1442
1443 let output = test_env.complete_fish(["log", "--config", "ui.conflict-marker-style=g"]);
1444 insta::assert_snapshot!(output, @"
1445 ui.conflict-marker-style=git
1446 [EOF]
1447 ");
1448
1449 let output = test_env.complete_fish(["log", "--config", "git.abandon-unreachable-commits="]);
1450 insta::assert_snapshot!(output, @"
1451 git.abandon-unreachable-commits=false
1452 git.abandon-unreachable-commits=true
1453 [EOF]
1454 ");
1455 }
1456
1457 #[test]
1458 fn test_config_unset() {
1459 let test_env = TestEnvironment::default();
1460
1461 // Create a repo with repo and workspace config
1462 let repo_dir = test_env.work_dir("repo");
1463 test_env
1464 .run_jj_in(test_env.env_root(), ["git", "init", "repo"])
1465 .success();
1466 repo_dir
1467 .run_jj(["config", "set", "--workspace", "ui.pager", "delta"])
1468 .success();
1469 repo_dir
1470 .run_jj(["config", "set", "--repo", "ui.diff-formatter", ":git"])
1471 .success();
1472
1473 // Only config options set in TestEnvironment are suggested initially + other
1474 // viable flags
1475 let output = test_env.complete_fish(["config", "unset", ""]);
1476 insta::assert_snapshot!(output.take_stdout_n_lines(5), @r#"
1477 template-aliases."format_time_range(time_range)" user: 'time_range.start() ++ " - " ++ time_range.end()'
1478 git.colocate user: false
1479 --user Target the user-level config
1480 --repo Target the repo-level config
1481 --workspace Target the workspace-level config
1482 [EOF]
1483 "#);
1484
1485 test_env.add_config(indoc! {r#"
1486 [ui]
1487 editor = "nvim"
1488 default-command = ["log", "--stat"]
1489
1490 [revset-aliases]
1491 'closest_bookmark(to)' = 'heads(::to & bookmarks())'
1492 'closest_pushable(to)' = 'heads(::to & ~description(exact:"") & (~empty() | merges()))'
1493
1494 [aliases]
1495 tug = ["bookmark", "move", "--from", "closest_bookmark(@)", "--to", "closest_pushable(@)"]
1496 cat = ["file", "show"]
1497 ll = ["log", "-T", "builtin_log_detailed", "-r", "::@"]
1498
1499 [templates]
1500 draft_commit_description = '''
1501 concat(
1502 coalesce(description, "\n"),
1503 surround(
1504 "\nJJ: This commit contains the following changes:\n", "",
1505 indent("JJ: ", diff.stat(72)),
1506 ),
1507 "\nJJ: ignore-rest\n",
1508 diff.git(),
1509 )
1510 '''
1511 "#});
1512
1513 // Matching config options are completed, including aliases
1514 let output = test_env.complete_fish(["config", "unset", "alias"]);
1515 insta::assert_snapshot!(output, @r#"
1516 aliases.tug user: ["bookmark", "move", "--from", "closest_bookmark(@)", "--to", "closest_pushable(@)"]
1517 aliases.cat user: ["file", "show"]
1518 aliases.ll user: ["log", "-T", "builtin_log_detailed", "-r", "::@"]
1519 [EOF]
1520 "#);
1521
1522 // Quoted config option keys are completed (accepting double-quotes only)
1523 let output = test_env.complete_fish(["config", "unset", "revset-aliases.\"close"]);
1524 insta::assert_snapshot!(output, @r#"
1525 revset-aliases."closest_bookmark(to)" user: 'heads(::to & bookmarks())'
1526 revset-aliases."closest_pushable(to)" user: 'heads(::to & ~description(exact:"") & (~empty() | merges()))'
1527 [EOF]
1528 "#);
1529 let output = test_env.complete_fish(["config", "unset", "revset-aliases.'close"]);
1530 insta::assert_snapshot!(output, @"");
1531
1532 // Multiline config values are squeezed onto a single line
1533 let output = test_env.complete_fish(["config", "unset", "templates"]);
1534 insta::assert_snapshot!(output, @r#"
1535 templates.draft_commit_description user: ''' concat( coalesce(description, "\n"), surround( "\nJJ: This commit contains the following changes:\n", "", indent("JJ: ", diff.stat(72)), ), "\nJJ: ignore-rest\n", diff.git(), ) '''
1536 [EOF]
1537 "#);
1538
1539 // If no config source is specified yet, options from all sources are completed
1540 let output = repo_dir.complete_fish(["config", "unset", "ui"]);
1541 insta::assert_snapshot!(output, @r#"
1542 ui.editor user: "nvim"
1543 ui.default-command user: ["log", "--stat"]
1544 ui.diff-formatter repo: ":git"
1545 ui.pager workspace: "delta"
1546 [EOF]
1547 "#);
1548
1549 // If a config source has already been specified, only its options as completed
1550 let output = repo_dir.complete_fish(["config", "unset", "--user", "ui"]);
1551 insta::assert_snapshot!(output, @r#"
1552 ui.editor user: "nvim"
1553 ui.default-command user: ["log", "--stat"]
1554 [EOF]
1555 "#);
1556 let output = repo_dir.complete_fish(["config", "unset", "--repo", "ui"]);
1557 insta::assert_snapshot!(output, @r#"
1558 ui.diff-formatter repo: ":git"
1559 [EOF]
1560 "#);
1561 let output = repo_dir.complete_fish(["config", "unset", "--workspace", "ui"]);
1562 insta::assert_snapshot!(output, @r#"
1563 ui.pager workspace: "delta"
1564 [EOF]
1565 "#);
1566
1567 // Override an option in the repo config
1568 repo_dir
1569 .run_jj(["config", "set", "--repo", "ui.editor", "hx"])
1570 .success();
1571
1572 // If no config source is specified yet, the overridden value is listed
1573 let output = repo_dir.complete_fish(["config", "unset", "ui.editor"]);
1574 insta::assert_snapshot!(output, @r#"
1575 ui.editor repo: "hx"
1576 [EOF]
1577 "#);
1578
1579 // If a config source has already been specified, the value according to that
1580 // source is listed
1581 let output = repo_dir.complete_fish(["config", "unset", "--user", "ui.editor"]);
1582 insta::assert_snapshot!(output, @r#"
1583 ui.editor user: "nvim"
1584 [EOF]
1585 "#);
1586 let output = repo_dir.complete_fish(["config", "unset", "--repo", "ui.editor"]);
1587 insta::assert_snapshot!(output, @r#"
1588 ui.editor repo: "hx"
1589 [EOF]
1590 "#);
1591 let output = repo_dir.complete_fish(["config", "unset", "--workspace", "ui.editor"]);
1592 insta::assert_snapshot!(output, @"");
1593 }
1594
1595 #[test]
1596 fn test_template_alias() {
1597 let test_env = TestEnvironment::default();
1598
1599 let output = test_env.complete_fish(["log", "-T", ""]);
1600 insta::assert_snapshot!(output, @"
1601 builtin_config_list
1602 builtin_config_list_detailed
1603 builtin_draft_commit_description
1604 builtin_draft_commit_description_with_diff
1605 builtin_evolog_compact
1606 builtin_log_comfortable
1607 builtin_log_compact
1608 builtin_log_compact_full_description
1609 builtin_log_detailed
1610 builtin_log_node
1611 builtin_log_node_ascii
1612 builtin_log_oneline
1613 builtin_log_redacted
1614 builtin_op_log_comfortable
1615 builtin_op_log_compact
1616 builtin_op_log_node
1617 builtin_op_log_node_ascii
1618 builtin_op_log_oneline
1619 builtin_op_log_redacted
1620 builtin_workspace_list
1621 commit_summary_separator
1622 default_commit_description
1623 description_placeholder
1624 email_placeholder
1625 empty_commit_marker
1626 git_format_patch_email_headers
1627 name_placeholder
1628 [EOF]
1629 ");
1630 }
1631
1632 #[test]
1633 fn test_merge_tools() {
1634 let mut test_env = TestEnvironment::default();
1635 // A tool without configured arguments is assumed to function as a diff
1636 // editor and formatter, but not as a merge editor.
1637 test_env.add_config("merge-tools.abracadabra={}");
1638 test_env.add_env_var("COMPLETE", "fish");
1639 let dir = test_env.env_root();
1640
1641 let output = test_env.run_jj_in(dir, ["--", "jj", "diff", "--tool", ""]);
1642 // Includes `difft`, excludes merge tools like `mergiraf`
1643 insta::assert_snapshot!(output, @"
1644 :summary
1645 :stat
1646 :types
1647 :name-only
1648 :git
1649 :color-words
1650 diffedit3
1651 diffedit3-ssh
1652 difft
1653 kdiff3
1654 meld
1655 meld-3
1656 vscode
1657 vscodium
1658 abracadabra
1659 [EOF]
1660 ");
1661 // Excludes `difft` and `mergiraf`
1662 let output = test_env.run_jj_in(dir, ["--", "jj", "diffedit", "--tool", ""]);
1663 insta::assert_snapshot!(output, @"
1664 :builtin
1665 diffedit3
1666 diffedit3-ssh
1667 kdiff3
1668 meld
1669 meld-3
1670 vimdiff
1671 abracadabra
1672 [EOF]
1673 ");
1674 // Includes `mergiraf`, but not `difft` or `abracadabra`
1675 let output = test_env.run_jj_in(dir, ["--", "jj", "resolve", "--tool", ""]);
1676 insta::assert_snapshot!(output, @"
1677 :builtin
1678 :ours
1679 :theirs
1680 kdiff3
1681 meld
1682 mergiraf
1683 smerge
1684 vimdiff
1685 vscode
1686 vscodium
1687 [EOF]
1688 ");
1689 }
1690
1691 fn create_commit(
1692 work_dir: &TestWorkDir,
1693 name: &str,
1694 parents: &[&str],
1695 files: &[(&str, Option<&str>)],
1696 ) {
1697 let parents = match parents {
1698 [] => &["root()"],
1699 parents => parents,
1700 };
1701 work_dir
1702 .run_jj_with(|cmd| cmd.args(["new", "-m", name]).args(parents))
1703 .success();
1704 for (name, content) in files {
1705 if let Some((dir, _)) = name.rsplit_once('/') {
1706 work_dir.create_dir_all(dir);
1707 }
1708 match content {
1709 Some(content) => work_dir.write_file(name, content),
1710 None => work_dir.remove_file(name),
1711 }
1712 }
1713 work_dir
1714 .run_jj(["bookmark", "create", "-r@", name])
1715 .success();
1716 }
1717
1718 #[test]
1719 fn test_files() {
1720 let test_env = TestEnvironment::default();
1721 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1722 let work_dir = test_env.work_dir("repo");
1723
1724 create_commit(
1725 &work_dir,
1726 "first",
1727 &[],
1728 &[
1729 ("f_unchanged", Some("unchanged\n")),
1730 ("f_modified", Some("not_yet_modified\n")),
1731 ("f_not_yet_copied", Some("copied\n")),
1732 ("f_not_yet_renamed", Some("renamed\n")),
1733 ("f_not_yet_renamed_2", Some("renamed_2\n")),
1734 ("f_not_yet_renamed_3", Some("renamed_3\n")),
1735 ("f_deleted", Some("not_yet_deleted\n")),
1736 // not yet: "added" file
1737 ],
1738 );
1739 create_commit(
1740 &work_dir,
1741 "second",
1742 &["first"],
1743 &[
1744 // "unchanged" file
1745 ("f_modified", Some("modified\n")),
1746 ("f_not_yet_copied", Some("copied\n\n")),
1747 ("f_not_yet_renamed", None),
1748 ("f_not_yet_renamed_2", None),
1749 ("f_not_yet_renamed_3", None),
1750 ("f_copied", Some("copied\n")),
1751 // f_not_yet_renamed < f_renamed
1752 ("f_renamed", Some("renamed\n")),
1753 // f_another_renamed_2 < f_not_yet_renamed_2
1754 ("f_another_renamed_2", Some("renamed_2\n")),
1755 ("f_deleted", None),
1756 ("f_added", Some("added\n")),
1757 ("f_dir/dir_file_1", Some("foo\n")),
1758 ("f_dir/dir_file_2", Some("foo\n")),
1759 ("f_dir/dir_file_3", Some("foo\n")),
1760 ("f_dir/f_renamed_3", Some("renamed_3\n")),
1761 ],
1762 );
1763
1764 // create a conflicted commit to check the completions of `jj restore`
1765 create_commit(
1766 &work_dir,
1767 "conflicted",
1768 &["second"],
1769 &[
1770 ("f_modified", Some("modified_again\n")),
1771 ("f_added_2", Some("added_2\n")),
1772 ("f_dir/dir_file_1", Some("bar\n")),
1773 ("f_dir/dir_file_2", Some("bar\n")),
1774 ("f_dir/dir_file_3", Some("bar\n")),
1775 ],
1776 );
1777 work_dir.run_jj(["rebase", "-r=@", "-d=first"]).success();
1778
1779 // two commits that are similar but not identical, for `jj interdiff`
1780 create_commit(
1781 &work_dir,
1782 "interdiff_from",
1783 &[],
1784 &[
1785 ("f_interdiff_same", Some("same in both commits\n")),
1786 (("f_interdiff_only_from"), Some("only from\n")),
1787 ],
1788 );
1789 create_commit(
1790 &work_dir,
1791 "interdiff_to",
1792 &[],
1793 &[
1794 ("f_interdiff_same", Some("same in both commits\n")),
1795 (("f_interdiff_only_to"), Some("only to\n")),
1796 ],
1797 );
1798
1799 // "dirty worktree"
1800 create_commit(
1801 &work_dir,
1802 "working_copy",
1803 &["second"],
1804 &[
1805 ("f_modified", Some("modified_again\n")),
1806 ("f_added_2", Some("added_2\n")),
1807 ],
1808 );
1809
1810 let output = work_dir.run_jj(["log", "-r", "all()", "--summary"]);
1811 insta::assert_snapshot!(output.normalize_backslash(), @"
1812 @ wqnwkozp [email protected] 2001-02-03 08:05:20 working_copy 5e0882cf
1813 │ working_copy
1814 │ A f_added_2
1815 │ M f_modified
1816 ○ zsuskuln [email protected] 2001-02-03 08:05:11 second 5d65dc93
1817 │ second
1818 │ A f_added
1819 │ R {f_not_yet_renamed_2 => f_another_renamed_2}
1820 │ C {f_not_yet_copied => f_copied}
1821 │ D f_deleted
1822 │ A f_dir/dir_file_1
1823 │ A f_dir/dir_file_2
1824 │ A f_dir/dir_file_3
1825 │ R {f_not_yet_renamed_3 => f_dir/f_renamed_3}
1826 │ M f_modified
1827 │ M f_not_yet_copied
1828 │ R {f_not_yet_renamed => f_renamed}
1829 │ × royxmykx [email protected] 2001-02-03 08:05:14 conflicted 26ca82ca (conflict)
1830 ├─╯ conflicted
1831 │ A f_added_2
1832 │ A f_dir/dir_file_1
1833 │ A f_dir/dir_file_2
1834 │ A f_dir/dir_file_3
1835 │ M f_modified
1836 ○ rlvkpnrz [email protected] 2001-02-03 08:05:09 first 221854a7
1837 │ first
1838 │ A f_deleted
1839 │ A f_modified
1840 │ A f_not_yet_copied
1841 │ A f_not_yet_renamed
1842 │ A f_not_yet_renamed_2
1843 │ A f_not_yet_renamed_3
1844 │ A f_unchanged
1845 │ ○ kpqxywon [email protected] 2001-02-03 08:05:18 interdiff_to 5e448a34
1846 ├─╯ interdiff_to
1847 │ A f_interdiff_only_to
1848 │ A f_interdiff_same
1849 │ ○ yostqsxw [email protected] 2001-02-03 08:05:16 interdiff_from 039b07b8
1850 ├─╯ interdiff_from
1851 │ A f_interdiff_only_from
1852 │ A f_interdiff_same
1853 ◆ zzzzzzzz root() 00000000
1854 [EOF]
1855 ");
1856
1857 let work_dir = test_env.work_dir("repo");
1858
1859 let output = work_dir.complete_fish(["file", "show", "f_"]);
1860 insta::assert_snapshot!(output, @"
1861 f_added
1862 f_added_2
1863 f_another_renamed_2
1864 f_copied
1865 f_dir/
1866 f_modified
1867 f_not_yet_copied
1868 f_renamed
1869 f_unchanged
1870 [EOF]
1871 ");
1872
1873 let output = work_dir.complete_fish(["file", "show", "./f_"]);
1874 insta::assert_snapshot!(output, @"
1875 ./f_added
1876 ./f_added_2
1877 ./f_another_renamed_2
1878 ./f_copied
1879 ./f_dir/
1880 ./f_modified
1881 ./f_not_yet_copied
1882 ./f_renamed
1883 ./f_unchanged
1884 [EOF]
1885 ");
1886
1887 let output = work_dir.complete_fish(["file", "show", "f_dir"]);
1888 insta::assert_snapshot!(output, @"
1889 f_dir/
1890 [EOF]
1891 ");
1892
1893 let output = work_dir.complete_fish(["file", "show", "f_dir/"]);
1894 insta::assert_snapshot!(output, @"
1895 f_dir/dir_file_1
1896 f_dir/dir_file_2
1897 f_dir/dir_file_3
1898 f_dir/f_renamed_3
1899 [EOF]
1900 ");
1901
1902 let output = work_dir.complete_fish(["file", "show", "f_dir/../"]);
1903 insta::assert_snapshot!(output, @"
1904 f_dir/../f_added
1905 f_dir/../f_added_2
1906 f_dir/../f_another_renamed_2
1907 f_dir/../f_copied
1908 f_dir/../f_dir/
1909 f_dir/../f_modified
1910 f_dir/../f_not_yet_copied
1911 f_dir/../f_renamed
1912 f_dir/../f_unchanged
1913 [EOF]
1914 ");
1915
1916 let output = work_dir.complete_fish(["file", "show", "f_dir/../f_dir/"]);
1917 insta::assert_snapshot!(output, @"
1918 f_dir/../f_dir/dir_file_1
1919 f_dir/../f_dir/dir_file_2
1920 f_dir/../f_dir/dir_file_3
1921 f_dir/../f_dir/f_renamed_3
1922 [EOF]
1923 ");
1924
1925 let subdir = work_dir.dir("f_dir");
1926 let output = subdir.complete_fish(["file", "show", "dir_"]);
1927 insta::assert_snapshot!(output, @"
1928 dir_file_1
1929 dir_file_2
1930 dir_file_3
1931 [EOF]
1932 ");
1933
1934 let output = subdir.complete_fish(["file", "show", "./"]);
1935 insta::assert_snapshot!(output, @"
1936 ./dir_file_1
1937 ./dir_file_2
1938 ./dir_file_3
1939 ./f_renamed_3
1940 [EOF]
1941 ");
1942
1943 let output = subdir.complete_fish(["file", "show", "../"]);
1944 insta::assert_snapshot!(output, @"
1945 ../f_added
1946 ../f_added_2
1947 ../f_another_renamed_2
1948 ../f_copied
1949 ../f_modified
1950 ../f_not_yet_copied
1951 ../f_renamed
1952 ../f_unchanged
1953 [EOF]
1954 ");
1955
1956 let output = work_dir.complete_fish(["file", "annotate", "-r@-", "f_"]);
1957 insta::assert_snapshot!(output, @"
1958 f_added
1959 f_another_renamed_2
1960 f_copied
1961 f_dir/
1962 f_modified
1963 f_not_yet_copied
1964 f_renamed
1965 f_unchanged
1966 [EOF]
1967 ");
1968
1969 let output = work_dir.complete_fish(["diff", "-r", "@-", "f_"]);
1970 insta::assert_snapshot!(output, @"
1971 f_added Added
1972 f_another_renamed_2 Renamed
1973 f_copied Copied
1974 f_deleted Deleted
1975 f_dir/
1976 f_modified Modified
1977 f_not_yet_copied Modified
1978 f_not_yet_renamed Renamed
1979 f_not_yet_renamed_2 Renamed
1980 f_not_yet_renamed_3 Renamed
1981 f_renamed Renamed
1982 [EOF]
1983 ");
1984
1985 let output = work_dir.complete_fish(["diff", "--revisions=@-", "f_"]);
1986 insta::assert_snapshot!(output, @"
1987 f_added Added
1988 f_another_renamed_2 Renamed
1989 f_copied Copied
1990 f_deleted Deleted
1991 f_dir/
1992 f_modified Modified
1993 f_not_yet_copied Modified
1994 f_not_yet_renamed Renamed
1995 f_not_yet_renamed_2 Renamed
1996 f_not_yet_renamed_3 Renamed
1997 f_renamed Renamed
1998 [EOF]
1999 ");
2000
2001 let output = work_dir.complete_fish(["diff", "-r", "first", "--revisions=second", "f_"]);
2002 insta::assert_snapshot!(output, @"
2003 f_added Added
2004 f_another_renamed_2 Added
2005 f_copied Added
2006 f_dir/
2007 f_modified Added
2008 f_not_yet_copied Added
2009 f_renamed Added
2010 f_unchanged Added
2011 [EOF]
2012 ");
2013
2014 let output = work_dir.complete_fish(["diff", "-r", "@-", "f_dir/../"]);
2015 insta::assert_snapshot!(output, @"
2016 f_dir/../f_added Added
2017 f_dir/../f_another_renamed_2 Renamed
2018 f_dir/../f_copied Copied
2019 f_dir/../f_deleted Deleted
2020 f_dir/../f_dir/
2021 f_dir/../f_modified Modified
2022 f_dir/../f_not_yet_copied Modified
2023 f_dir/../f_not_yet_renamed Renamed
2024 f_dir/../f_not_yet_renamed_2 Renamed
2025 f_dir/../f_not_yet_renamed_3 Renamed
2026 f_dir/../f_renamed Renamed
2027 [EOF]
2028 ");
2029
2030 // Given that the path prefix uses the main separator (e.g. `\` on Windows),
2031 // check that the completion continues to use the same separator.
2032 // The assertion maps the main separator to some arbitrary fictitious separator
2033 // (`→`) which is not used by real OSes (yet) to check that the main separator
2034 // is preserved on platforms where it differs from `/`.
2035 let output = work_dir.complete_fish([
2036 "diff",
2037 "-r",
2038 "@-",
2039 &format!("f_dir{}", std::path::MAIN_SEPARATOR),
2040 ]);
2041 insta::assert_snapshot!(
2042 output.normalize_stdout_with(|s| s.replace(std::path::MAIN_SEPARATOR, "→")),
2043 @"
2044 f_dir→dir_file_1 Added
2045 f_dir→dir_file_2 Added
2046 f_dir→dir_file_3 Added
2047 f_dir→f_renamed_3 Renamed
2048 [EOF]
2049 ");
2050
2051 let output = work_dir.complete_fish(["diff", "--from", "root()", "--to", "@-", "f_"]);
2052 insta::assert_snapshot!(output, @"
2053 f_added Added
2054 f_another_renamed_2 Added
2055 f_copied Added
2056 f_dir/
2057 f_modified Added
2058 f_not_yet_copied Added
2059 f_renamed Added
2060 f_unchanged Added
2061 [EOF]
2062 ");
2063
2064 let output = work_dir.complete_fish(["diff", "--to=second", "f_"]);
2065 insta::assert_snapshot!(output, @"
2066 f_added_2 Deleted
2067 f_modified Modified
2068 [EOF]
2069 ");
2070
2071 let output = work_dir.complete_fish(["restore", "-c", "@-", "f_"]);
2072 insta::assert_snapshot!(output.normalize_backslash(), @"
2073 f_added Added
2074 f_another_renamed_2 Renamed
2075 f_copied Copied
2076 f_deleted Deleted
2077 f_dir/
2078 f_modified Modified
2079 f_not_yet_copied Modified
2080 f_not_yet_renamed Renamed
2081 f_not_yet_renamed_2 Renamed
2082 f_not_yet_renamed_3 Renamed
2083 f_renamed Renamed
2084 [EOF]
2085 ");
2086
2087 let output = work_dir.complete_fish(["restore", "--from", "root()", "--to", "@-", "f_"]);
2088 insta::assert_snapshot!(output.normalize_backslash(), @"
2089 f_added Added
2090 f_another_renamed_2 Added
2091 f_copied Added
2092 f_dir/
2093 f_modified Added
2094 f_not_yet_copied Added
2095 f_renamed Added
2096 f_unchanged Added
2097 [EOF]
2098 ");
2099
2100 // interdiff has a different behavior with --from and --to flags
2101 let output = work_dir.complete_fish([
2102 "interdiff",
2103 "--to=interdiff_to",
2104 "--from=interdiff_from",
2105 "f_",
2106 ]);
2107 insta::assert_snapshot!(output, @"
2108 f_interdiff_only_from Added
2109 f_interdiff_same Added
2110 f_interdiff_only_to Added
2111 f_interdiff_same Added
2112 [EOF]
2113 ");
2114
2115 // squash has a different behavior with --from and --to flags
2116 let output = work_dir.complete_fish(["squash", "-f=first", "f_"]);
2117 insta::assert_snapshot!(output, @"
2118 f_deleted Added
2119 f_modified Added
2120 f_not_yet_copied Added
2121 f_not_yet_renamed Added
2122 f_not_yet_renamed_2 Added
2123 f_not_yet_renamed_3 Added
2124 f_unchanged Added
2125 [EOF]
2126 ");
2127
2128 let output = work_dir.complete_fish(["resolve", "-r=conflicted", "f_"]);
2129 insta::assert_snapshot!(output, @"
2130 f_dir/
2131 f_modified
2132 [EOF]
2133 ");
2134
2135 let output = work_dir.complete_fish(["file", "list", "-r=first", "f_"]);
2136 insta::assert_snapshot!(output, @"
2137 f_deleted
2138 f_modified
2139 f_not_yet_copied
2140 f_not_yet_renamed
2141 f_not_yet_renamed_2
2142 f_not_yet_renamed_3
2143 f_unchanged
2144 [EOF]
2145 ");
2146
2147 let output = work_dir.complete_fish(["log", "f_"]);
2148 insta::assert_snapshot!(output, @"
2149 f_added
2150 f_added_2
2151 f_another_renamed_2
2152 f_copied
2153 f_dir/
2154 f_modified
2155 f_not_yet_copied
2156 f_renamed
2157 f_unchanged
2158 [EOF]
2159 ");
2160
2161 let output = work_dir.complete_fish(["log", "-r=first", "--revisions", "conflicted", "f_"]);
2162 insta::assert_snapshot!(output, @"
2163 f_added_2
2164 f_deleted
2165 f_dir/
2166 f_modified
2167 f_not_yet_copied
2168 f_not_yet_renamed
2169 f_not_yet_renamed_2
2170 f_not_yet_renamed_3
2171 f_unchanged
2172 [EOF]
2173 ");
2174
2175 let outside_repo = test_env.env_root();
2176 let output = test_env.work_dir(outside_repo).complete_fish(["log", "f_"]);
2177 insta::assert_snapshot!(output, @"");
2178
2179 let output = work_dir.complete_fish(["absorb", "f_"]);
2180 insta::assert_snapshot!(output, @"
2181 f_added_2 Added
2182 f_modified Modified
2183 [EOF]
2184 ");
2185
2186 let output = work_dir.complete_fish(["absorb", "-f=conflicted", "f_"]);
2187 insta::assert_snapshot!(output, @"
2188 f_added_2 Added
2189 f_dir/
2190 f_modified Modified
2191 [EOF]
2192 ");
2193 }
2194
2195 #[test]
2196 fn test_command_alias_with_exec() {
2197 let test_env = TestEnvironment::default();
2198 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2199 let work_dir = test_env.work_dir("repo");
2200
2201 test_env.add_config(r#"aliases.my-script = ["util", "exec", "--", "my-jj-script"]"#);
2202
2203 work_dir.write_file("file1", "contents");
2204 work_dir.write_file("file2", "contents");
2205 work_dir.create_dir("folder");
2206 work_dir.write_file("folder/subfile", "contents");
2207
2208 let output = work_dir.complete_fish(["my-script", "f"]);
2209 insta::assert_snapshot!(output.normalize_backslash(), @"
2210 file1
2211 file2
2212 folder/
2213 [EOF]
2214 ");
2215 }