Debian Codegraph @

cli/tests/test_commit_command.rs

raw
1 // Copyright 2022 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use std::path::PathBuf;
16
17 use testutils::TestResult;
18
19 use crate::common::CommandOutput;
20 use crate::common::TestEnvironment;
21 use crate::common::TestWorkDir;
22
23 #[test]
24 fn test_commit_with_description_from_cli() {
25 let test_env = TestEnvironment::default();
26 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
27 let work_dir = test_env.work_dir("repo");
28
29 // Description applies to the current working-copy (not the new one)
30 work_dir.run_jj(["commit", "-m=first"]).success();
31 insta::assert_snapshot!(get_log_output(&work_dir), @"
32 @ eb9fd2ab82e7
33 ○ 68a505386f93 first
34 ◆ 000000000000
35 [EOF]
36 ");
37 }
38
39 #[test]
40 fn test_commit_with_editor() -> TestResult {
41 let mut test_env = TestEnvironment::default();
42 let edit_script = test_env.set_up_fake_editor();
43 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
44 let work_dir = test_env.work_dir("repo");
45
46 // Check that the text file gets initialized with the current description and
47 // set a new one
48 work_dir.run_jj(["describe", "-m=initial"]).success();
49 std::fs::write(&edit_script, ["dump editor0", "write\nmodified"].join("\0"))?;
50 work_dir.run_jj(["commit"]).success();
51 insta::assert_snapshot!(get_log_output(&work_dir), @"
52 @ 2094c8f2e360
53 ○ a7ba1eb73836 modified
54 ◆ 000000000000
55 [EOF]
56 ");
57 insta::assert_snapshot!(
58 std::fs::read_to_string(test_env.env_root().join("editor0"))?, @r#"
59 initial
60
61 JJ: Change ID: qpvuntsm
62 JJ:
63 JJ: Lines starting with "JJ:" (like this one) will be removed.
64 "#);
65
66 // Check that the editor content includes diff summary
67 work_dir.write_file("file1", "foo\n");
68 work_dir.write_file("file2", "foo\n");
69 work_dir.run_jj(["describe", "-m=add files"]).success();
70 std::fs::write(&edit_script, "dump editor1")?;
71 work_dir.run_jj(["commit"]).success();
72 insta::assert_snapshot!(
73 std::fs::read_to_string(test_env.env_root().join("editor1"))?, @r#"
74 add files
75
76 JJ: Change ID: kkmpptxz
77 JJ: This commit contains the following changes:
78 JJ: A file1
79 JJ: A file2
80 JJ:
81 JJ: Lines starting with "JJ:" (like this one) will be removed.
82 "#);
83 Ok(())
84 }
85
86 #[test]
87 fn test_commit_with_editor_avoids_unc() -> TestResult {
88 let mut test_env = TestEnvironment::default();
89 let edit_script = test_env.set_up_fake_editor();
90 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
91 let work_dir = test_env.work_dir("repo");
92
93 std::fs::write(edit_script, "dump-path path")?;
94 work_dir.run_jj(["commit"]).success();
95
96 let edited_path = PathBuf::from(std::fs::read_to_string(test_env.env_root().join("path"))?);
97 // While `assert!(!edited_path.starts_with("//?/"))` could work here in most
98 // cases, it fails when it is not safe to strip the prefix, such as paths
99 // over 260 chars.
100 assert_eq!(edited_path, dunce::simplified(&edited_path));
101 Ok(())
102 }
103
104 #[test]
105 fn test_commit_with_empty_description_from_cli() {
106 let test_env = TestEnvironment::default();
107 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
108 let work_dir = test_env.work_dir("repo");
109
110 let output = work_dir.run_jj(["commit", "-m="]).success();
111 insta::assert_snapshot!(get_log_output(&work_dir), @"
112 @ 51b556e22ca0
113 ○ cc8ff2284a8c
114 ◆ 000000000000
115 [EOF]
116 ");
117 insta::assert_snapshot!(output, @"
118 ------- stderr -------
119 Working copy (@) now at: rlvkpnrz 51b556e2 (empty) (no description set)
120 Parent commit (@-) : qpvuntsm cc8ff228 (empty) (no description set)
121 [EOF]
122 ");
123 }
124
125 #[test]
126 fn test_commit_with_empty_description_from_editor() -> TestResult {
127 let mut test_env = TestEnvironment::default();
128 let edit_script = test_env.set_up_fake_editor();
129 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
130 let work_dir = test_env.work_dir("repo");
131
132 // Check that the text file gets initialized and leave it untouched.
133 std::fs::write(&edit_script, ["dump editor0"].join("\0"))?;
134 let output = work_dir.run_jj(["commit"]).success();
135 insta::assert_snapshot!(get_log_output(&work_dir), @"
136 @ 51b556e22ca0
137 ○ cc8ff2284a8c
138 ◆ 000000000000
139 [EOF]
140 ");
141 insta::assert_snapshot!(
142 std::fs::read_to_string(test_env.env_root().join("editor0"))?,
143 @r#"
144
145
146 JJ: Change ID: qpvuntsm
147 JJ:
148 JJ: Lines starting with "JJ:" (like this one) will be removed.
149 "#);
150 insta::assert_snapshot!(output, @"
151 ------- stderr -------
152 Hint: The commit message was left empty.
153 If this was not intentional, run `jj undo` to restore the previous state.
154 Or run `jj desc @-` to add a description to the parent commit.
155 Working copy (@) now at: rlvkpnrz 51b556e2 (empty) (no description set)
156 Parent commit (@-) : qpvuntsm cc8ff228 (empty) (no description set)
157 [EOF]
158 ");
159 Ok(())
160 }
161
162 #[test]
163 fn test_commit_interactive() -> TestResult {
164 let mut test_env = TestEnvironment::default();
165 let edit_script = test_env.set_up_fake_editor();
166 let diff_editor = test_env.set_up_fake_diff_editor();
167 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
168 let work_dir = test_env.work_dir("repo");
169
170 work_dir.write_file("file1", "foo\n");
171 work_dir.write_file("file2", "bar\n");
172 work_dir.run_jj(["describe", "-m=add files"]).success();
173 std::fs::write(edit_script, ["dump editor"].join("\0"))?;
174
175 let diff_script = ["rm file2", "dump JJ-INSTRUCTIONS instrs"].join("\0");
176 std::fs::write(diff_editor, diff_script)?;
177 let setup_opid = work_dir.current_operation_id();
178
179 // Create a commit interactively and select only file1
180 work_dir.run_jj(["commit", "-i"]).success();
181
182 insta::assert_snapshot!(
183 std::fs::read_to_string(test_env.env_root().join("instrs"))?, @"
184 You are splitting the working-copy commit: qpvuntsm d849dc34 add files
185
186 The diff initially shows all changes. Adjust the right side until it shows the
187 contents you want for the first commit. The remainder will be included in the
188 new working-copy commit.
189 ");
190
191 insta::assert_snapshot!(
192 std::fs::read_to_string(test_env.env_root().join("editor"))?, @r#"
193 add files
194
195 JJ: Change ID: qpvuntsm
196 JJ: This commit contains the following changes:
197 JJ: A file1
198 JJ:
199 JJ: Lines starting with "JJ:" (like this one) will be removed.
200 "#);
201
202 // Try again with --tool=<name>, which implies --interactive
203 work_dir.run_jj(["op", "restore", &setup_opid]).success();
204 work_dir
205 .run_jj([
206 "commit",
207 "--config=ui.diff-editor='false'",
208 "--tool=fake-diff-editor",
209 ])
210 .success();
211
212 insta::assert_snapshot!(
213 std::fs::read_to_string(test_env.env_root().join("editor"))?, @r#"
214 add files
215
216 JJ: Change ID: qpvuntsm
217 JJ: This commit contains the following changes:
218 JJ: A file1
219 JJ:
220 JJ: Lines starting with "JJ:" (like this one) will be removed.
221 "#);
222
223 let output = work_dir.run_jj(["log", "--summary"]);
224 insta::assert_snapshot!(output, @"
225 @ mzvwutvl [email protected] 2001-02-03 08:05:11 9b0176ab
226 │ (no description set)
227 │ A file2
228 ○ qpvuntsm [email protected] 2001-02-03 08:05:11 6e6fa925
229 │ add files
230 │ A file1
231 ◆ zzzzzzzz root() 00000000
232 [EOF]
233 ");
234 Ok(())
235 }
236
237 #[test]
238 fn test_commit_interactive_with_paths() -> TestResult {
239 let mut test_env = TestEnvironment::default();
240 let edit_script = test_env.set_up_fake_editor();
241 let diff_editor = test_env.set_up_fake_diff_editor();
242 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
243 let work_dir = test_env.work_dir("repo");
244
245 work_dir.write_file("file2", "");
246 work_dir.write_file("file3", "");
247 work_dir.run_jj(["new", "-medit"]).success();
248 work_dir.write_file("file1", "foo\n");
249 work_dir.write_file("file2", "bar\n");
250 work_dir.write_file("file3", "baz\n");
251
252 std::fs::write(edit_script, ["dump editor"].join("\0"))?;
253 let diff_script = [
254 "files-before file2",
255 "files-after JJ-INSTRUCTIONS file1 file2",
256 "reset file2",
257 ]
258 .join("\0");
259 std::fs::write(diff_editor, diff_script)?;
260
261 // Select file1 and file2 by args, then select file1 interactively
262 let output = work_dir.run_jj(["commit", "-i", "file1", "file2"]);
263 insta::assert_snapshot!(output, @"
264 ------- stderr -------
265 Working copy (@) now at: kkmpptxz 50f426df (no description set)
266 Parent commit (@-) : rlvkpnrz eb640375 edit
267 [EOF]
268 ");
269
270 insta::assert_snapshot!(
271 std::fs::read_to_string(test_env.env_root().join("editor"))?, @r#"
272 edit
273
274 JJ: Change ID: rlvkpnrz
275 JJ: This commit contains the following changes:
276 JJ: A file1
277 JJ:
278 JJ: Lines starting with "JJ:" (like this one) will be removed.
279 "#);
280
281 let output = work_dir.run_jj(["log", "--summary"]);
282 insta::assert_snapshot!(output, @"
283 @ kkmpptxz [email protected] 2001-02-03 08:05:09 50f426df
284 │ (no description set)
285 │ M file2
286 │ M file3
287 ○ rlvkpnrz [email protected] 2001-02-03 08:05:09 eb640375
288 │ edit
289 │ A file1
290 ○ qpvuntsm [email protected] 2001-02-03 08:05:08 ff687a2f
291 │ (no description set)
292 │ A file2
293 │ A file3
294 ◆ zzzzzzzz root() 00000000
295 [EOF]
296 ");
297 Ok(())
298 }
299
300 #[test]
301 fn test_commit_with_default_description() -> TestResult {
302 let mut test_env = TestEnvironment::default();
303 let edit_script = test_env.set_up_fake_editor();
304 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
305 test_env.add_config(r#"template-aliases.default_commit_description = '"\n\nTESTED=TODO\n"'"#);
306 let work_dir = test_env.work_dir("repo");
307
308 work_dir.write_file("file1", "foo\n");
309 work_dir.write_file("file2", "bar\n");
310 std::fs::write(edit_script, ["dump editor"].join("\0"))?;
311 work_dir.run_jj(["commit"]).success();
312
313 insta::assert_snapshot!(get_log_output(&work_dir), @"
314 @ cba559ac1a48
315 ○ 7276dfff8027 TESTED=TODO
316 ◆ 000000000000
317 [EOF]
318 ");
319 insta::assert_snapshot!(
320 std::fs::read_to_string(test_env.env_root().join("editor"))?, @r#"
321
322
323 TESTED=TODO
324
325 JJ: Change ID: qpvuntsm
326 JJ: This commit contains the following changes:
327 JJ: A file1
328 JJ: A file2
329 JJ:
330 JJ: Lines starting with "JJ:" (like this one) will be removed.
331 "#);
332 Ok(())
333 }
334
335 #[test]
336 fn test_commit_with_description_template() -> TestResult {
337 let mut test_env = TestEnvironment::default();
338 let edit_script = test_env.set_up_fake_editor();
339 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
340 test_env.add_config(
341 r#"
342 [templates]
343 draft_commit_description = '''
344 concat(
345 description,
346 "\n",
347 indent(
348 "JJ: ",
349 concat(
350 "Author: " ++ format_detailed_signature(author) ++ "\n",
351 "Committer: " ++ format_detailed_signature(committer) ++ "\n",
352 "\n",
353 diff.stat(76),
354 ),
355 ),
356 )
357 '''
358 "#,
359 );
360 let work_dir = test_env.work_dir("repo");
361
362 std::fs::write(edit_script, ["dump editor"].join("\0"))?;
363
364 work_dir.write_file("file1", "foo\n");
365 work_dir.write_file("file2", "bar\n");
366
367 // Only file1 should be included in the diff
368 work_dir.run_jj(["commit", "file1"]).success();
369 insta::assert_snapshot!(
370 std::fs::read_to_string(test_env.env_root().join("editor"))?, @r#"
371
372 JJ: Author: Test User <[email protected]> (2001-02-03 08:05:08)
373 JJ: Committer: Test User <[email protected]> (2001-02-03 08:05:08)
374
375 JJ: file1 | 1 +
376 JJ: 1 file changed, 1 insertion(+), 0 deletions(-)
377 JJ:
378 JJ: Lines starting with "JJ:" (like this one) will be removed.
379 "#);
380
381 Ok(())
382 }
383
384 #[test]
385 fn test_commit_without_working_copy() {
386 let test_env = TestEnvironment::default();
387 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
388 let work_dir = test_env.work_dir("repo");
389
390 work_dir.run_jj(["workspace", "forget"]).success();
391 let output = work_dir.run_jj(["commit", "-m=first"]);
392 insta::assert_snapshot!(output, @"
393 ------- stderr -------
394 Error: This command requires a working copy
395 [EOF]
396 [exit status: 1]
397 ");
398 }
399
400 #[test]
401 fn test_commit_paths() {
402 let test_env = TestEnvironment::default();
403 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
404 let work_dir = test_env.work_dir("repo");
405
406 work_dir.write_file("file1", "foo\n");
407 work_dir.write_file("file2", "bar\n");
408
409 work_dir.run_jj(["commit", "-m=first", "file1"]).success();
410 let output = work_dir.run_jj(["diff", "-r", "@-"]);
411 insta::assert_snapshot!(output, @"
412 Added regular file file1:
413 1: foo
414 [EOF]
415 ");
416
417 let output = work_dir.run_jj(["diff"]);
418 insta::assert_snapshot!(output, @"
419 Added regular file file2:
420 1: bar
421 [EOF]
422 ");
423 }
424
425 #[test]
426 fn test_commit_paths_warning() {
427 let test_env = TestEnvironment::default();
428 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
429 let work_dir = test_env.work_dir("repo");
430
431 work_dir.write_file("file1", "foo\n");
432 work_dir.write_file("file2", "bar\n");
433
434 let output = work_dir.run_jj(["commit", "-m=first", "file3"]);
435 insta::assert_snapshot!(output, @"
436 ------- stderr -------
437 Warning: The given paths do not match any file: file3
438 Working copy (@) now at: rlvkpnrz 4c6f0146 (no description set)
439 Parent commit (@-) : qpvuntsm 68a50538 (empty) first
440 [EOF]
441 ");
442
443 let output = work_dir.run_jj(["diff"]);
444 insta::assert_snapshot!(output, @"
445 Added regular file file1:
446 1: foo
447 Added regular file file2:
448 1: bar
449 [EOF]
450 ");
451 }
452
453 #[test]
454 fn test_commit_trailers() -> TestResult {
455 let mut test_env = TestEnvironment::default();
456 let edit_script = test_env.set_up_fake_editor();
457 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
458 let work_dir = test_env.work_dir("repo");
459 test_env.add_config(
460 r#"[templates]
461 commit_trailers = '''"Reviewed-by: " ++ self.committer().email()'''"#,
462 );
463 work_dir.write_file("file1", "foo\n");
464
465 let output = work_dir.run_jj(["commit", "-m=first"]);
466 insta::assert_snapshot!(output, @"
467 ------- stderr -------
468 Working copy (@) now at: rlvkpnrz 0c0495f3 (empty) (no description set)
469 Parent commit (@-) : qpvuntsm ae86ffd4 first
470 [EOF]
471 ");
472
473 let output = work_dir.run_jj(["log", "--no-graph", "-r@-", "-Tdescription"]);
474 insta::assert_snapshot!(output, @"
475 first
476
477 Reviewed-by: [email protected]
478 [EOF]
479 ");
480
481 // the new committer should appear in the trailer
482 let output = work_dir.run_jj(["commit", "[email protected]", "-m=second"]);
483 insta::assert_snapshot!(output, @"
484 ------- stderr -------
485 Working copy (@) now at: zsuskuln fd73eac2 (empty) (no description set)
486 Parent commit (@-) : rlvkpnrz 6e69e833 (empty) second
487 [EOF]
488 ");
489
490 let output = work_dir.run_jj(["log", "--no-graph", "-r@-", "-Tdescription"]);
491 insta::assert_snapshot!(output, @"
492 second
493
494 Reviewed-by: [email protected]
495 [EOF]
496 ");
497
498 // the trailer is added in the editor
499 std::fs::write(&edit_script, "dump editor0")?;
500 let output = work_dir.run_jj(["commit", "[email protected]"]);
501 insta::assert_snapshot!(output, @"
502 ------- stderr -------
503 Working copy (@) now at: royxmykx dac9709c (empty) (no description set)
504 Parent commit (@-) : zsuskuln d9ced309 (empty) Reviewed-by: [email protected]
505 [EOF]
506 ");
507
508 let editor0 = std::fs::read_to_string(test_env.env_root().join("editor0"))?;
509 insta::assert_snapshot!(
510 format!("-----\n{editor0}-----\n"), @r#"
511 -----
512
513
514 Reviewed-by: [email protected]
515
516 JJ: Change ID: zsuskuln
517 JJ:
518 JJ: Lines starting with "JJ:" (like this one) will be removed.
519 -----
520 "#);
521
522 let output = work_dir.run_jj(["log", "--no-graph", "-r@-", "-Tdescription"]);
523 insta::assert_snapshot!(output, @"
524 Reviewed-by: [email protected]
525 [EOF]
526 ");
527 Ok(())
528 }
529
530 #[test]
531 fn test_commit_with_editor_and_message_args() -> TestResult {
532 let mut test_env = TestEnvironment::default();
533 let edit_script = test_env.set_up_fake_editor();
534 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
535 let work_dir = test_env.work_dir("repo");
536
537 work_dir.write_file("file1", "foo\n");
538 std::fs::write(&edit_script, "dump editor")?;
539 work_dir
540 .run_jj(["commit", "-m", "message from command line", "--editor"])
541 .success();
542
543 // Verify editor was opened with the message from command line
544 insta::assert_snapshot!(
545 std::fs::read_to_string(test_env.env_root().join("editor"))?, @r#"
546 message from command line
547
548 JJ: Change ID: qpvuntsm
549 JJ: This commit contains the following changes:
550 JJ: A file1
551 JJ:
552 JJ: Lines starting with "JJ:" (like this one) will be removed.
553 "#);
554 insta::assert_snapshot!(get_log_output(&work_dir), @"
555 @ c3a4bb8be8d7
556 ○ f6acb1a163f2 message from command line
557 ◆ 000000000000
558 [EOF]
559 ");
560 Ok(())
561 }
562
563 #[test]
564 fn test_commit_with_editor_and_empty_message() -> TestResult {
565 let mut test_env = TestEnvironment::default();
566 let edit_script = test_env.set_up_fake_editor();
567 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
568 let work_dir = test_env.work_dir("repo");
569
570 work_dir.write_file("file1", "foo\n");
571
572 // Use --editor with an empty message. The trailers should be added because
573 // the editor will be opened.
574 std::fs::write(&edit_script, "dump editor")?;
575 work_dir
576 .run_jj([
577 "commit",
578 "-m",
579 "",
580 "--editor",
581 "--config",
582 r#"templates.commit_trailers='"Trailer: value"'"#,
583 ])
584 .success();
585
586 // Verify editor was opened with trailers added to the empty message
587 insta::assert_snapshot!(
588 std::fs::read_to_string(test_env.env_root().join("editor"))?, @r#"
589
590
591 Trailer: value
592
593 JJ: Change ID: qpvuntsm
594 JJ: This commit contains the following changes:
595 JJ: A file1
596 JJ:
597 JJ: Lines starting with "JJ:" (like this one) will be removed.
598 "#);
599 Ok(())
600 }
601
602 #[test]
603 fn test_commit_with_editor_without_message() -> TestResult {
604 let mut test_env = TestEnvironment::default();
605 let edit_script = test_env.set_up_fake_editor();
606 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
607 let work_dir = test_env.work_dir("repo");
608
609 work_dir.write_file("file1", "foo\n");
610
611 // --editor without -m should behave the same as without --editor (normal flow)
612 std::fs::write(&edit_script, "dump editor")?;
613 let output = work_dir.run_jj(["commit", "--editor"]).success();
614
615 // Verify editor was opened
616 insta::assert_snapshot!(
617 std::fs::read_to_string(test_env.env_root().join("editor"))?, @r#"
618
619
620 JJ: Change ID: qpvuntsm
621 JJ: This commit contains the following changes:
622 JJ: A file1
623 JJ:
624 JJ: Lines starting with "JJ:" (like this one) will be removed.
625 "#);
626 insta::assert_snapshot!(get_log_output(&work_dir), @"
627 @ f5a89f0f6366
628 ○ 38f3e84bb6a9
629 ◆ 000000000000
630 [EOF]
631 ");
632 insta::assert_snapshot!(output, @"
633 ------- stderr -------
634 Hint: The commit message was left empty.
635 If this was not intentional, run `jj undo` to restore the previous state.
636 Or run `jj desc @-` to add a description to the parent commit.
637 Working copy (@) now at: rlvkpnrz f5a89f0f (empty) (no description set)
638 Parent commit (@-) : qpvuntsm 38f3e84b (no description set)
639 [EOF]
640 ");
641 Ok(())
642 }
643
644 #[must_use]
645 fn get_log_output(work_dir: &TestWorkDir) -> CommandOutput {
646 let template = r#"commit_id.short() ++ " " ++ description"#;
647 work_dir.run_jj(["log", "-T", template])
648 }