Debian Codegraph @

cli/tests/test_run_command.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
16 use std::fs;
17
18 use insta::assert_snapshot;
19
20 use crate::common::TestEnvironment;
21 use crate::common::TestWorkDir;
22 use crate::common::create_commit_with_files;
23 use crate::common::fake_editor_path;
24
25 #[test]
26 fn test_run_simple() {
27 let mut test_env = TestEnvironment::default();
28 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
29 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
30 assert!(fake_formatter.is_file());
31 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
32 test_env.add_paths_to_normalize(fake_formatter.clone(), "$FAKE_FORMATTER_PATH");
33 let work_dir = test_env.work_dir("repo");
34 work_dir.write_file("A.txt", "A");
35 work_dir.run_jj(&["commit", "-m", "A"]).success();
36 work_dir.write_file("b.txt", "b");
37 work_dir.run_jj(&["commit", "-m", "B"]).success();
38 work_dir.write_file("c.txt", "test to replace");
39 work_dir.run_jj(&["commit", "-m", "C"]).success();
40 insta::assert_snapshot!(get_log_output(&work_dir), @r"
41 @ zsuskulnrvyrovkzqrwmxqlsskqntxvp
42 ○ kkmpptxzrspxrzommnulwmwkkqwworplC
43 │
44 ○ rlvkpnrzqnoowoytxnquwvuryrwnrmlpB
45 │
46 ○ qpvuntsmwlqtpsluzzsnyyzlmlwvmlnuA
47 │
48 ◆ zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
49 [EOF]
50 ");
51 // `--tee touched.txt` creates a file in each working copy, so every commit's
52 // tree gets rewritten.
53 let stdout = work_dir
54 .run_jj(&[
55 "run",
56 "-r",
57 "..@",
58 "--",
59 &fake_formatter_path,
60 "--stdout",
61 "x",
62 "--tee",
63 "touched.txt",
64 ])
65 .success()
66 .stdout;
67 insta::assert_snapshot!(stdout, @"xxxx[EOF]");
68 }
69
70 #[test]
71 fn test_run_on_immutable() {
72 let test_env = TestEnvironment::default();
73 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
74 let work_dir = test_env.work_dir("repo");
75 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
76 assert!(fake_formatter.is_file());
77 let fake_formatter_path = fake_formatter.to_string_lossy();
78 work_dir.write_file("A.txt", "A");
79 work_dir.run_jj(&["commit", "-m", "A"]).success();
80 work_dir.write_file("b.txt", "b");
81 work_dir.run_jj(&["commit", "-m", "B"]).success();
82 work_dir.write_file("c.txt", "test to replace");
83 work_dir.run_jj(&["commit", "-m", "C"]).success();
84 insta::assert_snapshot!(get_log_output(&work_dir), @r"
85 @ zsuskulnrvyrovkzqrwmxqlsskqntxvp
86 ○ kkmpptxzrspxrzommnulwmwkkqwworplC
87 │
88 ○ rlvkpnrzqnoowoytxnquwvuryrwnrmlpB
89 │
90 ○ qpvuntsmwlqtpsluzzsnyyzlmlwvmlnuA
91 │
92 ◆ zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
93 [EOF]
94 ");
95 let output = work_dir.run_jj(&[
96 "run",
97 "-r",
98 "all()",
99 "--",
100 &fake_formatter_path,
101 "--uppercase",
102 ]);
103 insta::assert_snapshot!(output, @r"
104 ------- stderr -------
105 Error: The root commit 000000000000 is immutable
106 [EOF]
107 [exit status: 1]
108 ");
109 }
110
111 #[test]
112 fn test_run_noop() {
113 let mut test_env = TestEnvironment::default();
114 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
115 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
116 assert!(fake_formatter.is_file());
117 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
118 test_env.add_paths_to_normalize(fake_formatter.clone(), "$FAKE_FORMATTER_PATH");
119 let work_dir = test_env.work_dir("repo");
120 work_dir.write_file("A.txt", "A");
121 work_dir.run_jj(&["commit", "-m", "A"]).success();
122 work_dir.write_file("b.txt", "b");
123 work_dir.run_jj(&["commit", "-m", "B"]).success();
124 work_dir.write_file("c.txt", "test to replace");
125 work_dir.run_jj(&["commit", "-m", "C"]).success();
126 insta::assert_snapshot!(get_log_output(&work_dir), @r"
127 @ zsuskulnrvyrovkzqrwmxqlsskqntxvp
128 ○ kkmpptxzrspxrzommnulwmwkkqwworplC
129 │
130 ○ rlvkpnrzqnoowoytxnquwvuryrwnrmlpB
131 │
132 ○ qpvuntsmwlqtpsluzzsnyyzlmlwvmlnuA
133 │
134 ◆ zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
135 [EOF]
136 ");
137 // `--stdout foo` writes to the subprocess's stdout, which `jj run` buffers
138 // and emits to its own stdout. No tracked files in the working copy change,
139 // so no commits get rewritten. Using a fixed string keeps the per-commit
140 // output identical, so the concatenated stdout is stable regardless of the
141 // (non-deterministic) order in which the parallel jobs finish.
142 let output = work_dir
143 .run_jj(&[
144 "run",
145 "-r",
146 "..@",
147 "--",
148 &fake_formatter_path,
149 "--stdout",
150 "foo",
151 ])
152 .success();
153 insta::assert_snapshot!(output.stdout, @"foofoofoofoo[EOF]");
154 insta::assert_snapshot!(output.stderr, @r"
155 Nothing changed.
156 [EOF]
157 ");
158 }
159
160 #[test]
161 fn test_run_sets_env_vars() {
162 let test_env = TestEnvironment::default();
163 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
164 let work_dir = test_env.work_dir("repo");
165 work_dir.write_file("seed.txt", "seed");
166 work_dir.run_jj(&["commit", "-m", "seed"]).success();
167
168 // Show the change_id and commit_id so the reader can match them against
169 // the values the subprocess writes into the per-commit working copy.
170 let log_template = r#"change_id ++ " " ++ commit_id ++ " " ++ description ++ "\n""#;
171 insta::assert_snapshot!(
172 work_dir.run_jj(&["log", "-T", log_template]),
173 @r"
174 @ rlvkpnrzqnoowoytxnquwvuryrwnrmlp fc4c875c9bc90128cbb9e8084dd5f5f336b383d9
175 ○ qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu 5fbe90560fed1c39d46a46a672ba98abd53bdc6d seed
176 │
177 ◆ zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 0000000000000000000000000000000000000000
178 [EOF]
179 "
180 );
181
182 // Each subprocess echoes its JJ_CHANGE_ID and JJ_COMMIT_ID into files in
183 // the per-commit working copy, modifying the tree so the commit gets
184 // rewritten with those files.
185 let jj_args: &[&str] = if cfg!(windows) {
186 &[
187 "run",
188 "-r",
189 "@-",
190 "--",
191 "cmd",
192 "/c",
193 "echo %JJ_CHANGE_ID%>change_id.txt && echo %JJ_COMMIT_ID%>commit_id.txt",
194 ]
195 } else {
196 &[
197 "run",
198 "-r",
199 "@-",
200 "--",
201 "sh",
202 "-c",
203 "echo $JJ_CHANGE_ID > change_id.txt && echo $JJ_COMMIT_ID > commit_id.txt",
204 ]
205 };
206 work_dir.run_jj(jj_args).success();
207
208 let normalize_whitespace = |s: String| {
209 s.replace("\r\n", "\n")
210 .lines()
211 .map(|line| line.trim_end())
212 .collect::<Vec<_>>()
213 .join("\n")
214 + "\n"
215 };
216 insta::assert_snapshot!(
217 work_dir
218 .run_jj(&["file", "show", "-r", "@-", "change_id.txt"])
219 .normalize_stdout_with(normalize_whitespace),
220 @r"
221 qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu
222 [EOF]
223 "
224 );
225 insta::assert_snapshot!(
226 work_dir
227 .run_jj(&["file", "show", "-r", "@-", "commit_id.txt"])
228 .normalize_stdout_with(normalize_whitespace),
229 @r"
230 5fbe90560fed1c39d46a46a672ba98abd53bdc6d
231 [EOF]
232 "
233 );
234 }
235
236 #[test]
237 fn test_run_from_subdir_skips_commits_without_it() {
238 let mut test_env = TestEnvironment::default();
239 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
240 // `fake-formatter --tee ran.txt` is a portable way to create an empty
241 // `ran.txt`, equivalent to `touch ran.txt` but available on all platforms.
242 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
243 assert!(fake_formatter.is_file());
244 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
245 test_env.add_paths_to_normalize(fake_formatter.clone(), "$FAKE_FORMATTER_PATH");
246 let work_dir = test_env.work_dir("repo");
247
248 // First commit has only root-level files; no `sub/` exists yet.
249 work_dir.write_file("seed.txt", "seed");
250 work_dir.run_jj(&["commit", "-m", "no-sub"]).success();
251 // Second commit adds `sub/file.txt`, so `sub/` exists from here on.
252 work_dir.write_file("sub/file.txt", "x");
253 work_dir.run_jj(&["commit", "-m", "with-sub"]).success();
254
255 // Run from inside sub/ on both ancestors. The command creates `ran.txt`
256 // in cwd, so we can later tell where it ran. The `no-sub` commit has no
257 // `sub/` directory and should be skipped; the `with-sub` commit has
258 // `sub/` and should be rewritten with `sub/ran.txt` added.
259 let sub_dir = work_dir.dir("sub");
260 let output = sub_dir
261 .run_jj(&[
262 "run",
263 "-r",
264 "@-|@--",
265 "--",
266 &fake_formatter_path,
267 "--tee",
268 "ran.txt",
269 ])
270 .success()
271 .normalize_backslash();
272 insta::assert_snapshot!(output.stderr, @"
273 Skipped commit 3bb1f1ca3c09a8e6be46ef48515803464b16b426: directory does not exist: sub
274 Rewrote 1 commits.
275 Rebased 1 descendant commits.
276 Working copy (@) now at: kkmpptxz 47736de8 (empty) (no description set)
277 Parent commit (@-) : rlvkpnrz cb2b6779 with-sub
278 Added 1 files, modified 0 files, removed 0 files
279 [EOF]
280 ");
281
282 // The rewritten `with-sub` commit has `sub/ran.txt`, alongside the
283 // pre-existing `sub/file.txt`.
284 insta::assert_snapshot!(
285 work_dir
286 .run_jj(&["file", "list", "-r", "@-"])
287 .normalize_backslash(),
288 @r"
289 seed.txt
290 sub/file.txt
291 sub/ran.txt
292 [EOF]
293 "
294 );
295 }
296
297 #[test]
298 fn test_run_root_flag() {
299 let test_env = TestEnvironment::default();
300 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
301 // `fake-formatter --tee ran.txt` is a portable way to create an empty
302 // `ran.txt`, equivalent to `touch ran.txt` but available on all platforms.
303 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
304 assert!(fake_formatter.is_file());
305 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
306 let work_dir = test_env.work_dir("repo");
307 work_dir.write_file("sub/file.txt", "x");
308 work_dir.run_jj(&["commit", "-m", "with-sub"]).success();
309
310 // Invoke `jj run` from inside sub/, but pass `--root` so the command
311 // executes from the workspace root and `ran.txt` lands at the top level.
312 let sub_dir = work_dir.dir("sub");
313 sub_dir
314 .run_jj(&[
315 "run",
316 "--root",
317 "-r",
318 "@-",
319 "--",
320 &fake_formatter_path,
321 "--tee",
322 "ran.txt",
323 ])
324 .success();
325
326 insta::assert_snapshot!(
327 work_dir
328 .run_jj(&["file", "list", "-r", "@-"])
329 .normalize_backslash(),
330 @r"
331 ran.txt
332 sub/file.txt
333 [EOF]
334 "
335 );
336 }
337
338 #[test]
339 fn test_run_uses_revsets_run_as_default() {
340 let test_env = TestEnvironment::default();
341 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
342 // `fake-formatter --tee ran.txt` is a portable way to create an empty
343 // `ran.txt`, equivalent to `touch ran.txt` but available on all platforms.
344 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
345 assert!(fake_formatter.is_file());
346 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
347 let work_dir = test_env.work_dir("repo");
348
349 // Two sibling commits, `foo` and `bar`.
350 work_dir.write_file("file", "foo");
351 work_dir
352 .run_jj(["bookmark", "create", "-r@", "foo"])
353 .success();
354 work_dir.run_jj(["new", "root()"]).success();
355 work_dir.write_file("file", "bar");
356 work_dir
357 .run_jj(["bookmark", "create", "-r@", "bar"])
358 .success();
359 work_dir.run_jj(["edit", "foo"]).success();
360
361 test_env.add_config(r#"revsets.run = "bar""#);
362
363 // Running `jj run` with `revsets.run=bar` should only modify bar
364 work_dir
365 .run_jj([
366 "--config=revsets.run=\"bar\"",
367 "run",
368 "--",
369 &fake_formatter_path,
370 "--tee",
371 "ran.txt",
372 ])
373 .success();
374
375 insta::assert_snapshot!(
376 work_dir.run_jj(["file", "list", "-r", "foo"]),
377 @r"
378 file
379 [EOF]
380 "
381 );
382 insta::assert_snapshot!(
383 work_dir.run_jj(["file", "list", "-r", "bar"]),
384 @r"
385 file
386 ran.txt
387 [EOF]
388 "
389 );
390
391 // Run again but now with foo in the config
392 work_dir.run_jj(["undo"]).success();
393 work_dir
394 .run_jj([
395 "--config=revsets.run=\"foo\"",
396 "run",
397 "--",
398 &fake_formatter_path,
399 "--tee",
400 "ran.txt",
401 ])
402 .success();
403
404 insta::assert_snapshot!(
405 work_dir.run_jj(["file", "list", "-r", "foo"]),
406 @r"
407 file
408 ran.txt
409 [EOF]
410 "
411 );
412 insta::assert_snapshot!(
413 work_dir.run_jj(["file", "list", "-r", "bar"]),
414 @r"
415 file
416 [EOF]
417 "
418 );
419 }
420
421 #[test]
422 fn test_run_failure_rewrites_nothing() {
423 let test_env = TestEnvironment::default();
424 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
425 let work_dir = test_env.work_dir("repo");
426 work_dir.write_file("A.txt", "A");
427 work_dir.run_jj(&["commit", "-m", "A"]).success();
428 work_dir.write_file("b.txt", "b");
429 work_dir.run_jj(&["commit", "-m", "B"]).success();
430 let log_before = get_log_output(&work_dir);
431 insta::assert_snapshot!(log_before, @r"
432 @ kkmpptxzrspxrzommnulwmwkkqwworpl
433 ○ rlvkpnrzqnoowoytxnquwvuryrwnrmlpB
434 │
435 ○ qpvuntsmwlqtpsluzzsnyyzlmlwvmlnuA
436 │
437 ◆ zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
438 [EOF]
439 ");
440
441 // Fail on commit B; succeed (modify the tree) on every other commit. If
442 // any subprocess fails, `jj run` must roll back: no commit gets rewritten,
443 // even the ones whose commands ran to completion before B's failure
444 // propagated.
445 let cmd = "if [ \"$JJ_CHANGE_ID\" = 'rlvkpnrzqnoowoytxnquwvuryrwnrmlp' ]; then exit 1; fi; \
446 touch ran.txt";
447 let output = work_dir.run_jj(&["run", "-r", "..@", "--", "sh", "-c", cmd]);
448 assert!(!output.status.success(), "expected `jj run` to fail");
449
450 // Log is unchanged: same change_ids, same shape, no descendants of B got
451 // rebased onto a new commit.
452 assert_eq!(get_log_output(&work_dir), log_before);
453 }
454
455 #[test]
456 fn test_run_ignore_errors_rewrites_successes() {
457 let test_env = TestEnvironment::default();
458 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
459 let work_dir = test_env.work_dir("repo");
460 create_commit_with_files(&work_dir, "a", &[], &[("a.txt", "a")]);
461 create_commit_with_files(&work_dir, "b", &["a"], &[("b.txt", "b")]);
462
463 // Fail on A; succeed on B. A should be untouched, B should be rewritten.
464 let jj_args: &[&str] = if cfg!(windows) {
465 &[
466 "run",
467 "--ignore-errors",
468 "-r",
469 "..b",
470 "--",
471 "cmd",
472 "/c",
473 "type nul >ran.txt & (if not exist b.txt exit 1)",
474 ]
475 } else {
476 &[
477 "run",
478 "--ignore-errors",
479 "-r",
480 "..b",
481 "--",
482 "sh",
483 "-c",
484 "touch ran.txt; if [ ! -f b.txt ]; then exit 1; fi",
485 ]
486 };
487 let output = work_dir.run_jj(jj_args);
488 assert_snapshot!(
489 output.success(), @r"
490 ------- stderr -------
491 Rewrote 1 commits.
492 Working copy (@) now at: zsuskuln 8dcecfe7 b | b
493 Parent commit (@-) : rlvkpnrz b42c1b2c a | a
494 Added 1 files, modified 0 files, removed 0 files
495 [EOF]
496 "
497 );
498 insta::assert_snapshot!(
499 work_dir.run_jj(&["file", "list", "-r=a"]),
500 @r"
501 a.txt
502 [EOF]
503 "
504 );
505 insta::assert_snapshot!(
506 work_dir.run_jj(&["file", "list", "-r=b"]),
507 @r"
508 a.txt
509 b.txt
510 ran.txt
511 [EOF]
512 "
513 );
514 }
515
516 #[test]
517 fn test_run_ignore_errors_all_fail() {
518 let test_env = TestEnvironment::default();
519 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
520 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
521 assert!(fake_formatter.is_file());
522 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
523 let work_dir = test_env.work_dir("repo");
524 work_dir.write_file("A.txt", "A");
525 work_dir.run_jj(&["commit", "-m", "A"]).success();
526
527 let log_before = get_log_output(&work_dir);
528
529 let output = work_dir.run_jj(&[
530 "run",
531 "--ignore-errors",
532 "-r",
533 "@-",
534 "--",
535 &fake_formatter_path,
536 "--fail",
537 ]);
538 assert_snapshot!(
539 output.success(), @r"
540 ------- stderr -------
541 Nothing changed.
542 [EOF]
543 "
544 );
545
546 // Log is unchanged: no commits were rewritten.
547 assert_eq!(get_log_output(&work_dir), log_before);
548 }
549
550 /// `jj run --passthrough` must fail fast: once a command exits non-zero it
551 /// should stop, not keep running the command against every remaining revision.
552 /// `--passthrough` forces a single job, so the revisions run sequentially and
553 /// the output is deterministic.
554 #[test]
555 fn test_run_passthrough_stops_after_first_failure() {
556 let mut test_env = TestEnvironment::default();
557 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
558 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
559 assert!(fake_formatter.is_file());
560 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
561 test_env.add_paths_to_normalize(fake_formatter.clone(), "$FAKE_FORMATTER_PATH");
562 let work_dir = test_env.work_dir("repo");
563 work_dir.write_file("a.txt", "a");
564 work_dir.run_jj(&["commit", "-m", "A"]).success();
565 work_dir.write_file("b.txt", "b");
566 work_dir.run_jj(&["commit", "-m", "B"]).success();
567 work_dir.write_file("c.txt", "c");
568 work_dir.run_jj(&["commit", "-m", "C"]).success();
569
570 // Each invocation prints "x" (no newline) and exits non-zero. With
571 // `--passthrough` the command's stdout is streamed straight through, so the
572 // number of "x"es tells us how many revisions the command ran against.
573 // `jj run` must stop after the first failure, printing "x" exactly once
574 // rather than once per revision in `..@`.
575 let output = work_dir.run_jj(&[
576 "run",
577 "--passthrough",
578 "-r",
579 "..@",
580 "--",
581 &fake_formatter_path,
582 "--stdout",
583 "x",
584 "--fail",
585 ]);
586 insta::with_settings!({
587 filters => [
588 ("exit code", "exit status"), // Windows
589 ],
590 }, {
591 insta::assert_snapshot!(output, @r"
592 x[EOF]
593 ------- stderr -------
594 Error: the command '$FAKE_FORMATTER_PATH --stdout x --fail' failed with exit status: 1
595 Hint: Failed revision: qpvuntsm ffa1359e A
596 [EOF]
597 [exit status: 1]
598 ");
599 });
600
601 // With `--ignore-errors` the command runs against every revision in `..@`
602 // (A, B, C and the empty working-copy commit), so "x" is printed once per
603 // commit.
604 let output = work_dir.run_jj(&[
605 "run",
606 "--passthrough",
607 "--ignore-errors",
608 "-r",
609 "..@",
610 "--",
611 &fake_formatter_path,
612 "--stdout",
613 "x",
614 "--fail",
615 ]);
616 insta::assert_snapshot!(output.success(), @r"
617 xxxx[EOF]
618 ------- stderr -------
619 Nothing changed.
620 [EOF]
621 ");
622 }
623
624 #[test]
625 fn test_run_stops_after_first_failure() {
626 let test_env = TestEnvironment::default();
627 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
628 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
629 assert!(fake_formatter.is_file());
630 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
631 let work_dir = test_env.work_dir("repo");
632 work_dir.write_file("a.txt", "a");
633 work_dir.run_jj(&["commit", "-m", "A"]).success();
634 work_dir.write_file("b.txt", "b");
635 work_dir.run_jj(&["commit", "-m", "B"]).success();
636 work_dir.write_file("c.txt", "c");
637 work_dir.run_jj(&["commit", "-m", "C"]).success();
638
639 // The counter lives outside every ephemeral working copy, so all invocations
640 // append to the same file and its byte length is the execution count.
641 let counter = test_env.env_root().join("run-count.txt");
642 let counter_arg = counter.to_str().unwrap();
643
644 // Default (no `--ignore-errors`): the command fails on the first revision,
645 // so `jj run` must not touch the remaining revisions in `..@`.
646 let output = work_dir.run_jj(&[
647 "run",
648 "--jobs",
649 "1",
650 "-r",
651 "..@",
652 "--",
653 &fake_formatter_path,
654 "--stdout",
655 "x",
656 "--tee",
657 counter_arg,
658 "--fail",
659 ]);
660 assert!(!output.status.success(), "expected `jj run` to fail");
661 assert_eq!(
662 fs::read(&counter).unwrap().len(),
663 1,
664 "command should have run against exactly one revision before stopping"
665 );
666
667 // Verify with `--ignore-errors` the same command runs against every
668 // revision in `..@` (A, B, C and the empty working-copy commit)
669 let counter_all = test_env.env_root().join("run-count-all.txt");
670 let counter_all_arg = counter_all.to_str().unwrap();
671 let output = work_dir.run_jj(&[
672 "run",
673 "--ignore-errors",
674 "--jobs",
675 "1",
676 "-r",
677 "..@",
678 "--",
679 &fake_formatter_path,
680 "--stdout",
681 "x",
682 "--tee",
683 counter_all_arg,
684 "--fail",
685 ]);
686 assert!(
687 output.status.success(),
688 "expected `jj run --ignore-errors` to succeed"
689 );
690 assert_eq!(
691 fs::read(&counter_all).unwrap().len(),
692 4,
693 "with --ignore-errors the command should run against all four revisions"
694 );
695 }
696
697 #[test]
698 fn test_run_recovers_after_failure() {
699 let test_env = TestEnvironment::default();
700 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
701 // `fake-formatter --fail` exits non-zero (like `false`) and
702 // `fake-formatter --tee ran.txt` creates an empty `ran.txt` (like `touch`);
703 // both are portable across platforms.
704 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
705 assert!(fake_formatter.is_file());
706 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
707 let work_dir = test_env.work_dir("repo");
708 work_dir.write_file("A.txt", "A");
709 work_dir.run_jj(&["commit", "-m", "A"]).success();
710 work_dir.write_file("b.txt", "b");
711 work_dir.run_jj(&["commit", "-m", "B"]).success();
712
713 // First run fails; snapshot+persist still runs, leaving each slot with a
714 // valid tree_state on disk.
715 let first = work_dir.run_jj(&["run", "-r", "..@", "--", &fake_formatter_path, "--fail"]);
716 assert!(!first.status.success(), "expected first `jj run` to fail");
717
718 // A second run with a working command must succeed: the persisted
719 // tree_state lets each slot be reused without a wipe-and-reinit.
720 work_dir
721 .run_jj(&[
722 "run",
723 "-r",
724 "..@",
725 "--",
726 &fake_formatter_path,
727 "--tee",
728 "ran.txt",
729 ])
730 .success();
731
732 // Both commits in `..@` now carry `ran.txt`.
733 insta::assert_snapshot!(
734 work_dir.run_jj(&["file", "list", "-r", "@-"]),
735 @r"
736 A.txt
737 b.txt
738 ran.txt
739 [EOF]
740 "
741 );
742 insta::assert_snapshot!(
743 work_dir.run_jj(&["file", "list", "-r", "@--"]),
744 @r"
745 A.txt
746 ran.txt
747 [EOF]
748 "
749 );
750 }
751
752 #[test]
753 fn test_run_shell_command() {
754 // The new positional-args interface means users have to invoke a shell
755 // explicitly to use shell features. This verifies that path works
756 // end-to-end: each per-commit subprocess sees its `JJ_COMMIT_ID` and the
757 // shell echoes it to stdout.
758 let test_env = TestEnvironment::default();
759 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
760 let work_dir = test_env.work_dir("repo");
761 work_dir.write_file("A.txt", "A");
762 work_dir.run_jj(&["commit", "-m", "A"]).success();
763 work_dir.write_file("b.txt", "b");
764 work_dir.run_jj(&["commit", "-m", "B"]).success();
765 work_dir.write_file("c.txt", "test to replace");
766 work_dir.run_jj(&["commit", "-m", "C"]).success();
767
768 // Show the commit_ids so the reader can match them against the values
769 // the snapshot below was captured with.
770 let log_template = r#"change_id ++ " " ++ commit_id ++ " " ++ description ++ "\n""#;
771 insta::assert_snapshot!(
772 work_dir.run_jj(&["log", "-T", log_template, "-r", "..@"]),
773 @r"
774 @ zsuskulnrvyrovkzqrwmxqlsskqntxvp 8d0cb96bac2cfefd56a8691b9301ef44cc94a368
775 ○ kkmpptxzrspxrzommnulwmwkkqwworpl 3406218c99ce8076f3a28434ebda109cbd84de9e C
776 │
777 ○ rlvkpnrzqnoowoytxnquwvuryrwnrmlp 9453b0f03bbda20fa849b10eb051d1e3eed1ec5d B
778 │
779 ○ qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu 26d8ff9bba4faa4da6735ced959c57280e49afa7 A
780 │
781 ~
782 [EOF]
783 "
784 );
785
786 let jj_args: &[&str] = if cfg!(windows) {
787 &["run", "-r", "..@", "--", "cmd", "/c", "echo %JJ_COMMIT_ID%"]
788 } else {
789 &[
790 "run",
791 "-r",
792 "..@",
793 "--",
794 "sh",
795 "-c",
796 r#"echo "$JJ_COMMIT_ID""#,
797 ]
798 };
799 let output = work_dir.run_jj(jj_args).success();
800
801 // Parallel jobs finish in non-deterministic order, so sort before
802 // asserting.
803 let mut lines: Vec<&str> = output.stdout.raw().lines().collect();
804 lines.sort_unstable();
805 let sorted_stdout = lines.join("\n");
806 insta::assert_snapshot!(sorted_stdout, @r"
807 26d8ff9bba4faa4da6735ced959c57280e49afa7
808 3406218c99ce8076f3a28434ebda109cbd84de9e
809 8d0cb96bac2cfefd56a8691b9301ef44cc94a368
810 9453b0f03bbda20fa849b10eb051d1e3eed1ec5d
811 ");
812 }
813
814 #[test]
815 fn test_run_sets_workspace_root_env_var() {
816 let test_env = TestEnvironment::default();
817 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
818 let work_dir = test_env.work_dir("repo");
819
820 work_dir.write_file("seed.txt", "seed");
821 work_dir.run_jj(&["commit", "-m", "seed"]).success();
822
823 // Each subprocess writes $JJ_WORKSPACE_ROOT into a file so we can assert
824 // it equals the actual workspace root (not the per-commit working copy).
825 let jj_args: &[&str] = if cfg!(windows) {
826 &[
827 "run",
828 "-r",
829 "@-",
830 "--",
831 "cmd",
832 "/c",
833 "echo %JJ_WORKSPACE_ROOT%>workspace_root.txt",
834 ]
835 } else {
836 &[
837 "run",
838 "-r",
839 "@-",
840 "--",
841 "sh",
842 "-c",
843 "echo $JJ_WORKSPACE_ROOT > workspace_root.txt",
844 ]
845 };
846 work_dir.run_jj(jj_args).success();
847
848 // Trim trailing whitespace per line and normalize CRLF to LF so the
849 // snapshot is identical on Windows and Unix.
850 let normalize_whitespace = |s: String| {
851 s.replace("\r\n", "\n")
852 .lines()
853 .map(|line| line.trim_end().to_string())
854 .collect::<Vec<_>>()
855 .join("\n")
856 + "\n"
857 };
858 // $TEST_ENV is the normalized placeholder for the test environment's temp
859 // root directory. JJ_WORKSPACE_ROOT should point to the slot working copy
860 // under .jj/run/default/1/working_copy, not to the original workspace.
861 insta::assert_snapshot!(
862 work_dir
863 .run_jj(&["file", "show", "-r", "@-", "workspace_root.txt"])
864 .normalize_stdout_with(normalize_whitespace),
865 @r"
866 $TEST_ENV/repo/.jj/run/default/1/working_copy
867 [EOF]
868 "
869 );
870 }
871
872 /// After a failed `jj run`, the pool slot must be reusable: ignored files
873 /// (build artifacts) survive and non-ignored stray files are cleaned up.
874 #[test]
875 fn test_run_pool_reuses_slot_after_failure() {
876 let test_env = TestEnvironment::default();
877 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
878 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
879 assert!(fake_formatter.is_file());
880 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
881 let work_dir = test_env.work_dir("repo");
882
883 work_dir.write_file(".gitignore", "cache/\n");
884 work_dir.write_file("seed.txt", "seed");
885 work_dir.run_jj(&["commit", "-m", "seed"]).success();
886
887 // Failing command writes an ignored artifact and a non-ignored stray file.
888 let first = work_dir.run_jj(&[
889 "run",
890 "--config",
891 "run.jobs=1",
892 "-r",
893 "@-",
894 "--",
895 "sh",
896 "-c",
897 &format!(
898 "mkdir -p cache && echo artifact > cache/artifact.txt && touch stray.txt && \
899 {fake_formatter_path} --fail"
900 ),
901 ]);
902 assert!(!first.status.success(), "expected first run to fail");
903
904 let slot_state = work_dir.root().join(".jj/run/default/1/state/tree_state");
905 assert!(
906 slot_state.exists(),
907 "tree_state must be persisted even after failure"
908 );
909
910 // Succeeding second run: ignored artifact must still be present on disk;
911 // the non-ignored stray.txt must have been cleaned up (not leaked into
912 // the new commit).
913 work_dir
914 .run_jj(&[
915 "run",
916 "--config",
917 "run.jobs=1",
918 "-r",
919 "@-",
920 "--",
921 "sh",
922 "-c",
923 "if [ -f cache/artifact.txt ]; then cp cache/artifact.txt result.txt; else echo \
924 MISSING > result.txt; fi",
925 ])
926 .success();
927
928 assert_snapshot!(
929 work_dir.run_jj(&["file", "show", "-r", "@-", "result.txt"]),@r"
930 artifact
931 [EOF]
932 "
933 );
934 assert_snapshot!(
935 work_dir
936 .run_jj(&["file", "list", "-r", "@-"]),
937 @r"
938 .gitignore
939 result.txt
940 seed.txt
941 [EOF]
942 "
943 );
944 }
945
946 #[test]
947 fn test_run_pool_persists_between_runs() {
948 let test_env = TestEnvironment::default();
949 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
950 let work_dir = test_env.work_dir("repo");
951 work_dir.write_file("seed.txt", "seed");
952 work_dir.run_jj(&["commit", "-m", "seed"]).success();
953
954 work_dir
955 .run_jj(&[
956 "run",
957 "--config",
958 "run.jobs=1",
959 "-r",
960 "@-",
961 "--",
962 "touch",
963 "ran.txt",
964 ])
965 .success();
966
967 // The pool slot directory survives the run.
968 let pool_slot = work_dir.root().join(".jj/run/default/1");
969 assert!(
970 pool_slot.exists(),
971 "expected pool slot 1 to persist between runs at {pool_slot:?}",
972 );
973 assert!(pool_slot.join("working_copy").exists());
974 assert!(pool_slot.join("state").exists());
975
976 // A second run reuses the existing slot rather than recreating it from
977 // scratch.
978 work_dir
979 .run_jj(&[
980 "run",
981 "--config",
982 "run.jobs=1",
983 "-r",
984 "@-",
985 "--",
986 "touch",
987 "ran2.txt",
988 ])
989 .success();
990 assert!(pool_slot.join("working_copy").exists());
991 }
992
993 #[test]
994 fn test_run_pool_size_from_config() {
995 let test_env = TestEnvironment::default();
996 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
997 let work_dir = test_env.work_dir("repo");
998 work_dir.write_file("a.txt", "a");
999 work_dir.run_jj(&["commit", "-m", "A"]).success();
1000 work_dir.write_file("b.txt", "b");
1001 work_dir.run_jj(&["commit", "-m", "B"]).success();
1002 work_dir.write_file("c.txt", "c");
1003 work_dir.run_jj(&["commit", "-m", "C"]).success();
1004
1005 // `run.jobs = 1` forces all three commits to share a single slot
1006 // and be processed sequentially.
1007 work_dir
1008 .run_jj(&[
1009 "run",
1010 "--config",
1011 "run.jobs=2",
1012 "-r",
1013 "..@",
1014 "--",
1015 "sh",
1016 "-c",
1017 "touch ran-$JJ_CHANGE_ID.txt",
1018 ])
1019 .success();
1020
1021 let pool_dir = work_dir.root().join(".jj/run/default");
1022 assert!(pool_dir.join("1").exists(), "pool/1 should exist");
1023 assert!(pool_dir.join("2").exists(), "pool/2 should exist");
1024 assert!(
1025 !pool_dir.join("3").exists(),
1026 "pool/3 should NOT exist with size=2",
1027 );
1028
1029 // All three commits picked up `ran.txt`.
1030 assert_snapshot!(work_dir.run_jj(&["file", "list", "-r", "@---"]), @r"
1031 a.txt
1032 ran-qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu.txt
1033 [EOF]
1034 ");
1035 assert_snapshot!(work_dir.run_jj(&["file", "list", "-r", "@--"]), @r"
1036 a.txt
1037 b.txt
1038 ran-qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu.txt
1039 ran-rlvkpnrzqnoowoytxnquwvuryrwnrmlp.txt
1040 [EOF]
1041 ");
1042 assert_snapshot!(work_dir.run_jj(&["file", "list", "-r", "@-"]), @r"
1043 a.txt
1044 b.txt
1045 c.txt
1046 ran-kkmpptxzrspxrzommnulwmwkkqwworpl.txt
1047 ran-qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu.txt
1048 ran-rlvkpnrzqnoowoytxnquwvuryrwnrmlp.txt
1049 [EOF]
1050 ");
1051 }
1052
1053 /// `--jobs N` controls pool size when `run.jobs` is not set.
1054 #[test]
1055 fn test_run_pool_size_from_jobs_flag() {
1056 let test_env = TestEnvironment::default();
1057 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1058 let work_dir = test_env.work_dir("repo");
1059 work_dir.write_file("a.txt", "a");
1060 work_dir.run_jj(&["commit", "-m", "A"]).success();
1061 work_dir.write_file("b.txt", "b");
1062 work_dir.run_jj(&["commit", "-m", "B"]).success();
1063 work_dir.write_file("c.txt", "c");
1064 work_dir.run_jj(&["commit", "-m", "C"]).success();
1065
1066 // `--jobs 2` with pool: expect exactly 2 slots, no more.
1067 work_dir
1068 .run_jj(&["run", "--jobs", "2", "-r", "..@", "--", "touch", "ran.txt"])
1069 .success();
1070
1071 let pool_dir = work_dir.root().join(".jj/run/default");
1072 assert!(pool_dir.join("1").exists(), "pool/1 should exist");
1073 assert!(pool_dir.join("2").exists(), "pool/2 should exist");
1074 assert!(
1075 !pool_dir.join("3").exists(),
1076 "pool/3 must NOT exist with --jobs 2",
1077 );
1078 }
1079
1080 #[test]
1081 fn test_run_pool_preserves_untracked_artifacts() {
1082 let test_env = TestEnvironment::default();
1083 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1084 let work_dir = test_env.work_dir("repo");
1085
1086 // The .gitignore keeps `cache/` out of the snapshot's tracking set, so
1087 // files there persist on disk between jobs without being committed.
1088 work_dir.write_file(".gitignore", "cache/\n");
1089 work_dir.write_file("seed.txt", "seed");
1090 work_dir.run_jj(&["commit", "-m", "seed"]).success();
1091
1092 // Run 1: drop a marker into the gitignored cache directory.
1093 work_dir
1094 .run_jj(&[
1095 "run",
1096 "--config",
1097 "run.jobs=1",
1098 "-r",
1099 "@-",
1100 "--",
1101 "sh",
1102 "-c",
1103 "mkdir -p cache && echo run1 > cache/marker && touch ran1.txt",
1104 ])
1105 .success();
1106
1107 // Run 2: assert the marker is still there, write its contents into a
1108 // tracked file so we can verify from outside.
1109 work_dir
1110 .run_jj(&[
1111 "run",
1112 "--config",
1113 "run.jobs=1",
1114 "-r",
1115 "@-",
1116 "--",
1117 "sh",
1118 "-c",
1119 "if [ -f cache/marker ]; then cp cache/marker result.txt; else echo MISSING > \
1120 result.txt; fi",
1121 ])
1122 .success();
1123
1124 assert_snapshot!(
1125 work_dir
1126 .run_jj(&["file", "show", "-r", "@-", "result.txt"])
1127 .success()
1128 .stdout,
1129 @r"
1130 run1
1131 [EOF]
1132 ",
1133 );
1134 }
1135
1136 /// Pool correctly removes a file that is present in one commit's tree but
1137 /// absent in the next commit processed by the same slot.
1138 #[test]
1139 fn test_run_pool_removes_file_absent_in_next_commit() {
1140 let test_env = TestEnvironment::default();
1141 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1142 let work_dir = test_env.work_dir("repo");
1143
1144 // Commit A has only_in_a.txt.
1145 work_dir.write_file("only_in_a.txt", "a");
1146 work_dir.run_jj(&["commit", "-m", "A"]).success();
1147 // After commit -m A the WC inherits A's files. Delete the file so that
1148 // commit B's tree is empty (no only_in_a.txt).
1149 std::fs::remove_file(work_dir.root().join("only_in_a.txt")).unwrap();
1150 work_dir.run_jj(&["commit", "-m", "B"]).success();
1151 // Stack: root → A (only_in_a.txt) → B ({}) → @
1152
1153 // Pool size 1 forces both commits through the same slot sequentially.
1154 // @-- = A, @- = B (WC is above B).
1155 work_dir
1156 .run_jj(&[
1157 "run",
1158 "--config",
1159 "run.jobs=1",
1160 "-r",
1161 "@--::@-",
1162 "--",
1163 "touch",
1164 "ran.txt",
1165 ])
1166 .success();
1167
1168 // A's rewrite (@--) keeps only_in_a.txt (and gains ran.txt).
1169 assert_snapshot!(
1170 work_dir
1171 .run_jj(&["file", "list", "-r", "@--"])
1172 .success()
1173 .stdout,
1174 @r"
1175 only_in_a.txt
1176 ran.txt
1177 [EOF]
1178 ",
1179 );
1180
1181 // B's rewrite (@-) must NOT have only_in_a.txt leaked from A's slot.
1182 assert_snapshot!(
1183 work_dir
1184 .run_jj(&["file", "list", "-r", "@-"])
1185 .success()
1186 .stdout,
1187 @r"
1188 ran.txt
1189 [EOF]
1190 ",
1191 );
1192 }
1193
1194 /// When a pool slot is reused, files left on disk that the previous commit's
1195 /// .gitignore excluded must be removed before the next commit runs. Otherwise
1196 /// the next commit (which may not ignore the same paths) would pick them up at
1197 /// snapshot time and incorrectly add them to its rewritten tree.
1198 #[test]
1199 fn test_run_pool_removes_previously_ignored_files() {
1200 let test_env = TestEnvironment::default();
1201 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1202 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
1203 assert!(fake_formatter.is_file());
1204 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
1205 let work_dir = test_env.work_dir("repo");
1206
1207 // Commit a: no .gitignore.
1208 work_dir.write_file("seed.txt", "seed");
1209 work_dir.write_file(".gitignore", "always_ignored.txt\n");
1210 work_dir
1211 .run_jj(&["bookmark", "create", "-r@", "a"])
1212 .success();
1213 work_dir.run_jj(&["commit", "-m", "a"]).success();
1214 // Commit b: adds a .gitignore that excludes `ignored.txt`.
1215 work_dir.write_file(".gitignore", "always_ignored.txt\nignored.txt\n");
1216 work_dir
1217 .run_jj(&["bookmark", "create", "-r@", "b"])
1218 .success();
1219 work_dir.run_jj(&["commit", "-m", "b"]).success();
1220
1221 let run_wc = work_dir.root().join(".jj/run/default/1/working_copy");
1222
1223 // Leave two ignored files in the run workspace
1224 work_dir
1225 .run_jj(&[
1226 "run",
1227 "-r=b",
1228 "--",
1229 &fake_formatter_path,
1230 "--tee=always_ignored.txt",
1231 "--tee=ignored.txt",
1232 ])
1233 .success();
1234 assert_snapshot!(
1235 work_dir.run_jj(&["file", "list", "-r", "b"]),
1236 @r"
1237 .gitignore
1238 seed.txt
1239 [EOF]
1240 "
1241 );
1242 assert!(fs::exists(run_wc.join("always_ignored.txt")).unwrap());
1243 assert!(fs::exists(run_wc.join("ignored.txt")).unwrap());
1244
1245 // Run against a version that only ignores always_ignored.txt, ensure that
1246 // ignored.txt is removed, not amended into 'a'.
1247 work_dir
1248 .run_jj(&[
1249 "run",
1250 "-r=a",
1251 "--",
1252 &fake_formatter_path,
1253 "--stdout",
1254 "done",
1255 ])
1256 .success();
1257 assert_snapshot!(
1258 work_dir.run_jj(&["file", "list", "-r", "a"]),
1259 @r"
1260 .gitignore
1261 seed.txt
1262 [EOF]
1263 "
1264 );
1265 // Remove files that are no longer ignored in the new revision, but don't
1266 // unnecessarily remove files that are still ignored
1267 assert!(fs::exists(run_wc.join("always_ignored.txt")).unwrap());
1268 assert!(!fs::exists(run_wc.join("ignored.txt")).unwrap());
1269
1270 // Passing --clean removes even always-ignored files
1271 work_dir
1272 .run_jj(&[
1273 "run",
1274 "-r=a",
1275 "--clean",
1276 "--",
1277 &fake_formatter_path,
1278 "--stdout",
1279 "done",
1280 ])
1281 .success();
1282 assert_snapshot!(
1283 work_dir.run_jj(&["file", "list", "-r", "a"]),
1284 @r"
1285 .gitignore
1286 seed.txt
1287 [EOF]
1288 "
1289 );
1290 assert!(!fs::exists(run_wc.join("always_ignored.txt")).unwrap());
1291 assert!(!fs::exists(run_wc.join("ignored.txt")).unwrap());
1292 }
1293
1294 /// A slot whose tree_state is absent (simulating a crash mid-job) should be
1295 /// wiped and reinitialized on the next acquisition, not produce garbage.
1296 #[test]
1297 fn test_run_pool_recovers_from_missing_tree_state() {
1298 let test_env = TestEnvironment::default();
1299 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1300 let work_dir = test_env.work_dir("repo");
1301
1302 work_dir.write_file("seed.txt", "seed");
1303 work_dir.run_jj(&["commit", "-m", "seed"]).success();
1304
1305 // Prime the slot so working_copy/ exists.
1306 work_dir
1307 .run_jj(&["run", "--config", "run.jobs=1", "-r", "@-", "--", "true"])
1308 .success();
1309
1310 // Simulate a crash: plant a stale file in the slot and delete tree_state.
1311 let slot = work_dir.root().join(".jj/run/default/1");
1312 std::fs::write(slot.join("working_copy/stale.txt"), "crash leftovers").unwrap();
1313 std::fs::remove_file(slot.join("state/tree_state")).unwrap();
1314
1315 // A second run should wipe the stale state and produce a clean rewrite.
1316 work_dir
1317 .run_jj(&[
1318 "run",
1319 "--config",
1320 "run.jobs=1",
1321 "-r",
1322 "@-",
1323 "--",
1324 "touch",
1325 "ran.txt",
1326 ])
1327 .success();
1328
1329 assert_snapshot!(
1330 work_dir.run_jj(&["file", "list", "-r", "@-"]).success().stdout,
1331 @r"
1332 ran.txt
1333 seed.txt
1334 [EOF]
1335 ",
1336 );
1337 }
1338
1339 /// A failed command (non-zero exit) must not poison the pool slot for the
1340 /// next `jj run` invocation.
1341 #[test]
1342 fn test_run_pool_failed_command_does_not_poison_slot() {
1343 let test_env = TestEnvironment::default();
1344 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1345 let work_dir = test_env.work_dir("repo");
1346
1347 work_dir.write_file("seed.txt", "seed");
1348 work_dir.run_jj(&["commit", "-m", "seed"]).success();
1349
1350 // Run 1: command fails, but writes a file first.
1351 drop(work_dir.run_jj(&[
1352 "run",
1353 "--config",
1354 "run.jobs=1",
1355 "-r",
1356 "@-",
1357 "--",
1358 "sh",
1359 "-c",
1360 "touch poison.txt; exit 1",
1361 ]));
1362
1363 // Run 2: a clean command. poison.txt must not appear in the rewrite.
1364 work_dir
1365 .run_jj(&[
1366 "run",
1367 "--config",
1368 "run.jobs=1",
1369 "-r",
1370 "@-",
1371 "--",
1372 "touch",
1373 "ran.txt",
1374 ])
1375 .success();
1376
1377 assert_snapshot!(
1378 work_dir.run_jj(&["file", "list", "-r", "@-"]).success().stdout,
1379 @r"
1380 ran.txt
1381 seed.txt
1382 [EOF]
1383 ",
1384 );
1385 }
1386
1387 #[test]
1388 fn test_run_restore_descendants_preserves_content() {
1389 let mut test_env = TestEnvironment::default();
1390 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1391 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
1392 assert!(fake_formatter.is_file());
1393 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
1394 test_env.add_paths_to_normalize(fake_formatter.clone(), "$FAKE_FORMATTER_PATH");
1395 let work_dir = test_env.work_dir("repo");
1396
1397 create_commit_with_files(&work_dir, "a", &[], &[("file", "a\n")]);
1398 create_commit_with_files(&work_dir, "b", &["a"], &[("file", "b\n")]);
1399 create_commit_with_files(&work_dir, "c", &["b"], &[("file", "c\n")]);
1400
1401 let command = if cfg!(windows) {
1402 format!("{fake_formatter_path} --tee ran-%JJ_CHANGE_ID%.txt")
1403 } else {
1404 format!("{fake_formatter_path} --tee ran-$JJ_CHANGE_ID.txt")
1405 };
1406 let args: &[&str] = if cfg!(windows) {
1407 &[
1408 "run",
1409 "-r",
1410 "a::b",
1411 "--restore-descendants",
1412 "--",
1413 "cmd",
1414 "/c",
1415 command.as_str(),
1416 ]
1417 } else {
1418 &[
1419 "run",
1420 "-r",
1421 "a::b",
1422 "--restore-descendants",
1423 "--",
1424 "sh",
1425 "-c",
1426 command.as_str(),
1427 ]
1428 };
1429 let output = work_dir.run_jj(args).success();
1430 assert_snapshot!(output.stderr, @r"
1431 Rewrote 2 commits.
1432 Rebased 1 descendant commits (while preserving their content).
1433 Working copy (@) now at: royxmykx a741a7d3 c | c
1434 Parent commit (@-) : zsuskuln 43c5a714 b | b
1435 [EOF]
1436 ");
1437
1438 assert_snapshot!(
1439 work_dir
1440 .run_jj(&["file", "list", "-r", "a"])
1441 .success()
1442 .stdout,
1443 @r"
1444 file
1445 ran-rlvkpnrzqnoowoytxnquwvuryrwnrmlp.txt
1446 [EOF]
1447 "
1448 );
1449 assert_snapshot!(
1450 work_dir
1451 .run_jj(&["file", "list", "-r", "b"])
1452 .success()
1453 .stdout,
1454 @r"
1455 file
1456 ran-zsuskulnrvyrovkzqrwmxqlsskqntxvp.txt
1457 [EOF]
1458 "
1459 );
1460 assert_snapshot!(
1461 work_dir
1462 .run_jj(&["file", "list", "-r", "c"])
1463 .success()
1464 .stdout,
1465 @r"
1466 file
1467 [EOF]
1468 "
1469 );
1470
1471 assert_snapshot!(
1472 work_dir.run_jj(&["diff", "--from=a", "--to=b", "--git"]).success().stdout,
1473 @r"
1474 diff --git a/file b/file
1475 index 7898192261..6178079822 100644
1476 --- a/file
1477 +++ b/file
1478 @@ -1,1 +1,1 @@
1479 -a
1480 +b
1481 diff --git a/ran-rlvkpnrzqnoowoytxnquwvuryrwnrmlp.txt b/ran-rlvkpnrzqnoowoytxnquwvuryrwnrmlp.txt
1482 deleted file mode 100644
1483 index e69de29bb2..0000000000
1484 diff --git a/ran-zsuskulnrvyrovkzqrwmxqlsskqntxvp.txt b/ran-zsuskulnrvyrovkzqrwmxqlsskqntxvp.txt
1485 new file mode 100644
1486 index 0000000000..e69de29bb2
1487 [EOF]
1488 "
1489 );
1490 assert_snapshot!(
1491 work_dir.run_jj(&["diff", "--from=b", "--to=c", "--git"]).success().stdout,
1492 @r"
1493 diff --git a/file b/file
1494 index 6178079822..f2ad6c76f0 100644
1495 --- a/file
1496 +++ b/file
1497 @@ -1,1 +1,1 @@
1498 -b
1499 +c
1500 diff --git a/ran-zsuskulnrvyrovkzqrwmxqlsskqntxvp.txt b/ran-zsuskulnrvyrovkzqrwmxqlsskqntxvp.txt
1501 deleted file mode 100644
1502 index e69de29bb2..0000000000
1503 [EOF]
1504 "
1505 );
1506 }
1507
1508 #[test]
1509 fn test_run_failure_shows_output() {
1510 let test_env = TestEnvironment::default();
1511 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1512 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
1513 assert!(fake_formatter.is_file());
1514 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
1515 let work_dir = test_env.work_dir("repo");
1516 work_dir.write_file("A.txt", "A");
1517 work_dir.run_jj(&["commit", "-m", "A"]).success();
1518
1519 let output = work_dir.run_jj(&[
1520 "run",
1521 "-r",
1522 "@-",
1523 "--",
1524 &fake_formatter_path,
1525 "--stdout",
1526 "hello stdout\n",
1527 "--stderr",
1528 "hello stderr\n",
1529 "--fail",
1530 ]);
1531 assert!(!output.status.success());
1532 insta::with_settings!({
1533 filters => [
1534 ("exit code", "exit status"), // Windows
1535 ],
1536 }, {
1537 insta::assert_snapshot!(&output.normalize_stderr_with(|stderr| stderr.replace(&fake_formatter_path.clone(), "fake-formatter")), @"
1538 hello stdout
1539 [EOF]
1540 ------- stderr -------
1541 hello stderr
1542 Error: the command 'fake-formatter --stdout hello stdout
1543 --stderr hello stderr
1544 --fail' failed with exit status: 1
1545 Hint: Failed revision: qpvuntsm 26d8ff9b A
1546 [EOF]
1547 [exit status: 1]
1548 ");
1549 });
1550 }
1551
1552 /// Changes made to an ancestor commit must still propagate into descendant
1553 /// commits after rebasing.
1554 #[test]
1555 fn test_run_parallel_changes_propagate_to_descendants() {
1556 let test_env = TestEnvironment::default();
1557 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1558 let work_dir = test_env.work_dir("repo");
1559 work_dir.write_file("a.txt", "a");
1560 work_dir.run_jj(&["commit", "-m", "A"]).success();
1561 work_dir.write_file("b.txt", "b");
1562 work_dir.run_jj(&["commit", "-m", "B"]).success();
1563 work_dir.write_file("c.txt", "c");
1564 work_dir.run_jj(&["commit", "-m", "C"]).success();
1565
1566 // Add a unique file to each change
1567 work_dir
1568 .run_jj(&[
1569 "run",
1570 "--jobs=3",
1571 "-r=..@",
1572 "--",
1573 "sh",
1574 "-c",
1575 "touch newfile-$JJ_CHANGE_ID.txt",
1576 ])
1577 .success();
1578
1579 assert_snapshot!(work_dir.run_jj(&["file", "list", "-r=@---"]).success().stdout,@r"
1580 a.txt
1581 newfile-qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu.txt
1582 [EOF]
1583 ");
1584 assert_snapshot!(work_dir.run_jj(&["file", "list", "-r=@--"]).success().stdout,@r"
1585 a.txt
1586 b.txt
1587 newfile-qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu.txt
1588 newfile-rlvkpnrzqnoowoytxnquwvuryrwnrmlp.txt
1589 [EOF]
1590 ");
1591 assert_snapshot!(work_dir.run_jj(&["file", "list", "-r=@-"]).success().stdout,@r"
1592 a.txt
1593 b.txt
1594 c.txt
1595 newfile-kkmpptxzrspxrzommnulwmwkkqwworpl.txt
1596 newfile-qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu.txt
1597 newfile-rlvkpnrzqnoowoytxnquwvuryrwnrmlp.txt
1598 [EOF]
1599 ");
1600 }
1601
1602 #[test]
1603 fn test_run_ignore_changes_does_not_rewrite() {
1604 let mut test_env = TestEnvironment::default();
1605 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1606 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
1607 assert!(fake_formatter.is_file());
1608 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
1609 test_env.add_paths_to_normalize(fake_formatter.clone(), "$FAKE_FORMATTER_PATH");
1610 let work_dir = test_env.work_dir("repo");
1611
1612 crate::common::create_commit_with_files(&work_dir, "a", &[], &[("file.txt", "a\n")]);
1613 crate::common::create_commit_with_files(&work_dir, "b", &["a"], &[("file.txt", "b\n")]);
1614
1615 let log_before = get_log_output(&work_dir);
1616
1617 // --tee touched.txt writes a new file in each working copy. Without
1618 // --ignore-changes this would amend the commits; with it nothing is rewritten.
1619 let output = work_dir
1620 .run_jj(&[
1621 "run",
1622 "--ignore-changes",
1623 "--config",
1624 "run.jobs=1",
1625 "-r",
1626 "a",
1627 "--",
1628 &fake_formatter_path,
1629 "--tee",
1630 "touched.txt",
1631 ])
1632 .success();
1633 insta::assert_snapshot!(output.stderr, @r"
1634 Nothing changed.
1635 [EOF]
1636 ");
1637
1638 // Log must be identical to before.
1639 assert_eq!(get_log_output(&work_dir), log_before);
1640
1641 // touched.txt must not appear in commit a's tree.
1642 assert_snapshot!(
1643 work_dir
1644 .run_jj(&["file", "list", "-r", "a"])
1645 .success(),
1646 @r"
1647 file.txt
1648 [EOF]
1649 "
1650 );
1651 }
1652
1653 #[test]
1654 fn test_run_ignore_changes_does_not_leak_between_invocations() {
1655 // Mirrors test_run_pool_no_file_leak_between_invocations but run 1 uses
1656 // --ignore-changes. The pool slot's tree_state is still updated by the snapshot
1657 // during run 1, so run 2 correctly removes artifact.txt from the slot
1658 // before checking out commit1.
1659 let test_env = TestEnvironment::default();
1660 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1661 let work_dir = test_env.work_dir("repo");
1662
1663 work_dir.write_file("seed.txt", "seed");
1664 work_dir.run_jj(&["commit", "-m", "commit1"]).success();
1665 work_dir.run_jj(&["commit", "-m", "commit2"]).success();
1666
1667 // Run 1 (--ignore-changes): writes artifact.txt in the pool slot for commit2
1668 // but does not amend any commit. The slot's tree_state must record
1669 // artifact.txt so run 2 can remove it when checking out a different tree.
1670 let touch_cmd: &[&str] = if cfg!(windows) {
1671 &[
1672 "run",
1673 "--ignore-changes",
1674 "--config",
1675 "run.jobs=1",
1676 "-r",
1677 "@-",
1678 "--",
1679 "cmd",
1680 "/c",
1681 "echo. > artifact.txt",
1682 ]
1683 } else {
1684 &[
1685 "run",
1686 "--ignore-changes",
1687 "--config",
1688 "run.jobs=1",
1689 "-r",
1690 "@-",
1691 "--",
1692 "touch",
1693 "artifact.txt",
1694 ]
1695 };
1696 work_dir.run_jj(touch_cmd).success();
1697
1698 // Run 2: no-op on commit1 (@--) using the same pool slot that held
1699 // artifact.txt. The checkout diff should remove artifact.txt from the slot.
1700 work_dir
1701 .run_jj(&["run", "--config", "run.jobs=1", "-r", "@--", "--", "true"])
1702 .success();
1703
1704 let files = work_dir
1705 .run_jj(&["file", "list", "-r", "@--"])
1706 .success()
1707 .stdout
1708 .to_string();
1709 assert!(
1710 !files.contains("artifact.txt"),
1711 "artifact.txt must not leak into commit1 from --ignore-changes slot state; got:\n{files}",
1712 );
1713 assert!(
1714 files.contains("seed.txt"),
1715 "expected seed.txt in commit1; got:\n{files}",
1716 );
1717 }
1718
1719 #[test]
1720 fn test_run_ignore_changes_works_on_immutable() {
1721 let test_env = TestEnvironment::default();
1722 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1723 let work_dir = test_env.work_dir("repo");
1724
1725 // Set up commits and bookmark BEFORE configuring immutable_heads, so
1726 // setup commands don't trip over the missing revset.
1727 work_dir.write_file("file.txt", "content");
1728 work_dir.run_jj(&["commit", "-m", "the commit"]).success();
1729 work_dir
1730 .run_jj(&["bookmark", "create", "-r", "@-", "main"])
1731 .success();
1732 test_env.add_config(r#"revset-aliases."immutable_heads()" = "main""#);
1733
1734 // Without --ignore-changes, running on an immutable commit must fail.
1735 let err_output = work_dir.run_jj(&["run", "-r", "main", "--", "true"]);
1736 assert!(
1737 !err_output.status.success(),
1738 "expected failure on immutable commit"
1739 );
1740 let stderr = err_output.stderr.to_string();
1741 assert!(
1742 stderr.contains("immutable"),
1743 "expected 'immutable' in error; got:\n{stderr}",
1744 );
1745
1746 // With --ignore-changes the same commit is allowed.
1747 work_dir
1748 .run_jj(&["run", "--ignore-changes", "-r", "main", "--", "true"])
1749 .success();
1750 }
1751
1752 #[test]
1753 fn test_run_ignore_changes_conflicts_with_restore_descendants() {
1754 let test_env = TestEnvironment::default();
1755 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1756 let work_dir = test_env.work_dir("repo");
1757
1758 let output = work_dir.run_jj(&[
1759 "run",
1760 "--ignore-changes",
1761 "--restore-descendants",
1762 "--",
1763 "true",
1764 ]);
1765 assert!(!output.status.success());
1766 insta::assert_snapshot!(output.stderr, @r"
1767 error: the argument '--ignore-changes' cannot be used with '--restore-descendants'
1768
1769 Usage: jj run --ignore-changes <COMMAND> [ARGS]...
1770
1771 For more information, try '--help'.
1772 [EOF]
1773 ");
1774 }
1775
1776 fn get_log_output(work_dir: &TestWorkDir) -> String {
1777 work_dir
1778 .run_jj(&["log", "-T", r#"change_id ++ description ++ "\n""#])
1779 .success()
1780 .stdout
1781 .to_string()
1782 }
1783
1784 #[test]
1785 fn test_run_order() {
1786 let test_env = TestEnvironment::default();
1787 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1788 let work_dir = test_env.work_dir("repo");
1789 work_dir.write_file("file.txt", "a");
1790 work_dir.run_jj(&["commit", "-m", "A"]).success();
1791 work_dir.write_file("file.txt", "b");
1792 work_dir.run_jj(&["commit", "-m", "B"]).success();
1793 work_dir.write_file("file.txt", "c");
1794 work_dir.run_jj(&["commit", "-m", "C"]).success();
1795
1796 let jj_args: &[&str] = if cfg!(windows) {
1797 &["run", "-r=..@", "--", "cmd", "/c", "echo %JJ_CHANGE_ID%"]
1798 } else {
1799 &["run", "-r=..@", "--", "sh", "-c", "echo $JJ_CHANGE_ID"]
1800 };
1801 assert_snapshot!(work_dir.run_jj(jj_args).success().stdout,@"
1802 qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu
1803 rlvkpnrzqnoowoytxnquwvuryrwnrmlp
1804 kkmpptxzrspxrzommnulwmwkkqwworpl
1805 zsuskulnrvyrovkzqrwmxqlsskqntxvp
1806 [EOF]
1807 ");
1808 }
1809
1810 #[test]
1811 fn test_run_passthrough() {
1812 let test_env = TestEnvironment::default();
1813 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1814 let work_dir = test_env.work_dir("repo");
1815 work_dir.write_file("A.txt", "A");
1816 work_dir.run_jj(&["commit", "-m", "A"]).success();
1817 work_dir.write_file("b.txt", "b");
1818 work_dir.run_jj(&["commit", "-m", "B"]).success();
1819
1820 insta::assert_snapshot!(
1821 work_dir.run_jj(&["log", "-T", r#"change_id ++ " " ++ description ++ "\n""#, "-r", "..@"]),
1822 @r"
1823 @ kkmpptxzrspxrzommnulwmwkkqwworpl
1824 ○ rlvkpnrzqnoowoytxnquwvuryrwnrmlp B
1825 │
1826 ○ qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu A
1827 │
1828 ~
1829 [EOF]
1830 "
1831 );
1832
1833 // --passthrough passes the child's stdout/stderr directly through. The output
1834 // appears in `jj run`'s own stdout/stderr (captured by the test harness).
1835 let jj_args: &[&str] = if cfg!(windows) {
1836 &[
1837 "run",
1838 "--passthrough",
1839 "-r",
1840 "..@",
1841 "--",
1842 "cmd",
1843 "/c",
1844 "echo hello from passthrough",
1845 ]
1846 } else {
1847 &[
1848 "run",
1849 "--passthrough",
1850 "-r",
1851 "..@",
1852 "--",
1853 "echo",
1854 "hello from passthrough",
1855 ]
1856 };
1857 let output = work_dir.run_jj(jj_args).success();
1858 insta::assert_snapshot!(output.stdout, @"hello from passthrough
1859 hello from passthrough
1860 hello from passthrough
1861 [EOF]
1862 ");
1863 }
1864
1865 #[test]
1866 fn test_run_passthrough_failure_rewrites_nothing() {
1867 let test_env = TestEnvironment::default();
1868 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1869 let work_dir = test_env.work_dir("repo");
1870 work_dir.write_file("A.txt", "A");
1871 work_dir.run_jj(&["commit", "-m", "A"]).success();
1872 work_dir.write_file("b.txt", "b");
1873 work_dir.run_jj(&["commit", "-m", "B"]).success();
1874 let log_before = get_log_output(&work_dir);
1875 insta::assert_snapshot!(log_before, @r"
1876 @ kkmpptxzrspxrzommnulwmwkkqwworpl
1877 ○ rlvkpnrzqnoowoytxnquwvuryrwnrmlpB
1878 │
1879 ○ qpvuntsmwlqtpsluzzsnyyzlmlwvmlnuA
1880 │
1881 ◆ zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
1882 [EOF]
1883 ");
1884
1885 // A failing command run with --passthrough should not rewrite any commits.
1886 let output = work_dir.run_jj(&["run", "--passthrough", "-r", "..@", "--", "false"]);
1887 assert!(!output.status.success(), "expected `jj run` to fail");
1888 assert_eq!(get_log_output(&work_dir), log_before);
1889 }
1890
1891 #[test]
1892 fn test_run_passthrough_rejects_multi_job() {
1893 let test_env = TestEnvironment::default();
1894 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1895 let work_dir = test_env.work_dir("repo");
1896
1897 let output = work_dir.run_jj(&[
1898 "run",
1899 "--passthrough",
1900 "--jobs",
1901 "2",
1902 "-r",
1903 "@",
1904 "--",
1905 "true",
1906 ]);
1907 assert!(!output.status.success());
1908 insta::assert_snapshot!(output.stderr, @r"
1909 Error: cannot use --passthrough with more than one job
1910 [EOF]
1911 ");
1912 }
1913
1914 #[test]
1915 fn test_run_on_conflicted_commit() {
1916 // Regression test for https://github.com/jj-vcs/jj/issues/9747: `jj run`
1917 // panicked when its revset contained a conflicted commit. The post-command
1918 // snapshot of such a commit is a conflicted (unresolved) tree, and the code
1919 // assumed the result always resolved to a single tree.
1920 let mut test_env = TestEnvironment::default();
1921 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1922 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
1923 assert!(fake_formatter.is_file());
1924 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
1925 test_env.add_paths_to_normalize(fake_formatter.clone(), "$FAKE_FORMATTER_PATH");
1926 let work_dir = test_env.work_dir("repo");
1927
1928 // Build a merge whose two sides change `file` differently, so the merge
1929 // commit `conflict` (and the working copy on it) conflicts in `file`.
1930 create_commit_with_files(&work_dir, "base", &[], &[("file", "base\n")]);
1931 create_commit_with_files(&work_dir, "a", &["base"], &[("file", "a\n")]);
1932 create_commit_with_files(&work_dir, "b", &["base"], &[("file", "b\n")]);
1933 create_commit_with_files(&work_dir, "conflict", &["a", "b"], &[]);
1934 insta::assert_snapshot!(work_dir.run_jj(&["resolve", "--list"]).success().stdout, @r"
1935 file 2-sided conflict
1936 [EOF]
1937 ");
1938
1939 // `jj run` over the conflicted commit used to panic. `--tee` writes a file in
1940 // its working copy so the commit is actually rewritten, proving the conflict
1941 // survives the rewrite instead of crashing.
1942 let output = work_dir.run_jj(&[
1943 "run",
1944 "-r",
1945 "conflict",
1946 "--",
1947 &fake_formatter_path,
1948 "--stdout",
1949 "x",
1950 "--tee",
1951 "touched.txt",
1952 ]);
1953 insta::assert_snapshot!(output, @r"
1954 x[EOF]
1955 ------- stderr -------
1956 Rewrote 1 commits.
1957 Working copy (@) now at: vruxwmqv 74643194 conflict | (conflict) conflict
1958 Parent commit (@-) : zsuskuln 45537d53 a | a
1959 Parent commit (@-) : royxmykx 89d1b299 b | b
1960 Added 1 files, modified 0 files, removed 0 files
1961 Warning: There are unresolved conflicts at these paths:
1962 file 2-sided conflict
1963 New conflicts appeared in 1 commits:
1964 vruxwmqv 74643194 conflict | (conflict) conflict
1965 Hint: To resolve the conflicts, start by creating a commit on top of
1966 the conflicted commit:
1967 jj new vruxwmqv
1968 Then use `jj resolve`, or edit the conflict markers in the file directly.
1969 Once the conflicts are resolved, you can inspect the result with `jj diff`.
1970 Then run `jj squash` to move the resolution into the conflicted commit.
1971 [EOF]
1972 ");
1973
1974 // The rewritten commit still conflicts in `file` and gained touched.txt.
1975 insta::assert_snapshot!(work_dir.run_jj(&["resolve", "--list", "-r", "conflict"]).success().stdout, @r"
1976 file 2-sided conflict
1977 [EOF]
1978 ");
1979 insta::assert_snapshot!(work_dir.run_jj(&["file", "list", "-r", "conflict"]).success().stdout, @r"
1980 file
1981 touched.txt
1982 [EOF]
1983 ");
1984 }
1985
1986 #[test]
1987 fn test_run_touches_file_without_diff() {
1988 let mut test_env = TestEnvironment::default();
1989 let edit_script = test_env.set_up_fake_editor();
1990 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1991 let work_dir = test_env.work_dir("repo");
1992 work_dir.write_file("file.txt", "content\n");
1993 work_dir.run_jj(&["commit", "-m", "initial"]).success();
1994
1995 let log_before = work_dir
1996 .run_jj(&["log", "-T", "commit_id.short()"])
1997 .success()
1998 .stdout
1999 .to_string();
2000
2001 std::fs::write(&edit_script, "write\ncontent\n").unwrap();
2002 let output = work_dir
2003 .run_jj(&["run", "-r", "@-", "--", &fake_editor_path(), "file.txt"])
2004 .success();
2005 insta::assert_snapshot!(output.stderr, @r"
2006 Nothing changed.
2007 [EOF]
2008 ");
2009
2010 let log_after = work_dir
2011 .run_jj(&["log", "-T", "commit_id.short()"])
2012 .success()
2013 .stdout
2014 .to_string();
2015 assert_eq!(log_before, log_after);
2016 }
2017
2018 #[test]
2019 fn test_run_multi_commit_partial_diff() {
2020 let mut test_env = TestEnvironment::default();
2021 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2022 let fake_formatter = assert_cmd::cargo::cargo_bin("fake-formatter");
2023 assert!(fake_formatter.is_file());
2024 let fake_formatter_path = fake_formatter.to_string_lossy().into_owned();
2025 test_env.add_paths_to_normalize(fake_formatter.clone(), "$FAKE_FORMATTER_PATH");
2026 let work_dir = test_env.work_dir("repo");
2027
2028 // First commit has only root-level file; no `sub/` exists.
2029 work_dir.write_file("file_a.txt", "content_a\n");
2030 work_dir.run_jj(&["describe", "-m", "commit_a"]).success();
2031
2032 // Second commit adds `sub/file_b.txt`.
2033 work_dir.run_jj(&["new", "-m", "commit_b"]).success();
2034 work_dir.write_file("sub/file_b.txt", "content_b\n");
2035
2036 let log_commit_a_before = work_dir
2037 .run_jj(&[
2038 "log",
2039 "-r",
2040 "description('commit_a')",
2041 "-T",
2042 "commit_id.short()",
2043 ])
2044 .success()
2045 .stdout
2046 .to_string();
2047
2048 let sub_dir = work_dir.dir("sub");
2049 let output = sub_dir
2050 .run_jj(&[
2051 "run",
2052 "-r",
2053 "..@",
2054 "--",
2055 &fake_formatter_path,
2056 "--tee",
2057 "ran.txt",
2058 ])
2059 .success()
2060 .normalize_backslash();
2061
2062 insta::assert_snapshot!(output.stderr, @r"
2063 Skipped commit 9c0b665484a6ebf6770a7f72511c1a9c8ea75183: directory does not exist: sub
2064 Rewrote 1 commits.
2065 Working copy (@) now at: kkmpptxz 54d102ff commit_b
2066 Parent commit (@-) : qpvuntsm 9c0b6654 commit_a
2067 Added 1 files, modified 0 files, removed 0 files
2068 [EOF]
2069 ");
2070
2071 // commit_a was skipped and must NOT have been rewritten
2072 let log_commit_a_after = work_dir
2073 .run_jj(&[
2074 "log",
2075 "-r",
2076 "description('commit_a')",
2077 "-T",
2078 "commit_id.short()",
2079 ])
2080 .success()
2081 .stdout
2082 .to_string();
2083 assert_eq!(log_commit_a_before, log_commit_a_after);
2084 }