Debian Codegraph @

cli/tests/test_fix_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 #[cfg(unix)]
16 use std::os::unix::fs::PermissionsExt as _;
17 use std::path::PathBuf;
18
19 use indoc::formatdoc;
20 use indoc::indoc;
21 use jj_lib::file_util::symlink_file;
22 use testutils::TestResult;
23
24 use crate::common::TestEnvironment;
25 use crate::common::to_toml_value;
26
27 fn set_up_fake_formatter(test_env: &mut TestEnvironment, args: &[&str]) {
28 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
29 assert!(formatter_path.is_file());
30 test_env.add_config(formatdoc! {"
31 [fix.tools.fake-formatter]
32 command = {command}
33 patterns = ['all()']
34 ",
35 command = toml_edit::Value::from_iter(
36 [formatter_path.to_str().unwrap()]
37 .iter()
38 .chain(args)
39 .copied()
40 )
41 });
42 test_env.add_paths_to_normalize(formatter_path, "$FAKE_FORMATTER_PATH");
43 }
44
45 #[test]
46 fn test_config_no_tools() {
47 let test_env = TestEnvironment::default();
48 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
49 let work_dir = test_env.work_dir("repo");
50
51 work_dir.write_file("file", "content\n");
52 let output = work_dir.run_jj(["fix"]);
53 insta::assert_snapshot!(output, @"
54 ------- stderr -------
55 Config error: No `fix.tools` are configured
56 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
57 [EOF]
58 [exit status: 1]
59 ");
60
61 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
62 insta::assert_snapshot!(output, @"
63 content
64 [EOF]
65 ");
66 }
67
68 #[test]
69 fn test_config_nonexistent_tool() {
70 let test_env = TestEnvironment::default();
71 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
72 let work_dir = test_env.work_dir("repo");
73 test_env.add_config(
74 r###"
75 [fix.tools.my-tool]
76 command = ["nonexistent-fix-tool-binary"]
77 patterns = ["glob:**"]
78 "###,
79 );
80
81 work_dir.write_file("file", "content\n");
82 let output = work_dir.run_jj(["fix"]);
83 // We inform the user about the non-existent tool
84 insta::assert_snapshot!(output, @r"
85 ------- stderr -------
86 Warning: Failed to start `nonexistent-fix-tool-binary`.
87 Fixed 0 commits of 1 checked.
88 Nothing changed.
89 [EOF]
90 ");
91 }
92
93 #[test]
94 fn test_config_multiple_tools() {
95 let test_env = TestEnvironment::default();
96 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
97 let work_dir = test_env.work_dir("repo");
98 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
99 assert!(formatter_path.is_file());
100 let formatter = to_toml_value(formatter_path.to_str().unwrap());
101 test_env.add_config(format!(
102 r###"
103 [fix.tools.tool-1]
104 command = [{formatter}, "--uppercase"]
105 patterns = ["foo"]
106
107 [fix.tools.tool-2]
108 command = [{formatter}, "--lowercase"]
109 patterns = ["bar"]
110 "###,
111 ));
112
113 work_dir.write_file("foo", "Foo\n");
114 work_dir.write_file("bar", "Bar\n");
115 work_dir.write_file("baz", "Baz\n");
116
117 work_dir.run_jj(["fix"]).success();
118
119 let output = work_dir.run_jj(["file", "show", "foo", "-r", "@"]);
120 insta::assert_snapshot!(output, @"
121 FOO
122 [EOF]
123 ");
124 let output = work_dir.run_jj(["file", "show", "bar", "-r", "@"]);
125 insta::assert_snapshot!(output, @"
126 bar
127 [EOF]
128 ");
129 let output = work_dir.run_jj(["file", "show", "baz", "-r", "@"]);
130 insta::assert_snapshot!(output, @"
131 Baz
132 [EOF]
133 ");
134 }
135
136 #[test]
137 fn test_config_multiple_tools_with_same_name() {
138 let mut test_env = TestEnvironment::default();
139 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
140 let work_dir = test_env.work_dir("repo");
141 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
142 assert!(formatter_path.is_file());
143 let formatter = to_toml_value(formatter_path.to_str().unwrap());
144
145 // Multiple definitions with the same `name` are not allowed, because it is
146 // likely to be a mistake, and mistakes are risky when they rewrite files.
147 test_env.add_config(format!(
148 r###"
149 [fix.tools.my-tool]
150 command = [{formatter}, "--uppercase"]
151 patterns = ["foo"]
152
153 [fix.tools.my-tool]
154 command = [{formatter}, "--lowercase"]
155 patterns = ["bar"]
156 "###,
157 ));
158
159 work_dir.write_file("foo", "Foo\n");
160 work_dir.write_file("bar", "Bar\n");
161
162 let output = work_dir.run_jj(["fix"]);
163 insta::assert_snapshot!(output, @"
164 ------- stderr -------
165 Config error: Configuration cannot be parsed as TOML document
166 Caused by: TOML parse error at line 6, column 20
167 |
168 6 | [fix.tools.my-tool]
169 | ^^^^^^^
170 duplicate key
171
172 Hint: Check the config file: $TEST_ENV/config/config0002.toml
173 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
174 [EOF]
175 [exit status: 1]
176 ");
177
178 test_env.set_config_path("/dev/null");
179 let work_dir = test_env.work_dir("repo");
180 let output = work_dir.run_jj(["file", "show", "foo", "-r", "@"]);
181 insta::assert_snapshot!(output, @"
182 Foo
183 [EOF]
184 ");
185 let output = work_dir.run_jj(["file", "show", "bar", "-r", "@"]);
186 insta::assert_snapshot!(output, @"
187 Bar
188 [EOF]
189 ");
190 }
191
192 #[test]
193 fn test_config_disabled_tools() {
194 let test_env = TestEnvironment::default();
195 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
196 let work_dir = test_env.work_dir("repo");
197 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
198 assert!(formatter_path.is_file());
199 let formatter = to_toml_value(formatter_path.to_str().unwrap());
200 test_env.add_config(format!(
201 r###"
202 [fix.tools.tool-1]
203 # default is enabled
204 command = [{formatter}, "--uppercase"]
205 patterns = ["foo"]
206
207 [fix.tools.tool-2]
208 enabled = true
209 command = [{formatter}, "--lowercase"]
210 patterns = ["bar"]
211
212 [fix.tools.tool-3]
213 enabled = false
214 command = [{formatter}, "--lowercase"]
215 patterns = ["baz"]
216 "###
217 ));
218
219 work_dir.write_file("foo", "Foo\n");
220 work_dir.write_file("bar", "Bar\n");
221 work_dir.write_file("baz", "Baz\n");
222
223 work_dir.run_jj(["fix"]).success();
224
225 let output = work_dir.run_jj(["file", "show", "foo", "-r", "@"]);
226 insta::assert_snapshot!(output, @"
227 FOO
228 [EOF]
229 ");
230 let output = work_dir.run_jj(["file", "show", "bar", "-r", "@"]);
231 insta::assert_snapshot!(output, @"
232 bar
233 [EOF]
234 ");
235 let output = work_dir.run_jj(["file", "show", "baz", "-r", "@"]);
236 insta::assert_snapshot!(output, @"
237 Baz
238 [EOF]
239 ");
240 }
241
242 #[test]
243 fn test_config_disabled_tools_warning_when_all_tools_are_disabled() {
244 let test_env = TestEnvironment::default();
245 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
246 let work_dir = test_env.work_dir("repo");
247 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
248 assert!(formatter_path.is_file());
249 let formatter = to_toml_value(formatter_path.to_str().unwrap());
250 test_env.add_config(format!(
251 r###"
252 [fix.tools.tool-2]
253 enabled = false
254 command = [{formatter}, "--lowercase"]
255 patterns = ["bar"]
256 "###
257 ));
258
259 work_dir.write_file("bar", "Bar\n");
260
261 let output = work_dir.run_jj(["fix"]);
262 insta::assert_snapshot!(output, @"
263 ------- stderr -------
264 Config error: At least one entry of `fix.tools` must be enabled.
265 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
266 [EOF]
267 [exit status: 1]
268 ");
269 }
270
271 #[test]
272 fn test_config_tables_overlapping_patterns() {
273 let test_env = TestEnvironment::default();
274 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
275 let work_dir = test_env.work_dir("repo");
276 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
277 assert!(formatter_path.is_file());
278 let formatter = to_toml_value(formatter_path.to_str().unwrap());
279
280 test_env.add_config(format!(
281 r###"
282 [fix.tools.tool-1]
283 command = [{formatter}, "--append", "tool-1"]
284 patterns = ["foo", "bar"]
285
286 [fix.tools.tool-2]
287 command = [{formatter}, "--append", "tool-2"]
288 patterns = ["bar", "baz"]
289 "###,
290 ));
291
292 work_dir.write_file("foo", "foo\n");
293 work_dir.write_file("bar", "bar\n");
294 work_dir.write_file("baz", "baz\n");
295
296 work_dir.run_jj(["fix"]).success();
297
298 let output = work_dir.run_jj(["file", "show", "foo", "-r", "@"]);
299 insta::assert_snapshot!(output, @"
300 foo
301 tool-1[EOF]
302 ");
303 let output = work_dir.run_jj(["file", "show", "bar", "-r", "@"]);
304 insta::assert_snapshot!(output, @"
305 bar
306 tool-1tool-2[EOF]
307 ");
308 let output = work_dir.run_jj(["file", "show", "baz", "-r", "@"]);
309 insta::assert_snapshot!(output, @"
310 baz
311 tool-2[EOF]
312 ");
313 }
314
315 #[test]
316 fn test_config_tables_all_commands_missing() {
317 let test_env = TestEnvironment::default();
318 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
319 let work_dir = test_env.work_dir("repo");
320 test_env.add_config(
321 r###"
322 [fix.tools.my-tool-missing-command-1]
323 patterns = ["foo"]
324
325 [fix.tools.my-tool-missing-command-2]
326 patterns = ['glob:"ba*"']
327 "###,
328 );
329
330 work_dir.write_file("foo", "foo\n");
331
332 let output = work_dir.run_jj(["fix"]);
333 insta::assert_snapshot!(output.normalize_backslash(), @"
334 ------- stderr -------
335 Config error: Invalid type or value for fix.tools.my-tool-missing-command-1
336 Caused by: missing field `command`
337
338 Hint: Check the config file: $TEST_ENV/config/config0002.toml
339 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
340 [EOF]
341 [exit status: 1]
342 ");
343
344 let output = work_dir.run_jj(["file", "show", "foo", "-r", "@"]);
345 insta::assert_snapshot!(output, @"
346 foo
347 [EOF]
348 ");
349 }
350
351 #[test]
352 fn test_config_tables_some_commands_missing() {
353 let test_env = TestEnvironment::default();
354 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
355 let work_dir = test_env.work_dir("repo");
356 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
357 assert!(formatter_path.is_file());
358 let formatter = to_toml_value(formatter_path.to_str().unwrap());
359 test_env.add_config(format!(
360 r###"
361 [fix.tools.tool-1]
362 command = [{formatter}, "--uppercase"]
363 patterns = ["foo"]
364
365 [fix.tools.my-tool-missing-command]
366 patterns = ['bar']
367 "###,
368 ));
369
370 work_dir.write_file("foo", "foo\n");
371
372 let output = work_dir.run_jj(["fix"]);
373 insta::assert_snapshot!(output.normalize_backslash(), @"
374 ------- stderr -------
375 Config error: Invalid type or value for fix.tools.my-tool-missing-command
376 Caused by: missing field `command`
377
378 Hint: Check the config file: $TEST_ENV/config/config0002.toml
379 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
380 [EOF]
381 [exit status: 1]
382 ");
383
384 let output = work_dir.run_jj(["file", "show", "foo", "-r", "@"]);
385 insta::assert_snapshot!(output, @"
386 foo
387 [EOF]
388 ");
389 }
390
391 #[test]
392 fn test_config_tables_empty_patterns_list() {
393 let test_env = TestEnvironment::default();
394 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
395 let work_dir = test_env.work_dir("repo");
396 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
397 assert!(formatter_path.is_file());
398 let formatter = to_toml_value(formatter_path.to_str().unwrap());
399 test_env.add_config(format!(
400 r###"
401 [fix.tools.my-tool-empty-patterns]
402 command = [{formatter}, "--uppercase"]
403 patterns = []
404 "###,
405 ));
406
407 work_dir.write_file("foo", "foo\n");
408
409 let output = work_dir.run_jj(["fix"]);
410 insta::assert_snapshot!(output, @"
411 ------- stderr -------
412 Fixed 0 commits of 1 checked.
413 Nothing changed.
414 [EOF]
415 ");
416
417 let output = work_dir.run_jj(["file", "show", "foo", "-r", "@"]);
418 insta::assert_snapshot!(output, @"
419 foo
420 [EOF]
421 ");
422 }
423
424 #[test]
425 fn test_config_filesets() {
426 let test_env = TestEnvironment::default();
427 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
428 let work_dir = test_env.work_dir("repo");
429 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
430 assert!(formatter_path.is_file());
431 let formatter = to_toml_value(formatter_path.to_str().unwrap());
432 test_env.add_config(format!(
433 r###"
434 [fix.tools.my-tool-match-one]
435 command = [{formatter}, "--uppercase"]
436 patterns = ['glob:"a*"']
437
438 [fix.tools.my-tool-match-two]
439 command = [{formatter}, "--reverse"]
440 patterns = ['glob:"b*"']
441
442 [fix.tools.my-tool-match-none]
443 command = [{formatter}, "--append", "SHOULD NOT APPEAR"]
444 patterns = ['glob:"this-doesnt-match-anything-*"']
445 "###,
446 ));
447
448 work_dir.write_file("a1", "a1\n");
449 work_dir.write_file("b1", "b1\n");
450 work_dir.write_file("b2", "b2\n");
451
452 work_dir.run_jj(["fix"]).success();
453
454 let output = work_dir.run_jj(["file", "show", "a1", "-r", "@"]);
455 insta::assert_snapshot!(output, @"
456 A1
457 [EOF]
458 ");
459 let output = work_dir.run_jj(["file", "show", "b1", "-r", "@"]);
460 insta::assert_snapshot!(output, @"
461
462 1b[EOF]
463 ");
464 let output = work_dir.run_jj(["file", "show", "b2", "-r", "@"]);
465 insta::assert_snapshot!(output, @"
466
467 2b[EOF]
468 ");
469 }
470
471 #[test]
472 fn test_relative_paths() {
473 let test_env = TestEnvironment::default();
474 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
475 let work_dir = test_env.work_dir("repo");
476 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
477 assert!(formatter_path.is_file());
478 let formatter = to_toml_value(formatter_path.to_str().unwrap());
479 test_env.add_config(format!(
480 r###"
481 [fix.tools.tool]
482 command = [{formatter}, "--stdout", "Fixed!"]
483 patterns = ['glob:"foo*"']
484 "###,
485 ));
486
487 let sub_dir = work_dir.create_dir("dir");
488 work_dir.write_file("foo1", "unfixed\n");
489 work_dir.write_file("foo2", "unfixed\n");
490 work_dir.write_file("dir/foo3", "unfixed\n");
491
492 // Positional arguments are cwd-relative, but the configured patterns are
493 // repo-relative, so this command fixes the empty intersection of those
494 // filesets.
495 sub_dir.run_jj(["fix", "foo3"]).success();
496 let output = work_dir.run_jj(["file", "show", "foo1", "-r", "@"]);
497 insta::assert_snapshot!(output, @"
498 unfixed
499 [EOF]
500 ");
501 let output = work_dir.run_jj(["file", "show", "foo2", "-r", "@"]);
502 insta::assert_snapshot!(output, @"
503 unfixed
504 [EOF]
505 ");
506 let output = work_dir.run_jj(["file", "show", "dir/foo3", "-r", "@"]);
507 insta::assert_snapshot!(output, @"
508 unfixed
509 [EOF]
510 ");
511
512 // Positional arguments can specify a subset of the configured fileset.
513 sub_dir.run_jj(["fix", "../foo1"]).success();
514 let output = work_dir.run_jj(["file", "show", "foo1", "-r", "@"]);
515 insta::assert_snapshot!(output, @"Fixed![EOF]");
516 let output = work_dir.run_jj(["file", "show", "foo2", "-r", "@"]);
517 insta::assert_snapshot!(output, @"
518 unfixed
519 [EOF]
520 ");
521 let output = work_dir.run_jj(["file", "show", "dir/foo3", "-r", "@"]);
522 insta::assert_snapshot!(output, @"
523 unfixed
524 [EOF]
525 ");
526
527 // The current directory does not change the interpretation of the config, so
528 // foo2 is fixed but not dir/foo3.
529 sub_dir.run_jj(["fix"]).success();
530 let output = work_dir.run_jj(["file", "show", "foo1", "-r", "@"]);
531 insta::assert_snapshot!(output, @"Fixed![EOF]");
532 let output = work_dir.run_jj(["file", "show", "foo2", "-r", "@"]);
533 insta::assert_snapshot!(output, @"Fixed![EOF]");
534 let output = work_dir.run_jj(["file", "show", "dir/foo3", "-r", "@"]);
535 insta::assert_snapshot!(output, @"
536 unfixed
537 [EOF]
538 ");
539
540 // The output filtered to a non-existent file should display a warning.
541 let output = work_dir.run_jj(["fix", "nonexistent"]);
542 insta::assert_snapshot!(output, @"
543 ------- stderr -------
544 Warning: No matching entries for paths: nonexistent
545 Fixed 0 commits of 1 checked.
546 Nothing changed.
547 [EOF]
548 ");
549 }
550
551 #[test]
552 fn test_relative_tool_path_from_subdirectory() -> TestResult {
553 let test_env = TestEnvironment::default();
554 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
555 let work_dir = test_env.work_dir("repo");
556
557 // Copy the fake-formatter into the workspace as a relative tool
558 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
559 let formatter_name = formatter_path.file_name().unwrap().to_str().unwrap();
560 let tool_dir = work_dir.create_dir("tools");
561 let workspace_formatter_path = tool_dir.root().join(formatter_name);
562 std::fs::copy(formatter_path, &workspace_formatter_path)?;
563 work_dir.write_file(".gitignore", "tools/\n");
564 let formatter_relative_path = PathBuf::from_iter(["$root", "tools", formatter_name]);
565 test_env.add_config(format!(
566 r###"
567 [fix.tools.a]
568 command = [{path}, "--uppercase"]
569 patterns = ['glob:"**/*.txt"']
570 "###,
571 path = to_toml_value(formatter_relative_path.to_str().unwrap())
572 ));
573
574 // Create a test file and subdirectory
575 work_dir.write_file("test.txt", "hello world\n");
576 let sub_dir = work_dir.create_dir("subdir");
577 work_dir.write_file("subdir/nested.txt", "nested content\n");
578 work_dir.run_jj(["debug", "snapshot"]).success();
579 let setup_opid = work_dir.current_operation_id();
580
581 // Run fix from workspace root
582 let output = work_dir.run_jj(["fix"]);
583 insta::assert_snapshot!(output, @"
584 ------- stderr -------
585 Fixed 1 commits of 1 checked.
586 Working copy (@) now at: qpvuntsm 57a27b36 (no description set)
587 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
588 Added 0 files, modified 2 files, removed 0 files
589 [EOF]
590 ");
591
592 let output = work_dir.run_jj(["file", "show", "test.txt", "-r", "@"]);
593 insta::assert_snapshot!(output, @"
594 HELLO WORLD
595 [EOF]
596 ");
597 let output = work_dir.run_jj(["file", "show", "subdir/nested.txt", "-r", "@"]);
598 insta::assert_snapshot!(output, @"
599 NESTED CONTENT
600 [EOF]
601 ");
602
603 // Reset so the fix tools should have an effect again
604 work_dir.run_jj(["op", "restore", &setup_opid]).success();
605
606 // Run fix from the subdirectory
607 let output = sub_dir.run_jj(["fix"]);
608 insta::assert_snapshot!(output, @"
609 ------- stderr -------
610 Fixed 1 commits of 1 checked.
611 Working copy (@) now at: qpvuntsm 05404d5b (no description set)
612 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
613 Added 0 files, modified 2 files, removed 0 files
614 [EOF]
615 ");
616
617 let output = work_dir.run_jj(["file", "show", "test.txt", "-r", "@"]);
618 insta::assert_snapshot!(output, @"
619 HELLO WORLD
620 [EOF]
621 ");
622 let output = work_dir.run_jj(["file", "show", "subdir/nested.txt", "-r", "@"]);
623 insta::assert_snapshot!(output, @"
624 NESTED CONTENT
625 [EOF]
626 ");
627 Ok(())
628 }
629
630 #[test]
631 fn test_fix_empty_commit() {
632 let mut test_env = TestEnvironment::default();
633 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
634 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
635 let work_dir = test_env.work_dir("repo");
636 let output = work_dir.run_jj(["fix", "-s", "@"]);
637 insta::assert_snapshot!(output, @"
638 ------- stderr -------
639 Fixed 0 commits of 1 checked.
640 Nothing changed.
641 [EOF]
642 ");
643 }
644
645 #[test]
646 fn test_fix_leaf_commit() {
647 let mut test_env = TestEnvironment::default();
648 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
649 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
650 let work_dir = test_env.work_dir("repo");
651 work_dir.write_file("file", "unaffected");
652 work_dir.run_jj(["new"]).success();
653 work_dir.write_file("file", "affected");
654
655 let output = work_dir.run_jj(["fix", "-s", "@"]);
656 insta::assert_snapshot!(output, @"
657 ------- stderr -------
658 Fixed 1 commits of 1 checked.
659 Working copy (@) now at: rlvkpnrz f5c11961 (no description set)
660 Parent commit (@-) : qpvuntsm b37955c0 (no description set)
661 Added 0 files, modified 1 files, removed 0 files
662 [EOF]
663 ");
664 let output = work_dir.run_jj(["file", "show", "file", "-r", "@-"]);
665 insta::assert_snapshot!(output, @"unaffected[EOF]");
666 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
667 insta::assert_snapshot!(output, @"AFFECTED[EOF]");
668 }
669
670 #[test]
671 fn test_fix_parent_commit() {
672 let mut test_env = TestEnvironment::default();
673 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
674 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
675 let work_dir = test_env.work_dir("repo");
676 // Using one file name for all commits adds coverage of some possible bugs.
677 work_dir.write_file("file", "parent");
678 work_dir
679 .run_jj(["bookmark", "create", "-r@", "parent"])
680 .success();
681 work_dir.run_jj(["new"]).success();
682 work_dir.write_file("file", "child1");
683 work_dir
684 .run_jj(["bookmark", "create", "-r@", "child1"])
685 .success();
686 work_dir.run_jj(["new", "-r", "parent"]).success();
687 work_dir.write_file("file", "child2");
688 work_dir
689 .run_jj(["bookmark", "create", "-r@", "child2"])
690 .success();
691
692 let output = work_dir.run_jj(["fix", "-s", "parent"]);
693 insta::assert_snapshot!(output, @"
694 ------- stderr -------
695 Fixed 3 commits of 3 checked.
696 Working copy (@) now at: mzvwutvl e7ba6d31 child2 | (no description set)
697 Parent commit (@-) : qpvuntsm 49f1ddd5 parent | (no description set)
698 Added 0 files, modified 1 files, removed 0 files
699 [EOF]
700 ");
701 let output = work_dir.run_jj(["file", "show", "file", "-r", "parent"]);
702 insta::assert_snapshot!(output, @"PARENT[EOF]");
703 let output = work_dir.run_jj(["file", "show", "file", "-r", "child1"]);
704 insta::assert_snapshot!(output, @"CHILD1[EOF]");
705 let output = work_dir.run_jj(["file", "show", "file", "-r", "child2"]);
706 insta::assert_snapshot!(output, @"CHILD2[EOF]");
707 }
708
709 #[test]
710 fn test_fix_sibling_commit() {
711 let mut test_env = TestEnvironment::default();
712 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
713 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
714 let work_dir = test_env.work_dir("repo");
715 work_dir.write_file("file", "parent");
716 work_dir
717 .run_jj(["bookmark", "create", "-r@", "parent"])
718 .success();
719 work_dir.run_jj(["new"]).success();
720 work_dir.write_file("file", "child1");
721 work_dir
722 .run_jj(["bookmark", "create", "-r@", "child1"])
723 .success();
724 work_dir.run_jj(["new", "-r", "parent"]).success();
725 work_dir.write_file("file", "child2");
726 work_dir
727 .run_jj(["bookmark", "create", "-r@", "child2"])
728 .success();
729
730 let output = work_dir.run_jj(["fix", "-s", "child1"]);
731 insta::assert_snapshot!(output, @"
732 ------- stderr -------
733 Fixed 1 commits of 1 checked.
734 [EOF]
735 ");
736 let output = work_dir.run_jj(["file", "show", "file", "-r", "parent"]);
737 insta::assert_snapshot!(output, @"parent[EOF]");
738 let output = work_dir.run_jj(["file", "show", "file", "-r", "child1"]);
739 insta::assert_snapshot!(output, @"CHILD1[EOF]");
740 let output = work_dir.run_jj(["file", "show", "file", "-r", "child2"]);
741 insta::assert_snapshot!(output, @"child2[EOF]");
742 }
743
744 #[test]
745 fn test_fix_descendant_commits() {
746 let mut test_env = TestEnvironment::default();
747 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
748 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
749 let work_dir = test_env.work_dir("repo");
750 work_dir.write_file("parent", "parent");
751 work_dir
752 .run_jj(["bookmark", "create", "-r@", "parent"])
753 .success();
754 work_dir.run_jj(["new"]).success();
755 work_dir.write_file("child1", "child1");
756 work_dir
757 .run_jj(["bookmark", "create", "-r@", "child1"])
758 .success();
759 work_dir.run_jj(["new", "-r", "parent"]).success();
760 work_dir.write_file("child2", "child2");
761 work_dir
762 .run_jj(["bookmark", "create", "-r@", "child2"])
763 .success();
764
765 let output = work_dir.run_jj(["fix", "-s", "parent", "child1", "child2", "nonexistent"]);
766 insta::assert_snapshot!(output, @"
767 ------- stderr -------
768 Warning: No matching entries for paths: nonexistent
769 Fixed 2 commits of 3 checked.
770 Working copy (@) now at: mzvwutvl afe0ade0 child2 | (no description set)
771 Parent commit (@-) : qpvuntsm c9cb6288 parent | (no description set)
772 Added 0 files, modified 1 files, removed 0 files
773 [EOF]
774 ");
775 let output = work_dir.run_jj(["file", "show", "parent", "-r", "parent"]);
776 insta::assert_snapshot!(output, @"parent[EOF]");
777 let output = work_dir.run_jj(["file", "show", "child1", "-r", "child1"]);
778 insta::assert_snapshot!(output, @"CHILD1[EOF]");
779 let output = work_dir.run_jj(["file", "show", "child2", "-r", "child2"]);
780 insta::assert_snapshot!(output, @"CHILD2[EOF]");
781 }
782
783 #[test]
784 fn test_default_revset() {
785 let mut test_env = TestEnvironment::default();
786 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
787 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
788 let work_dir = test_env.work_dir("repo");
789 work_dir.write_file("file", "trunk1");
790 work_dir
791 .run_jj(["bookmark", "create", "-r@", "trunk1"])
792 .success();
793 work_dir.run_jj(["new"]).success();
794 work_dir.write_file("file", "trunk2");
795 work_dir
796 .run_jj(["bookmark", "create", "-r@", "trunk2"])
797 .success();
798 work_dir.run_jj(["new", "trunk1"]).success();
799 work_dir.write_file("file", "foo");
800 work_dir
801 .run_jj(["bookmark", "create", "-r@", "foo"])
802 .success();
803 work_dir.run_jj(["new", "trunk1"]).success();
804 work_dir.write_file("file", "bar1");
805 work_dir
806 .run_jj(["bookmark", "create", "-r@", "bar1"])
807 .success();
808 work_dir.run_jj(["new"]).success();
809 work_dir.write_file("file", "bar2");
810 work_dir
811 .run_jj(["bookmark", "create", "-r@", "bar2"])
812 .success();
813 work_dir.run_jj(["new"]).success();
814 work_dir.write_file("file", "bar3");
815 work_dir
816 .run_jj(["bookmark", "create", "-r@", "bar3"])
817 .success();
818 work_dir.run_jj(["edit", "bar2"]).success();
819
820 // With no args and no revset configuration, we fix `reachable(@, mutable())`,
821 // which includes bar{1,2,3} and excludes trunk{1,2} (which is immutable) and
822 // foo (which is mutable but not reachable).
823 test_env.add_config(r#"revset-aliases."immutable_heads()" = "trunk2""#);
824 let output = work_dir.run_jj(["fix"]);
825 insta::assert_snapshot!(output, @"
826 ------- stderr -------
827 Fixed 3 commits of 3 checked.
828 Working copy (@) now at: yostqsxw 932b950d bar2 | (no description set)
829 Parent commit (@-) : yqosqzyt 8a37ed67 bar1 | (no description set)
830 Added 0 files, modified 1 files, removed 0 files
831 [EOF]
832 ");
833 let output = work_dir.run_jj(["file", "show", "file", "-r", "trunk1"]);
834 insta::assert_snapshot!(output, @"trunk1[EOF]");
835 let output = work_dir.run_jj(["file", "show", "file", "-r", "trunk2"]);
836 insta::assert_snapshot!(output, @"trunk2[EOF]");
837 let output = work_dir.run_jj(["file", "show", "file", "-r", "foo"]);
838 insta::assert_snapshot!(output, @"foo[EOF]");
839 let output = work_dir.run_jj(["file", "show", "file", "-r", "bar1"]);
840 insta::assert_snapshot!(output, @"BAR1[EOF]");
841 let output = work_dir.run_jj(["file", "show", "file", "-r", "bar2"]);
842 insta::assert_snapshot!(output, @"BAR2[EOF]");
843 let output = work_dir.run_jj(["file", "show", "file", "-r", "bar3"]);
844 insta::assert_snapshot!(output, @"BAR3[EOF]");
845 }
846
847 #[test]
848 fn test_custom_default_revset() {
849 let mut test_env = TestEnvironment::default();
850 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
851 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
852 let work_dir = test_env.work_dir("repo");
853
854 work_dir.write_file("file", "foo");
855 work_dir
856 .run_jj(["bookmark", "create", "-r@", "foo"])
857 .success();
858 work_dir.run_jj(["new"]).success();
859 work_dir.write_file("file", "bar");
860 work_dir
861 .run_jj(["bookmark", "create", "-r@", "bar"])
862 .success();
863
864 // Check out a different commit so that the schema default `reachable(@,
865 // mutable())` would behave differently from our customized default.
866 work_dir.run_jj(["new", "-r", "foo"]).success();
867 test_env.add_config(r#"revsets.fix = "bar""#);
868
869 let output = work_dir.run_jj(["fix"]);
870 insta::assert_snapshot!(output, @"
871 ------- stderr -------
872 Fixed 1 commits of 1 checked.
873 [EOF]
874 ");
875 let output = work_dir.run_jj(["file", "show", "file", "-r", "foo"]);
876 insta::assert_snapshot!(output, @"foo[EOF]");
877 let output = work_dir.run_jj(["file", "show", "file", "-r", "bar"]);
878 insta::assert_snapshot!(output, @"BAR[EOF]");
879 }
880
881 #[test]
882 fn test_fix_immutable_commit() {
883 let mut test_env = TestEnvironment::default();
884 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
885 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
886 let work_dir = test_env.work_dir("repo");
887 work_dir.write_file("file", "immutable");
888 work_dir
889 .run_jj(["bookmark", "create", "-r@", "immutable"])
890 .success();
891 work_dir.run_jj(["new"]).success();
892 work_dir.write_file("file", "mutable");
893 work_dir
894 .run_jj(["bookmark", "create", "-r@", "mutable"])
895 .success();
896 test_env.add_config(r#"revset-aliases."immutable_heads()" = "immutable""#);
897
898 let output = work_dir.run_jj(["fix", "-s", "immutable"]);
899 insta::assert_snapshot!(output, @r#"
900 ------- stderr -------
901 Error: Commit a86b2eccaaab is immutable
902 Hint: Could not modify commit: qpvuntsm a86b2ecc immutable | (no description set)
903 Hint: Immutable commits are used to protect shared history.
904 Hint: For more information, see:
905 - https://docs.jj-vcs.dev/latest/config/#set-of-immutable-commits
906 - `jj help -k config`, "Set of immutable commits"
907 Hint: This operation would rewrite 1 immutable commits.
908 [EOF]
909 [exit status: 1]
910 "#);
911 let output = work_dir.run_jj(["file", "show", "file", "-r", "immutable"]);
912 insta::assert_snapshot!(output, @"immutable[EOF]");
913 let output = work_dir.run_jj(["file", "show", "file", "-r", "mutable"]);
914 insta::assert_snapshot!(output, @"mutable[EOF]");
915 }
916
917 #[test]
918 fn test_fix_empty_file() {
919 let mut test_env = TestEnvironment::default();
920 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
921 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
922 let work_dir = test_env.work_dir("repo");
923 work_dir.write_file("file", "");
924
925 let output = work_dir.run_jj(["fix", "-s", "@"]);
926 insta::assert_snapshot!(output, @"
927 ------- stderr -------
928 Fixed 0 commits of 1 checked.
929 Nothing changed.
930 [EOF]
931 ");
932 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
933 insta::assert_snapshot!(output, @"");
934 }
935
936 #[test]
937 fn test_fix_large_file() {
938 let mut test_env = TestEnvironment::default();
939 // Set to byte mode, so that fake-formatter doesn't read from the stdin all at
940 // once.
941 set_up_fake_formatter(&mut test_env, &["--lowercase", "--byte-mode"]);
942 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
943 let work_dir = test_env.work_dir("repo");
944 // 512KB should be larger than most default page size, so that the stdin pipe
945 // will be blocked if fake-formatter doesn't read, and the stdout pipe will
946 // be blocked if jj doesn't read.
947 let mut large_contents = b"A".repeat(0x800_000);
948 large_contents.push(b'\n');
949 work_dir.write_file("file", &large_contents);
950
951 work_dir
952 .run_jj([
953 "config",
954 "set",
955 "--repo",
956 "snapshot.max-new-file-size",
957 &large_contents.len().to_string(),
958 ])
959 .success();
960 let output = work_dir.run_jj(["fix", "-s", "@"]);
961 insta::assert_snapshot!(output, @"
962 ------- stderr -------
963 Fixed 1 commits of 1 checked.
964 Working copy (@) now at: qpvuntsm d500c021 (no description set)
965 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
966 Added 0 files, modified 1 files, removed 0 files
967 [EOF]
968 ");
969 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
970 let stdout_bytes = output.stdout.raw().as_bytes();
971 assert_eq!(stdout_bytes.len(), large_contents.len());
972 assert!(stdout_bytes == large_contents.to_ascii_lowercase());
973 }
974
975 #[test]
976 fn test_fix_some_paths() {
977 let mut test_env = TestEnvironment::default();
978 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
979 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
980 let work_dir = test_env.work_dir("repo");
981 work_dir.write_file("file1", "foo");
982 work_dir.write_file("file2", "bar");
983
984 let output = work_dir.run_jj(["fix", "-s", "@", "file1"]);
985 insta::assert_snapshot!(output, @"
986 ------- stderr -------
987 Fixed 1 commits of 1 checked.
988 Working copy (@) now at: qpvuntsm 0279baba (no description set)
989 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
990 Added 0 files, modified 1 files, removed 0 files
991 [EOF]
992 ");
993 let output = work_dir.run_jj(["file", "show", "file1"]);
994 insta::assert_snapshot!(output, @"FOO[EOF]");
995 let output = work_dir.run_jj(["file", "show", "file2"]);
996 insta::assert_snapshot!(output, @"bar[EOF]");
997 }
998
999 #[test]
1000 fn test_fix_cyclic() {
1001 let mut test_env = TestEnvironment::default();
1002 set_up_fake_formatter(&mut test_env, &["--reverse"]);
1003 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1004 let work_dir = test_env.work_dir("repo");
1005 work_dir.write_file("file", "content\n");
1006
1007 let output = work_dir.run_jj(["fix"]);
1008 insta::assert_snapshot!(output, @"
1009 ------- stderr -------
1010 Fixed 1 commits of 1 checked.
1011 Working copy (@) now at: qpvuntsm ce361156 (no description set)
1012 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
1013 Added 0 files, modified 1 files, removed 0 files
1014 [EOF]
1015 ");
1016 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
1017 insta::assert_snapshot!(output, @"
1018
1019 tnetnoc[EOF]
1020 ");
1021
1022 let output = work_dir.run_jj(["fix"]);
1023 insta::assert_snapshot!(output, @"
1024 ------- stderr -------
1025 Fixed 1 commits of 1 checked.
1026 Working copy (@) now at: qpvuntsm 547f589b (no description set)
1027 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
1028 Added 0 files, modified 1 files, removed 0 files
1029 [EOF]
1030 ");
1031 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
1032 insta::assert_snapshot!(output, @"
1033
1034 content[EOF]
1035 ");
1036 }
1037
1038 #[test]
1039 fn test_deduplication() {
1040 // Append all fixed content to a log file. Note that fix tools are always run
1041 // from the workspace root, so this will always write to $root/$path-fixlog.
1042 let mut test_env = TestEnvironment::default();
1043 set_up_fake_formatter(&mut test_env, &["--uppercase", "--tee", "$path-fixlog"]);
1044 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1045 let work_dir = test_env.work_dir("repo");
1046
1047 // There are at least two interesting cases: the content is repeated immediately
1048 // in the child commit, or later in another descendant.
1049 work_dir.write_file("file", "foo\n");
1050 work_dir
1051 .run_jj(["bookmark", "create", "-r@", "a"])
1052 .success();
1053 work_dir.run_jj(["new"]).success();
1054 work_dir.write_file("file", "bar\n");
1055 work_dir
1056 .run_jj(["bookmark", "create", "-r@", "b"])
1057 .success();
1058 work_dir.run_jj(["new"]).success();
1059 work_dir.write_file("file", "bar\n");
1060 work_dir
1061 .run_jj(["bookmark", "create", "-r@", "c"])
1062 .success();
1063 work_dir.run_jj(["new"]).success();
1064 work_dir.write_file("file", "foo\n");
1065 work_dir
1066 .run_jj(["bookmark", "create", "-r@", "d"])
1067 .success();
1068
1069 let output = work_dir.run_jj(["fix", "-s", "a"]);
1070 insta::assert_snapshot!(output, @"
1071 ------- stderr -------
1072 Fixed 4 commits of 4 checked.
1073 Working copy (@) now at: yqosqzyt 9849a250 d | (no description set)
1074 Parent commit (@-) : mzvwutvl 9544f381 c | (empty) (no description set)
1075 Added 0 files, modified 1 files, removed 0 files
1076 [EOF]
1077 ");
1078 let output = work_dir.run_jj(["file", "show", "file", "-r", "a"]);
1079 insta::assert_snapshot!(output, @"
1080 FOO
1081 [EOF]
1082 ");
1083 let output = work_dir.run_jj(["file", "show", "file", "-r", "b"]);
1084 insta::assert_snapshot!(output, @"
1085 BAR
1086 [EOF]
1087 ");
1088 let output = work_dir.run_jj(["file", "show", "file", "-r", "c"]);
1089 insta::assert_snapshot!(output, @"
1090 BAR
1091 [EOF]
1092 ");
1093 let output = work_dir.run_jj(["file", "show", "file", "-r", "d"]);
1094 insta::assert_snapshot!(output, @"
1095 FOO
1096 [EOF]
1097 ");
1098
1099 // Each new content string only appears once in the log, because all the other
1100 // inputs (like file name) were identical, and so the results were reused. We
1101 // sort the log because the order of execution inside `jj fix` is undefined.
1102 insta::assert_snapshot!(sorted_lines(work_dir.root().join("file-fixlog")), @"
1103 BAR
1104 FOO
1105 ");
1106 }
1107
1108 fn sorted_lines(path: PathBuf) -> String {
1109 let mut log: Vec<_> = std::fs::read_to_string(path.as_os_str())
1110 .unwrap()
1111 .lines()
1112 .map(String::from)
1113 .collect();
1114 log.sort();
1115 log.join("\n")
1116 }
1117
1118 #[test]
1119 fn test_executed_but_nothing_changed() {
1120 // Show that the tool ran by causing a side effect with --tee, and test that we
1121 // do the right thing when the tool's output is exactly equal to its input.
1122 let mut test_env = TestEnvironment::default();
1123 set_up_fake_formatter(&mut test_env, &["--tee", "$path-copy"]);
1124 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1125 let work_dir = test_env.work_dir("repo");
1126 work_dir.write_file("file", "content\n");
1127
1128 let output = work_dir.run_jj(["fix", "-s", "@"]);
1129 insta::assert_snapshot!(output, @"
1130 ------- stderr -------
1131 Fixed 0 commits of 1 checked.
1132 Nothing changed.
1133 [EOF]
1134 ");
1135 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
1136 insta::assert_snapshot!(output, @"
1137 content
1138 [EOF]
1139 ");
1140 let copy_content = work_dir.read_file("file-copy");
1141 insta::assert_snapshot!(copy_content, @"content");
1142
1143 // fix tools are always run from the workspace root, regardless of working
1144 // directory at time of invocation.
1145 let sub_dir = work_dir.create_dir("dir");
1146 let output = sub_dir.run_jj(["fix"]);
1147 insta::assert_snapshot!(output, @"
1148 ------- stderr -------
1149 Fixed 0 commits of 1 checked.
1150 Nothing changed.
1151 [EOF]
1152 ");
1153
1154 let copy_content = work_dir.read_file("file-copy");
1155 insta::assert_snapshot!(copy_content, @"
1156 content
1157 content
1158 ");
1159 assert!(!sub_dir.root().join("file-copy").exists());
1160 }
1161
1162 #[test]
1163 fn test_failure() {
1164 let mut test_env = TestEnvironment::default();
1165 set_up_fake_formatter(&mut test_env, &["--fail"]);
1166 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1167 let work_dir = test_env.work_dir("repo");
1168 work_dir.write_file("file", "content");
1169
1170 let output = work_dir.run_jj(["fix", "-s", "@"]);
1171 insta::assert_snapshot!(output, @"
1172 ------- stderr -------
1173 Warning: Fix tool `$FAKE_FORMATTER_PATH` exited with non-zero exit code for `file`
1174 Fixed 0 commits of 1 checked.
1175 Nothing changed.
1176 [EOF]
1177 ");
1178 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
1179 insta::assert_snapshot!(output, @"content[EOF]");
1180 }
1181
1182 #[test]
1183 fn test_stderr_success() {
1184 let mut test_env = TestEnvironment::default();
1185 set_up_fake_formatter(
1186 &mut test_env,
1187 &["--stderr", "error", "--stdout", "new content"],
1188 );
1189 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1190 let work_dir = test_env.work_dir("repo");
1191 work_dir.write_file("file", "old content");
1192
1193 // TODO: Associate the stderr lines with the relevant tool/file/commit instead
1194 // of passing it through directly.
1195 let output = work_dir.run_jj(["fix", "-s", "@"]);
1196 insta::assert_snapshot!(output, @"
1197 ------- stderr -------
1198 file:
1199 error
1200 Fixed 1 commits of 1 checked.
1201 Working copy (@) now at: qpvuntsm cb75cbcb (no description set)
1202 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
1203 Added 0 files, modified 1 files, removed 0 files
1204 [EOF]
1205 ");
1206 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
1207 insta::assert_snapshot!(output, @"new content[EOF]");
1208 }
1209
1210 #[test]
1211 fn test_stderr_failure() {
1212 let mut test_env = TestEnvironment::default();
1213 set_up_fake_formatter(
1214 &mut test_env,
1215 &["--stderr", "error", "--stdout", "new content", "--fail"],
1216 );
1217 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1218 let work_dir = test_env.work_dir("repo");
1219 work_dir.write_file("file", "old content");
1220
1221 let output = work_dir.run_jj(["fix", "-s", "@"]);
1222 insta::assert_snapshot!(output, @"
1223 ------- stderr -------
1224 file:
1225 error
1226 Warning: Fix tool `$FAKE_FORMATTER_PATH` exited with non-zero exit code for `file`
1227 Fixed 0 commits of 1 checked.
1228 Nothing changed.
1229 [EOF]
1230 ");
1231 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
1232 insta::assert_snapshot!(output, @"old content[EOF]");
1233 }
1234
1235 #[test]
1236 fn test_missing_command() {
1237 let test_env = TestEnvironment::default();
1238 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1239 let work_dir = test_env.work_dir("repo");
1240 test_env.add_config(indoc! {"
1241 [fix.tools.bad-tool]
1242 command = ['this_executable_shouldnt_exist']
1243 patterns = ['all()']
1244 "});
1245 // TODO: We should display a warning about invalid tool configurations. When we
1246 // support multiple tools, we should also keep going to see if any of the other
1247 // executions succeed.
1248 let output = work_dir.run_jj(["fix", "-s", "@"]);
1249 insta::assert_snapshot!(output, @"
1250 ------- stderr -------
1251 Fixed 0 commits of 1 checked.
1252 Nothing changed.
1253 [EOF]
1254 ");
1255 }
1256
1257 #[test]
1258 fn test_fix_file_types() -> TestResult {
1259 let mut test_env = TestEnvironment::default();
1260 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
1261 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1262 let work_dir = test_env.work_dir("repo");
1263 work_dir.write_file("file", "content");
1264 work_dir.create_dir("dir");
1265 symlink_file("file", work_dir.root().join("link"))?;
1266
1267 let output = work_dir.run_jj(["fix", "-s", "@"]);
1268 insta::assert_snapshot!(output, @"
1269 ------- stderr -------
1270 Fixed 1 commits of 1 checked.
1271 Working copy (@) now at: qpvuntsm 0184b215 (no description set)
1272 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
1273 Added 0 files, modified 1 files, removed 0 files
1274 [EOF]
1275 ");
1276 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
1277 insta::assert_snapshot!(output, @"CONTENT[EOF]");
1278 Ok(())
1279 }
1280
1281 #[cfg(unix)]
1282 #[test]
1283 fn test_fix_executable() -> TestResult {
1284 let mut test_env = TestEnvironment::default();
1285 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
1286 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1287 let work_dir = test_env.work_dir("repo");
1288 let path = work_dir.root().join("file");
1289 work_dir.write_file("file", "content");
1290 let mut permissions = std::fs::metadata(&path)?.permissions();
1291 permissions.set_mode(permissions.mode() | 0o111);
1292 std::fs::set_permissions(&path, permissions)?;
1293
1294 let output = work_dir.run_jj(["fix", "-s", "@"]);
1295 insta::assert_snapshot!(output, @"
1296 ------- stderr -------
1297 Fixed 1 commits of 1 checked.
1298 Working copy (@) now at: qpvuntsm 5293bf26 (no description set)
1299 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
1300 Added 0 files, modified 1 files, removed 0 files
1301 [EOF]
1302 ");
1303 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
1304 insta::assert_snapshot!(output, @"CONTENT[EOF]");
1305 let executable = std::fs::metadata(&path)?.permissions().mode() & 0o111;
1306 assert_eq!(executable, 0o111);
1307 Ok(())
1308 }
1309
1310 #[test]
1311 fn test_fix_trivial_merge_commit() {
1312 // All the changes are attributable to a parent, so none are fixed (in the same
1313 // way that none would be shown in `jj diff -r @`).
1314 let mut test_env = TestEnvironment::default();
1315 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
1316 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1317 let work_dir = test_env.work_dir("repo");
1318 work_dir.write_file("file_a", "content a");
1319 work_dir.write_file("file_c", "content c");
1320 work_dir
1321 .run_jj(["bookmark", "create", "-r@", "a"])
1322 .success();
1323 work_dir.run_jj(["new", "@-"]).success();
1324 work_dir.write_file("file_b", "content b");
1325 work_dir.write_file("file_c", "content c");
1326 work_dir
1327 .run_jj(["bookmark", "create", "-r@", "b"])
1328 .success();
1329 work_dir.run_jj(["new", "a", "b"]).success();
1330
1331 let output = work_dir.run_jj(["fix", "-s", "@"]);
1332 insta::assert_snapshot!(output, @"
1333 ------- stderr -------
1334 Fixed 0 commits of 1 checked.
1335 Nothing changed.
1336 [EOF]
1337 ");
1338 let output = work_dir.run_jj(["file", "show", "file_a", "-r", "@"]);
1339 insta::assert_snapshot!(output, @"content a[EOF]");
1340 let output = work_dir.run_jj(["file", "show", "file_b", "-r", "@"]);
1341 insta::assert_snapshot!(output, @"content b[EOF]");
1342 let output = work_dir.run_jj(["file", "show", "file_c", "-r", "@"]);
1343 insta::assert_snapshot!(output, @"content c[EOF]");
1344 }
1345
1346 #[test]
1347 fn test_fix_adding_merge_commit() {
1348 // None of the changes are attributable to a parent, so they are all fixed (in
1349 // the same way that they would be shown in `jj diff -r @`).
1350 let mut test_env = TestEnvironment::default();
1351 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
1352 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1353 let work_dir = test_env.work_dir("repo");
1354 work_dir.write_file("file_a", "content a");
1355 work_dir.write_file("file_c", "content c");
1356 work_dir
1357 .run_jj(["bookmark", "create", "-r@", "a"])
1358 .success();
1359 work_dir.run_jj(["new", "@-"]).success();
1360 work_dir.write_file("file_b", "content b");
1361 work_dir.write_file("file_c", "content c");
1362 work_dir
1363 .run_jj(["bookmark", "create", "-r@", "b"])
1364 .success();
1365 work_dir.run_jj(["new", "a", "b"]).success();
1366 work_dir.write_file("file_a", "change a");
1367 work_dir.write_file("file_b", "change b");
1368 work_dir.write_file("file_c", "change c");
1369 work_dir.write_file("file_d", "change d");
1370
1371 let output = work_dir.run_jj(["fix", "-s", "@"]);
1372 insta::assert_snapshot!(output, @"
1373 ------- stderr -------
1374 Fixed 1 commits of 1 checked.
1375 Working copy (@) now at: mzvwutvl 9f580aac (no description set)
1376 Parent commit (@-) : qpvuntsm 93f04460 a | (no description set)
1377 Parent commit (@-) : kkmpptxz ad4fc36c b | (no description set)
1378 Added 0 files, modified 4 files, removed 0 files
1379 [EOF]
1380 ");
1381 let output = work_dir.run_jj(["file", "show", "file_a", "-r", "@"]);
1382 insta::assert_snapshot!(output, @"CHANGE A[EOF]");
1383 let output = work_dir.run_jj(["file", "show", "file_b", "-r", "@"]);
1384 insta::assert_snapshot!(output, @"CHANGE B[EOF]");
1385 let output = work_dir.run_jj(["file", "show", "file_c", "-r", "@"]);
1386 insta::assert_snapshot!(output, @"CHANGE C[EOF]");
1387 let output = work_dir.run_jj(["file", "show", "file_d", "-r", "@"]);
1388 insta::assert_snapshot!(output, @"CHANGE D[EOF]");
1389 }
1390
1391 #[test]
1392 fn test_fix_both_sides_of_conflict() {
1393 let mut test_env = TestEnvironment::default();
1394 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
1395 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1396 let work_dir = test_env.work_dir("repo");
1397 work_dir.write_file("file", "content a\n");
1398 work_dir
1399 .run_jj(["bookmark", "create", "-r@", "a"])
1400 .success();
1401 work_dir.run_jj(["new", "@-"]).success();
1402 work_dir.write_file("file", "content b\n");
1403 work_dir
1404 .run_jj(["bookmark", "create", "-r@", "b"])
1405 .success();
1406 work_dir.run_jj(["new", "a", "b"]).success();
1407
1408 // The conflicts are not different from the merged parent, so they would not be
1409 // fixed if we didn't fix the parents also.
1410 let output = work_dir.run_jj(["fix", "-s", "a", "-s", "b"]);
1411 insta::assert_snapshot!(output, @"
1412 ------- stderr -------
1413 Fixed 3 commits of 3 checked.
1414 Working copy (@) now at: mzvwutvl 1bfa5dc3 (conflict) (empty) (no description set)
1415 Parent commit (@-) : qpvuntsm 0eae0dae a | (no description set)
1416 Parent commit (@-) : kkmpptxz eb61ba8d b | (no description set)
1417 Added 0 files, modified 1 files, removed 0 files
1418 Warning: There are unresolved conflicts at these paths:
1419 file 2-sided conflict
1420 [EOF]
1421 ");
1422 let output = work_dir.run_jj(["file", "show", "file", "-r", "a"]);
1423 insta::assert_snapshot!(output, @"
1424 CONTENT A
1425 [EOF]
1426 ");
1427 let output = work_dir.run_jj(["file", "show", "file", "-r", "b"]);
1428 insta::assert_snapshot!(output, @"
1429 CONTENT B
1430 [EOF]
1431 ");
1432 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
1433 insta::assert_snapshot!(output, @r"
1434 <<<<<<< conflict 1 of 1
1435 %%%%%%% diff from: zzzzzzzz 00000000
1436 \\\\\\\ to: qpvuntsm c8346c1c
1437 +CONTENT A
1438 +++++++ kkmpptxz 7c4518cb
1439 CONTENT B
1440 >>>>>>> conflict 1 of 1 ends
1441 [EOF]
1442 ");
1443 }
1444
1445 #[test]
1446 fn test_fix_resolve_conflict() {
1447 // If both sides of the conflict look the same after being fixed, the conflict
1448 // will be resolved.
1449 let mut test_env = TestEnvironment::default();
1450 set_up_fake_formatter(&mut test_env, &["--uppercase"]);
1451 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1452 let work_dir = test_env.work_dir("repo");
1453 work_dir.write_file("file", "Content\n");
1454 work_dir
1455 .run_jj(["bookmark", "create", "-r@", "a"])
1456 .success();
1457 work_dir.run_jj(["new", "@-"]).success();
1458 work_dir.write_file("file", "cOnTeNt\n");
1459 work_dir
1460 .run_jj(["bookmark", "create", "-r@", "b"])
1461 .success();
1462 work_dir.run_jj(["new", "a", "b"]).success();
1463
1464 // The conflicts are not different from the merged parent, so they would not be
1465 // fixed if we didn't fix the parents also.
1466 let output = work_dir.run_jj(["fix", "-s", "a", "-s", "b"]);
1467 insta::assert_snapshot!(output, @"
1468 ------- stderr -------
1469 Fixed 3 commits of 3 checked.
1470 Working copy (@) now at: mzvwutvl c4e4665e (empty) (no description set)
1471 Parent commit (@-) : qpvuntsm 7a0dbb95 a | (no description set)
1472 Parent commit (@-) : kkmpptxz 5d9510ab b | (no description set)
1473 Added 0 files, modified 1 files, removed 0 files
1474 [EOF]
1475 ");
1476 let output = work_dir.run_jj(["file", "show", "file", "-r", "@"]);
1477 insta::assert_snapshot!(output, @"
1478 CONTENT
1479 [EOF]
1480 ");
1481 }
1482
1483 #[test]
1484 fn test_all_files() {
1485 let test_env = TestEnvironment::default();
1486 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1487 let work_dir = test_env.work_dir("repo");
1488 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
1489 assert!(formatter_path.is_file());
1490 let formatter = to_toml_value(formatter_path.to_str().unwrap());
1491
1492 // Consider a few cases:
1493 // File A: in patterns, changed in child
1494 // File B: in patterns, NOT changed in child
1495 // File C: NOT in patterns, NOT changed in child
1496 // File D: NOT in patterns, changed in child
1497 // Some files will be in subdirectories to make sure we're covering that aspect
1498 // of matching.
1499 test_env.add_config(format!(
1500 r###"
1501 [fix.tools.tool]
1502 command = [{formatter}, "--append", "fixed"]
1503 patterns = ["a/a", "b/b"]
1504 "###,
1505 ));
1506
1507 work_dir.create_dir("a");
1508 work_dir.create_dir("b");
1509 work_dir.create_dir("c");
1510 work_dir.write_file("a/a", "parent aaa\n");
1511 work_dir.write_file("b/b", "parent bbb\n");
1512 work_dir.write_file("c/c", "parent ccc\n");
1513 work_dir.write_file("ddd", "parent ddd\n");
1514 work_dir.run_jj(["commit", "-m", "parent"]).success();
1515
1516 work_dir.write_file("a/a", "child aaa\n");
1517 work_dir.write_file("ddd", "child ddd\n");
1518 work_dir.run_jj(["describe", "-m", "child"]).success();
1519
1520 // Specifying files means exactly those files will be fixed in each revision,
1521 // although some like file C won't have any tools configured to make changes to
1522 // them. Specified but unfixed files are silently skipped, whether they lack
1523 // configuration, are ignored, don't exist, aren't normal files, etc.
1524 let output = work_dir.run_jj([
1525 "fix",
1526 "--include-unchanged-files",
1527 "b/b",
1528 "c/c",
1529 "does_not.exist",
1530 ]);
1531 insta::assert_snapshot!(output, @"
1532 ------- stderr -------
1533 Warning: No matching entries for paths: does_not.exist
1534 Fixed 2 commits of 2 checked.
1535 Working copy (@) now at: rlvkpnrz d8503fee child
1536 Parent commit (@-) : qpvuntsm 62c6ee98 parent
1537 Added 0 files, modified 1 files, removed 0 files
1538 [EOF]
1539 ");
1540
1541 let output = work_dir.run_jj(["file", "show", "a/a", "-r", "@-"]);
1542 insta::assert_snapshot!(output, @"
1543 parent aaa
1544 [EOF]
1545 ");
1546 let output = work_dir.run_jj(["file", "show", "b/b", "-r", "@-"]);
1547 insta::assert_snapshot!(output, @"
1548 parent bbb
1549 fixed[EOF]
1550 ");
1551 let output = work_dir.run_jj(["file", "show", "c/c", "-r", "@-"]);
1552 insta::assert_snapshot!(output, @"
1553 parent ccc
1554 [EOF]
1555 ");
1556 let output = work_dir.run_jj(["file", "show", "ddd", "-r", "@-"]);
1557 insta::assert_snapshot!(output, @"
1558 parent ddd
1559 [EOF]
1560 ");
1561
1562 let output = work_dir.run_jj(["file", "show", "a/a", "-r", "@"]);
1563 insta::assert_snapshot!(output, @"
1564 child aaa
1565 [EOF]
1566 ");
1567 let output = work_dir.run_jj(["file", "show", "b/b", "-r", "@"]);
1568 insta::assert_snapshot!(output, @"
1569 parent bbb
1570 fixed[EOF]
1571 ");
1572 let output = work_dir.run_jj(["file", "show", "c/c", "-r", "@"]);
1573 insta::assert_snapshot!(output, @"
1574 parent ccc
1575 [EOF]
1576 ");
1577 let output = work_dir.run_jj(["file", "show", "ddd", "-r", "@"]);
1578 insta::assert_snapshot!(output, @"
1579 child ddd
1580 [EOF]
1581 ");
1582
1583 // Not specifying files means all files will be fixed in each revision.
1584 let output = work_dir.run_jj(["fix", "--include-unchanged-files"]);
1585 insta::assert_snapshot!(output, @"
1586 ------- stderr -------
1587 Fixed 2 commits of 2 checked.
1588 Working copy (@) now at: rlvkpnrz 16aeb14c child
1589 Parent commit (@-) : qpvuntsm 5257b8ec parent
1590 Added 0 files, modified 2 files, removed 0 files
1591 [EOF]
1592 ");
1593
1594 let output = work_dir.run_jj(["file", "show", "a/a", "-r", "@-"]);
1595 insta::assert_snapshot!(output, @"
1596 parent aaa
1597 fixed[EOF]
1598 ");
1599 let output = work_dir.run_jj(["file", "show", "b/b", "-r", "@-"]);
1600 insta::assert_snapshot!(output, @"
1601 parent bbb
1602 fixedfixed[EOF]
1603 ");
1604 let output = work_dir.run_jj(["file", "show", "c/c", "-r", "@-"]);
1605 insta::assert_snapshot!(output, @"
1606 parent ccc
1607 [EOF]
1608 ");
1609 let output = work_dir.run_jj(["file", "show", "ddd", "-r", "@-"]);
1610 insta::assert_snapshot!(output, @"
1611 parent ddd
1612 [EOF]
1613 ");
1614
1615 let output = work_dir.run_jj(["file", "show", "a/a", "-r", "@"]);
1616 insta::assert_snapshot!(output, @"
1617 child aaa
1618 fixed[EOF]
1619 ");
1620 let output = work_dir.run_jj(["file", "show", "b/b", "-r", "@"]);
1621 insta::assert_snapshot!(output, @"
1622 parent bbb
1623 fixedfixed[EOF]
1624 ");
1625 let output = work_dir.run_jj(["file", "show", "c/c", "-r", "@"]);
1626 insta::assert_snapshot!(output, @"
1627 parent ccc
1628 [EOF]
1629 ");
1630 let output = work_dir.run_jj(["file", "show", "ddd", "-r", "@"]);
1631 insta::assert_snapshot!(output, @"
1632 child ddd
1633 [EOF]
1634 ");
1635 }
1636
1637 #[test]
1638 fn test_fix_with_line_ranges() {
1639 let test_env = TestEnvironment::default();
1640 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1641 let work_dir = test_env.work_dir("repo");
1642 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
1643 assert!(formatter_path.is_file());
1644 let formatter = to_toml_value(formatter_path.to_str().unwrap());
1645 test_env.add_config(format!(
1646 r###"
1647 [fix.tools.tool-1]
1648 command = [{formatter}, "--uppercase"]
1649 patterns = ["foo", "baz", "qux"]
1650 line-range-arg = "--line-ranges=$first-$last"
1651
1652 [fix.tools.tool-2]
1653 command = [{formatter}, "--lowercase"]
1654 patterns = ["bar", "baz"]
1655 line-range-arg = "--line-ranges=$first-$last"
1656 "###,
1657 ));
1658
1659 // Initial commit.
1660 work_dir.write_file("foo", "Foo1\nFoo2\nFoo3\n");
1661 work_dir.write_file("bar", "Bar1\nBar2\nBar3\n");
1662 work_dir.write_file("baz", "unmodified\n");
1663 work_dir
1664 .run_jj(["bookmark", "create", "-r@", "c1"])
1665 .success();
1666
1667 // Create new commit modifying foo and bar.
1668 work_dir.run_jj(["new"]).success();
1669 work_dir.write_file("foo", "Foo1\nFoo2-Mod\nFoo3\n");
1670 work_dir.write_file("bar", "Bar1-Mod\nBar2\nBar3\n");
1671 work_dir.write_file("baz", "unmodified\n");
1672 work_dir
1673 .run_jj(["bookmark", "create", "-r@", "c2"])
1674 .success();
1675
1676 // Commit c3 doing more modifications to foo and bar.
1677 work_dir.run_jj(["new"]).success();
1678 work_dir.write_file("foo", "Foo1\nFoo2\nFoo3-Mod\n");
1679 work_dir.remove_file("bar");
1680 work_dir.write_file("baz", "");
1681 work_dir.write_file("qux", "newfile\n");
1682 work_dir
1683 .run_jj(["bookmark", "create", "-r@", "c3"])
1684 .success();
1685
1686 // Run `jj fix` on the second commit.
1687 let output = work_dir.run_jj(["fix", "-s", "c2"]).success();
1688 insta::assert_snapshot!(output, @"
1689 ------- stderr -------
1690 Fixed 2 commits of 2 checked.
1691 Working copy (@) now at: mzvwutvl 7d52a6e8 c3 | (no description set)
1692 Parent commit (@-) : kkmpptxz a8e4c2e8 c2 | (no description set)
1693 Added 0 files, modified 2 files, removed 0 files
1694 [EOF]
1695 ");
1696
1697 // Check that the formatter was not applied to the first commit.
1698 let output = work_dir.run_jj(["file", "show", "foo", "-r", "c1"]);
1699 insta::assert_snapshot!(output, @r"
1700 Foo1
1701 Foo2
1702 Foo3
1703 [EOF]
1704 ");
1705 let output = work_dir.run_jj(["file", "show", "bar", "-r", "c1"]);
1706 insta::assert_snapshot!(output, @r"
1707 Bar1
1708 Bar2
1709 Bar3
1710 [EOF]
1711 ");
1712 let output = work_dir.run_jj(["file", "show", "baz", "-r", "c1"]);
1713 insta::assert_snapshot!(output, @r"
1714 unmodified
1715 [EOF]
1716 ");
1717
1718 // Check that the formatter was applied to the second commit.
1719 let output = work_dir.run_jj(["file", "show", "foo", "-r", "c2"]);
1720 insta::assert_snapshot!(output, @r"
1721 Foo1
1722 FOO2-MOD
1723 Foo3
1724 [EOF]
1725 ");
1726 let output = work_dir.run_jj(["file", "show", "bar", "-r", "c2"]);
1727 insta::assert_snapshot!(output, @r"
1728 bar1-mod
1729 Bar2
1730 Bar3
1731 [EOF]
1732 ");
1733 let output = work_dir.run_jj(["file", "show", "baz", "-r", "c2"]);
1734 insta::assert_snapshot!(output, @r"
1735 unmodified
1736 [EOF]
1737 ");
1738
1739 // Check that the formatter was applied to the third commit but in different
1740 // line ranges.
1741 let output = work_dir.run_jj(["file", "show", "foo", "-r", "c3"]);
1742 insta::assert_snapshot!(output, @r"
1743 Foo1
1744 Foo2
1745 FOO3-MOD
1746 [EOF]
1747 ");
1748 let output = work_dir.run_jj(["file", "show", "baz", "-r", "c3"]);
1749 insta::assert_snapshot!(output, @"");
1750 let output = work_dir.run_jj(["file", "show", "qux", "-r", "c3"]);
1751 insta::assert_snapshot!(output, @r"
1752 NEWFILE
1753 [EOF]
1754 ");
1755 }
1756
1757 #[test]
1758 fn test_fix_with_run_tool_if_zero_line_ranges() {
1759 let test_env = TestEnvironment::default();
1760 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1761 let work_dir = test_env.work_dir("repo");
1762 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
1763 assert!(formatter_path.is_file());
1764 let formatter = to_toml_value(formatter_path.to_str().unwrap());
1765 test_env.add_config(format!(
1766 r###"
1767 [fix.tools.tool-1]
1768 command = [{formatter}, "--uppercase"]
1769 patterns = ["foo", "baz"]
1770 line-range-arg = "--line-ranges=$first-$last"
1771 run-tool-if-zero-line-ranges = true
1772
1773 [fix.tools.tool-2]
1774 command = [{formatter}, "--lowercase"]
1775 patterns = ["bar"]
1776 line-range-arg = "--line-ranges=$first-$last"
1777 run-tool-if-zero-line-ranges = false
1778
1779 [fix.tools.tool-3]
1780 command = [{formatter}, "--uppercase", "--split-even-length-lines"]
1781 patterns = ["qux"]
1782 line-range-arg = "--line-ranges=$first-$last"
1783 run-tool-if-zero-line-ranges = true
1784 "###,
1785 ));
1786
1787 // Initial commit.
1788 work_dir.write_file("foo", "Foo1\nFoo2\nFoo3\n");
1789 work_dir.write_file("bar", "Bar1\nBar2\nBar3\n");
1790 work_dir.write_file("qux", "Qux1\nQux2\nQux3\n");
1791 work_dir.write_file("baz", "unmodified\n");
1792 work_dir
1793 .run_jj(["bookmark", "create", "-r@", "c1"])
1794 .success();
1795
1796 // Create a new commit doing a deletion of a line in foo and bar (which should
1797 // produce empty line ranges).
1798 work_dir.run_jj(["new"]).success();
1799 work_dir.write_file("foo", "Foo1\nFoo3\n");
1800 work_dir.write_file("bar", "Bar1\nBar3\n");
1801 work_dir.write_file("baz", "unmodified\n");
1802 work_dir
1803 .run_jj(["bookmark", "create", "-r@", "c2"])
1804 .success();
1805
1806 // Run `jj fix` on the second commit.
1807 let output = work_dir.run_jj(["fix", "-s", "c2"]).success();
1808 insta::assert_snapshot!(output, @"
1809 ------- stderr -------
1810 Fixed 1 commits of 1 checked.
1811 Working copy (@) now at: kkmpptxz 4613d090 c2 | (no description set)
1812 Parent commit (@-) : qpvuntsm 074c5dba c1 | (no description set)
1813 Added 0 files, modified 1 files, removed 0 files
1814 [EOF]
1815 ");
1816
1817 // Check that the formatter was not applied to the first commit.
1818 let output = work_dir.run_jj(["file", "show", "foo", "-r", "c1"]);
1819 insta::assert_snapshot!(output, @r"
1820 Foo1
1821 Foo2
1822 Foo3
1823 [EOF]
1824 ");
1825 let output = work_dir.run_jj(["file", "show", "bar", "-r", "c1"]);
1826 insta::assert_snapshot!(output, @r"
1827 Bar1
1828 Bar2
1829 Bar3
1830 [EOF]
1831 ");
1832 let output = work_dir.run_jj(["file", "show", "baz", "-r", "c1"]);
1833 insta::assert_snapshot!(output, @r"
1834 unmodified
1835 [EOF]
1836 ");
1837
1838 // Check that the formatter was applied to the second commit where
1839 // run-tool-if-zero-line-ranges is true.
1840 let output = work_dir.run_jj(["file", "show", "foo", "-r", "c2"]);
1841 insta::assert_snapshot!(output, @r"
1842 FOO1
1843 FOO3
1844 [EOF]
1845 ");
1846 let output = work_dir.run_jj(["file", "show", "bar", "-r", "c2"]);
1847 insta::assert_snapshot!(output, @r"
1848 Bar1
1849 Bar3
1850 [EOF]
1851 ");
1852 let output = work_dir.run_jj(["file", "show", "baz", "-r", "c2"]);
1853 insta::assert_snapshot!(output, @r"
1854 unmodified
1855 [EOF]
1856 ");
1857
1858 // Create a new commit doing a deletion of a line in qux. This commit's parent
1859 // is c1.
1860 work_dir.run_jj(["new", "-r", "c1"]).success();
1861 work_dir.write_file("qux", "Qux1\nQux3\n");
1862 work_dir
1863 .run_jj(["bookmark", "create", "-r@", "c3"])
1864 .success();
1865
1866 // Run `jj fix` on the third commit. This command should pass, but the
1867 // fake-formatter should fail to format qux because `--split-even-length-lines`
1868 // requires line-ranges to be passed into the fake formatter and qux should
1869 // not have any line ranges passed due to run-tool-if-zero-line-ranges.
1870 // This should result in no changes to qux.
1871 let output = work_dir.run_jj(["fix", "-s", "c3"]).success();
1872 let output = output.normalize_stderr_with(|s| {
1873 s.replace(formatter_path.to_str().unwrap(), "fake-formatter")
1874 .replace(".exe", "")
1875 });
1876 insta::assert_snapshot!(output, @"
1877 ------- stderr -------
1878 qux:
1879 error: the following required arguments were not provided:
1880 --line-ranges <LINE_RANGES>
1881
1882 Usage: fake-formatter --line-ranges <LINE_RANGES> --uppercase --split-even-length-lines
1883
1884 For more information, try '--help'.
1885
1886 Warning: Fix tool `fake-formatter` exited with non-zero exit code for `qux`
1887 Fixed 0 commits of 1 checked.
1888 Nothing changed.
1889 [EOF]
1890 ");
1891
1892 // Check that the formatter was not applied to the third commit.
1893 let output = work_dir.run_jj(["file", "show", "qux", "-r", "c3"]);
1894 insta::assert_snapshot!(output, @r"
1895 Qux1
1896 Qux3
1897 [EOF]
1898 ");
1899 }
1900
1901 #[test]
1902 fn test_fix_with_all_lines_arg() {
1903 let test_env = TestEnvironment::default();
1904 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1905 let work_dir = test_env.work_dir("repo");
1906 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
1907 assert!(formatter_path.is_file());
1908 let formatter = to_toml_value(formatter_path.to_str().unwrap());
1909 test_env.add_config(format!(
1910 r###"
1911 [fix.tools.tool-1]
1912 command = [{formatter}, "--uppercase"]
1913 patterns = ["foo", "baz", "qux"]
1914 line-range-arg = "--line-ranges=$first-$last"
1915
1916 [fix.tools.tool-2]
1917 command = [{formatter}, "--lowercase"]
1918 patterns = ["bar"]
1919 "###,
1920 ));
1921
1922 // Initial commit.
1923 work_dir.write_file("foo", "Foo1\nFoo2\nFoo3\n");
1924 work_dir.write_file("bar", "Bar1\nBar2\nBar3\n");
1925 work_dir.write_file("baz", "unmodified\n");
1926 work_dir
1927 .run_jj(["bookmark", "create", "-r@", "c1"])
1928 .success();
1929
1930 // Create new commit modifying foo and bar.
1931 work_dir.run_jj(["new"]).success();
1932 work_dir.write_file("foo", "Foo1\nFoo2-Mod\nFoo3\n");
1933 work_dir.write_file("bar", "Bar1\nBar2-Mod\nBar3\n");
1934 work_dir.write_file("baz", "unmodified\n");
1935 work_dir.write_file("qux", "newfile\n");
1936 work_dir
1937 .run_jj(["bookmark", "create", "-r@", "c2"])
1938 .success();
1939
1940 // Run `jj fix` on the second commit with --all-lines arg.
1941 let output = work_dir
1942 .run_jj(["fix", "--all-lines", "-s", "c2"])
1943 .success();
1944 insta::assert_snapshot!(output, @"
1945 ------- stderr -------
1946 Fixed 1 commits of 1 checked.
1947 Working copy (@) now at: kkmpptxz d875fd1f c2 | (no description set)
1948 Parent commit (@-) : qpvuntsm 81234495 c1 | (no description set)
1949 Added 0 files, modified 3 files, removed 0 files
1950 [EOF]
1951 ");
1952
1953 // Check that the formatter was not applied to the first commit.
1954 let output = work_dir.run_jj(["file", "show", "foo", "-r", "c1"]);
1955 insta::assert_snapshot!(output, @r"
1956 Foo1
1957 Foo2
1958 Foo3
1959 [EOF]
1960 ");
1961 let output = work_dir.run_jj(["file", "show", "bar", "-r", "c1"]);
1962 insta::assert_snapshot!(output, @r"
1963 Bar1
1964 Bar2
1965 Bar3
1966 [EOF]
1967 ");
1968 let output = work_dir.run_jj(["file", "show", "baz", "-r", "c1"]);
1969 insta::assert_snapshot!(output, @r"
1970 unmodified
1971 [EOF]
1972 ");
1973
1974 // Check that the formatter was applied to the second commit. And that the
1975 // whole-file was formatted for changed files. And whole file was formatted
1976 // for formatters that don't support line ranges.
1977 let output = work_dir.run_jj(["file", "show", "foo", "-r", "c2"]);
1978 insta::assert_snapshot!(output, @r"
1979 FOO1
1980 FOO2-MOD
1981 FOO3
1982 [EOF]
1983 ");
1984 let output = work_dir.run_jj(["file", "show", "bar", "-r", "c2"]);
1985 insta::assert_snapshot!(output, @r"
1986 bar1
1987 bar2-mod
1988 bar3
1989 [EOF]
1990 ");
1991 let output = work_dir.run_jj(["file", "show", "baz", "-r", "c2"]);
1992 insta::assert_snapshot!(output, @r"
1993 unmodified
1994 [EOF]
1995 ");
1996 let output = work_dir.run_jj(["file", "show", "qux", "-r", "c2"]);
1997 insta::assert_snapshot!(output, @r"
1998 NEWFILE
1999 [EOF]
2000 ");
2001 }
2002
2003 #[test]
2004 fn test_fix_with_line_ranges_multiple_formatters() {
2005 let test_env = TestEnvironment::default();
2006 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2007 let work_dir = test_env.work_dir("repo");
2008 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
2009 assert!(formatter_path.is_file());
2010 let formatter = to_toml_value(formatter_path.to_str().unwrap());
2011 test_env.add_config(format!(
2012 r###"
2013 [fix.tools.tool-1]
2014 command = [{formatter}, "--split-even-length-lines"]
2015 patterns = ["foo", "boo"]
2016 line-range-arg = "--line-ranges=$first-$last"
2017
2018 [fix.tools.tool-2]
2019 command = [{formatter}, "--uppercase"]
2020 patterns = ["foo", "boo"]
2021 line-range-arg = "--line-ranges=$first-$last"
2022 "###,
2023 ));
2024
2025 // Initial commit.
2026 work_dir.write_file("foo", "ab\ncd\nefg\n");
2027 work_dir
2028 .run_jj(["bookmark", "create", "-r@", "c1"])
2029 .success();
2030
2031 // Create new commit to deal with split even lines and uppercase.
2032 work_dir.run_jj(["new"]).success();
2033 work_dir.write_file("foo", "abcd\nefgh\n");
2034 work_dir.write_file("boo", "abcd\nefgh\n");
2035 work_dir
2036 .run_jj(["bookmark", "create", "-r@", "c2"])
2037 .success();
2038
2039 // Run `jj fix` on the second commit.
2040 let output = work_dir.run_jj(["fix", "-s", "c2"]).success();
2041 insta::assert_snapshot!(output, @"
2042 ------- stderr -------
2043 Fixed 1 commits of 1 checked.
2044 Working copy (@) now at: kkmpptxz 4c5f12f5 c2 | (no description set)
2045 Parent commit (@-) : qpvuntsm 37f1e5c6 c1 | (no description set)
2046 Added 0 files, modified 2 files, removed 0 files
2047 [EOF]
2048 ");
2049
2050 // Check that the formatter was not applied to the first commit.
2051 let output = work_dir.run_jj(["file", "show", "foo", "-r", "c1"]);
2052 insta::assert_snapshot!(output, @r"
2053 ab
2054 cd
2055 efg
2056 [EOF]
2057 ");
2058
2059 // Check that split lines was applied first and then upper was only applied to
2060 // the second half.
2061 let output = work_dir.run_jj(["file", "show", "foo", "-r", "c2"]);
2062 insta::assert_snapshot!(output, @r"
2063 ab
2064 cd
2065 EF
2066 GH
2067 [EOF]
2068 ");
2069 let output = work_dir.run_jj(["file", "show", "boo", "-r", "c2"]);
2070 insta::assert_snapshot!(output, @r"
2071 AB
2072 CD
2073 EF
2074 GH
2075 [EOF]
2076 ");
2077 }
2078
2079 #[test]
2080 fn test_fix_with_line_ranges_and_include_unchanged_files_all_lines() {
2081 let test_env = TestEnvironment::default();
2082 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2083 let work_dir = test_env.work_dir("repo");
2084 let formatter_path = assert_cmd::cargo::cargo_bin!("fake-formatter");
2085 assert!(formatter_path.is_file());
2086 let formatter = to_toml_value(formatter_path.to_str().unwrap());
2087 test_env.add_config(format!(
2088 r###"
2089 [fix.tools.tool-1]
2090 command = [{formatter}, "--uppercase"]
2091 patterns = ["all()"]
2092 line-range-arg = "--line-ranges=$first-$last"
2093 "###,
2094 ));
2095
2096 // Initial commit.
2097 work_dir.write_file("changed.txt", "Foo1\nFoo2\nFoo3\n");
2098 work_dir.write_file("unchanged.txt", "baz1\nbaz2\nbaz3\n");
2099 work_dir.write_file("empty.txt", "content\n");
2100 work_dir
2101 .run_jj(["bookmark", "create", "-r@", "c1"])
2102 .success();
2103
2104 // Create new commit modifying changed.txt.
2105 work_dir.run_jj(["new"]).success();
2106 work_dir.write_file("changed.txt", "Foo1\nFoo2-Mod\nFoo3\n");
2107 work_dir.write_file("unchanged.txt", "baz1\nbaz2\nbaz3\n");
2108 work_dir.write_file("newfile.txt", "new\n");
2109 work_dir.write_file("empty.txt", "");
2110 work_dir
2111 .run_jj(["bookmark", "create", "-r@", "c2"])
2112 .success();
2113
2114 // Run `jj fix` on the second commit, including unchanged files.
2115 let output = work_dir
2116 .run_jj([
2117 "fix",
2118 "--include-unchanged-files",
2119 "--all-lines",
2120 "-s",
2121 "c2",
2122 ])
2123 .success();
2124 insta::assert_snapshot!(output, @"
2125 ------- stderr -------
2126 Fixed 1 commits of 1 checked.
2127 Working copy (@) now at: kkmpptxz 1fb2bf8f c2 | (no description set)
2128 Parent commit (@-) : qpvuntsm 42db21c2 c1 | (no description set)
2129 Added 0 files, modified 3 files, removed 0 files
2130 [EOF]
2131 ");
2132
2133 // Check that the formatter was not applied to the first commit.
2134 let output = work_dir.run_jj(["file", "show", "changed.txt", "-r", "c1"]);
2135 insta::assert_snapshot!(output, @r"
2136 Foo1
2137 Foo2
2138 Foo3
2139 [EOF]
2140 ");
2141 let output = work_dir.run_jj(["file", "show", "unchanged.txt", "-r", "c1"]);
2142 insta::assert_snapshot!(output, @r"
2143 baz1
2144 baz2
2145 baz3
2146 [EOF]
2147 ");
2148 let output = work_dir.run_jj(["file", "show", "empty.txt", "-r", "c1"]);
2149 insta::assert_snapshot!(output, @r"
2150 content
2151 [EOF]
2152 ");
2153
2154 // Check that the formatter was applied to the second commit and all lines were
2155 // affected.
2156 let output = work_dir.run_jj(["file", "show", "changed.txt", "-r", "c2"]);
2157 insta::assert_snapshot!(output, @"
2158 FOO1
2159 FOO2-MOD
2160 FOO3
2161 [EOF]
2162 ");
2163 let output = work_dir.run_jj(["file", "show", "unchanged.txt", "-r", "c2"]);
2164 insta::assert_snapshot!(output, @r"
2165 BAZ1
2166 BAZ2
2167 BAZ3
2168 [EOF]
2169 ");
2170 let output = work_dir.run_jj(["file", "show", "newfile.txt", "-r", "c2"]);
2171 insta::assert_snapshot!(output, @r"
2172 NEW
2173 [EOF]
2174 ");
2175 let output = work_dir.run_jj(["file", "show", "empty.txt", "-r", "c2"]);
2176 insta::assert_snapshot!(output, @r"");
2177 }