Debian Codegraph @

cli/tests/test_describe_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 indoc::indoc;
18 use testutils::TestResult;
19
20 use crate::common::CommandOutput;
21 use crate::common::TestEnvironment;
22 use crate::common::TestWorkDir;
23 use crate::common::force_interactive;
24
25 #[test]
26 fn test_describe() -> TestResult {
27 let mut test_env = TestEnvironment::default();
28 let edit_script = test_env.set_up_fake_editor();
29 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
30 let work_dir = test_env.work_dir("repo");
31
32 // Set a description using `-m` flag
33 let output = work_dir.run_jj(["describe", "-m", "description from CLI"]);
34 insta::assert_snapshot!(output, @"
35 ------- stderr -------
36 Working copy (@) now at: qpvuntsm 7b186b4f (empty) description from CLI
37 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
38 [EOF]
39 ");
40
41 // Set the same description using `-m` flag, but with explicit newline
42 let output = work_dir.run_jj(["describe", "-m", "description from CLI\n"]);
43 insta::assert_snapshot!(output, @"
44 ------- stderr -------
45 Nothing changed.
46 [EOF]
47 ");
48
49 // Check that the text file gets initialized with the current description and
50 // make no changes
51 std::fs::write(&edit_script, "dump editor0")?;
52 let output = work_dir.run_jj(["describe"]);
53 insta::assert_snapshot!(output, @"
54 ------- stderr -------
55 Nothing changed.
56 [EOF]
57 ");
58 insta::assert_snapshot!(
59 std::fs::read_to_string(test_env.env_root().join("editor0"))?, @r#"
60 description from CLI
61
62 JJ: Change ID: qpvuntsm
63 JJ:
64 JJ: Lines starting with "JJ:" (like this one) will be removed.
65 "#);
66
67 // Set a description in editor
68 std::fs::write(&edit_script, "write\ndescription from editor")?;
69 let output = work_dir.run_jj(["describe"]);
70 insta::assert_snapshot!(output, @"
71 ------- stderr -------
72 Working copy (@) now at: qpvuntsm 28173c3e (empty) description from editor
73 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
74 [EOF]
75 ");
76
77 // Lines in editor starting with "JJ: " are ignored
78 std::fs::write(
79 &edit_script,
80 "write\nJJ: ignored\ndescription among comment\nJJ: ignored",
81 )?;
82 let output = work_dir.run_jj(["describe"]);
83 insta::assert_snapshot!(output, @"
84 ------- stderr -------
85 Working copy (@) now at: qpvuntsm e7488502 (empty) description among comment
86 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
87 [EOF]
88 ");
89
90 // Multi-line description
91 std::fs::write(&edit_script, "write\nline1\nline2\n\nline4\n\n")?;
92 let output = work_dir.run_jj(["describe"]);
93 insta::assert_snapshot!(output, @"
94 ------- stderr -------
95 Working copy (@) now at: qpvuntsm 7438c202 (empty) line1
96 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
97 [EOF]
98 ");
99 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
100 insta::assert_snapshot!(output, @"
101 line1
102 line2
103
104 line4
105 [EOF]
106 ");
107
108 // Multi-line description again with CRLF, which should make no changes
109 std::fs::write(&edit_script, "write\nline1\r\nline2\r\n\r\nline4\r\n\r\n")?;
110 let output = work_dir.run_jj(["describe"]);
111 insta::assert_snapshot!(output, @"
112 ------- stderr -------
113 Nothing changed.
114 [EOF]
115 ");
116
117 // Multi-line description starting with newlines
118 std::fs::write(&edit_script, "write\n\n\nline1\nline2")?;
119 let output = work_dir.run_jj(["describe"]);
120 insta::assert_snapshot!(output, @"
121 ------- stderr -------
122 Working copy (@) now at: qpvuntsm f38e2bd7 (empty) line1
123 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
124 [EOF]
125 ");
126 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
127 insta::assert_snapshot!(output, @"
128 line1
129 line2
130 [EOF]
131 ");
132
133 // Clear description
134 let output = work_dir.run_jj(["describe", "-m", ""]);
135 insta::assert_snapshot!(output, @"
136 ------- stderr -------
137 Working copy (@) now at: qpvuntsm 7c00df81 (empty) (no description set)
138 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
139 [EOF]
140 ");
141 std::fs::write(&edit_script, "write\n")?;
142 let output = work_dir.run_jj(["describe"]);
143 insta::assert_snapshot!(output, @"
144 ------- stderr -------
145 Nothing changed.
146 [EOF]
147 ");
148
149 // Fails if the editor fails
150 std::fs::write(&edit_script, "fail")?;
151 let output = work_dir.run_jj(["describe"]);
152 insta::with_settings!({
153 filters => [
154 (r"\bEditor '[^']*'", "Editor '<redacted>'"),
155 (r"in .*(editor-)[^.]*(\.jjdescription)\b", "in <redacted>$1<redacted>$2"),
156 ("exit code", "exit status"), // Windows
157 ],
158 }, {
159 insta::assert_snapshot!(output, @"
160 ------- stderr -------
161 Error: Failed to edit description
162 Caused by: Editor '<redacted>' exited with exit status: 1
163 Hint: Edited description is left in <redacted>editor-<redacted>.jjdescription
164 [EOF]
165 [exit status: 1]
166 ");
167 });
168
169 // ignore everything after the first ignore-rest line
170 std::fs::write(
171 &edit_script,
172 indoc! {"
173 write
174 description from editor
175
176 content of message from editor
177 JJ: ignore-rest
178 content after ignore line should not be included
179 JJ: ignore-rest
180 ignore everything until EOF or next description
181 "},
182 )?;
183 let output = work_dir.run_jj(["describe"]);
184 insta::assert_snapshot!(output, @"
185 ------- stderr -------
186 Working copy (@) now at: qpvuntsm 0ec68094 (empty) description from editor
187 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
188 [EOF]
189 ");
190 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
191 insta::assert_snapshot!(output, @"
192 description from editor
193
194 content of message from editor
195 [EOF]
196 ");
197 Ok(())
198 }
199
200 #[test]
201 fn test_describe_editor_env() {
202 let test_env = TestEnvironment::default();
203 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
204 let work_dir = test_env.work_dir("repo");
205
206 // Fails if the editor doesn't exist
207 let output = work_dir.run_jj_with(|cmd| {
208 cmd.arg("describe")
209 .env("EDITOR", "this-editor-does-not-exist")
210 });
211 insta::assert_snapshot!(
212 output.normalize_stderr_with(|s| s.split_inclusive('\n').take(3).collect()), @"
213 ------- stderr -------
214 Error: Failed to edit description
215 Caused by:
216 1: Failed to run editor 'this-editor-does-not-exist'
217 [EOF]
218 [exit status: 1]
219 ");
220
221 // `$VISUAL` overrides `$EDITOR`
222 let output = work_dir.run_jj_with(|cmd| {
223 cmd.arg("describe")
224 .env("VISUAL", "bad-editor-from-visual-env")
225 .env("EDITOR", "bad-editor-from-editor-env")
226 });
227 insta::assert_snapshot!(
228 output.normalize_stderr_with(|s| s.split_inclusive('\n').take(3).collect()), @"
229 ------- stderr -------
230 Error: Failed to edit description
231 Caused by:
232 1: Failed to run editor 'bad-editor-from-visual-env'
233 [EOF]
234 [exit status: 1]
235 ");
236
237 // `ui.editor` config overrides `$VISUAL`
238 test_env.add_config(r#"ui.editor = "bad-editor-from-config""#);
239 let output = work_dir.run_jj_with(|cmd| {
240 cmd.arg("describe")
241 .env("VISUAL", "bad-editor-from-visual-env")
242 });
243 insta::assert_snapshot!(
244 output.normalize_stderr_with(|s| s.split_inclusive('\n').take(3).collect()), @"
245 ------- stderr -------
246 Error: Failed to edit description
247 Caused by:
248 1: Failed to run editor 'bad-editor-from-config'
249 [EOF]
250 [exit status: 1]
251 ");
252
253 // `$JJ_EDITOR` overrides `ui.editor` config
254 let output = work_dir.run_jj_with(|cmd| {
255 cmd.arg("describe")
256 .env("JJ_EDITOR", "bad-jj-editor-from-jj-editor-env")
257 });
258 insta::assert_snapshot!(
259 output.normalize_stderr_with(|s| s.split_inclusive('\n').take(3).collect()), @"
260 ------- stderr -------
261 Error: Failed to edit description
262 Caused by:
263 1: Failed to run editor 'bad-jj-editor-from-jj-editor-env'
264 [EOF]
265 [exit status: 1]
266 ");
267 }
268
269 #[test]
270 fn test_describe_no_matching_revisions() {
271 let test_env = TestEnvironment::default();
272 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
273 let work_dir = test_env.work_dir("repo");
274 let output = work_dir.run_jj(["describe", "none()"]);
275 insta::assert_snapshot!(output, @"
276 ------- stderr -------
277 No revisions to describe.
278 [EOF]
279 ");
280 }
281
282 #[test]
283 fn test_describe_multiple_commits() -> TestResult {
284 let mut test_env = TestEnvironment::default();
285 let edit_script = test_env.set_up_fake_editor();
286 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
287 let work_dir = test_env.work_dir("repo");
288
289 // Initial setup
290 work_dir.run_jj(["new"]).success();
291 work_dir.run_jj(["new"]).success();
292 insta::assert_snapshot!(get_log_output(&work_dir), @"
293 @ 3cd3b246e098
294 ○ 43444d88b009
295 ○ e8849ae12c70
296 ◆ 000000000000
297 [EOF]
298 ");
299
300 // Set the description of multiple commits using `-m` flag
301 let output = work_dir.run_jj(["describe", "-r@", "-r@--", "-m", "description from CLI"]);
302 insta::assert_snapshot!(output, @r"
303 ------- stderr -------
304 Updated 2 commits.
305 Rebased 1 descendant commits.
306 Working copy (@) now at: kkmpptxz 4c3ccb9d (empty) description from CLI
307 Parent commit (@-) : rlvkpnrz 650ac8f2 (empty) (no description set)
308 [EOF]
309 ");
310 insta::assert_snapshot!(get_log_output(&work_dir), @"
311 @ 4c3ccb9d4fb2 description from CLI
312 ○ 650ac8f249be
313 ○ 0ff65c91377a description from CLI
314 ◆ 000000000000
315 [EOF]
316 ");
317
318 // Check that the text file gets initialized with the current description of
319 // each commit and doesn't update commits if no changes are made.
320 // Commit descriptions are edited in topological order
321 std::fs::write(&edit_script, "dump editor0")?;
322 let output = work_dir.run_jj(["describe", "-r@", "@-"]);
323 insta::assert_snapshot!(output, @"
324 ------- stderr -------
325 Nothing changed.
326 [EOF]
327 ");
328 insta::assert_snapshot!(
329 std::fs::read_to_string(test_env.env_root().join("editor0"))?, @r#"
330 JJ: Enter or edit commit descriptions after the `JJ: describe` lines.
331 JJ: Warning:
332 JJ: - The text you enter will be lost on a syntax error.
333 JJ: - The syntax of the separator lines may change in the future.
334 JJ:
335 JJ: describe 650ac8f249be -------
336
337
338 JJ: Change ID: rlvkpnrz
339 JJ:
340 JJ: describe 4c3ccb9d4fb2 -------
341 description from CLI
342
343 JJ: Change ID: kkmpptxz
344 JJ:
345 JJ: Lines starting with "JJ:" (like this one) will be removed.
346 "#);
347
348 // Set the description of multiple commits in the editor
349 std::fs::write(
350 &edit_script,
351 indoc! {"
352 write
353 JJ: Enter or edit commit descriptions after the `JJ: describe` lines.
354
355 JJ: More header tests. Library tests verify parsing in other situations.
356
357 JJ: describe 650ac8f249be -------
358 description from editor of @-
359
360 further commit message of @-
361
362 JJ: describe 4c3ccb9d4fb2 -------
363 description from editor of @
364
365 further commit message of @
366
367 JJ: Lines starting with \"JJ: \" (like this one) will be removed.
368 "},
369 )?;
370 let output = work_dir.run_jj(["describe", "@", "@-"]);
371 insta::assert_snapshot!(output, @r"
372 ------- stderr -------
373 Updated 2 commits.
374 Working copy (@) now at: kkmpptxz 87c0f3c7 (empty) description from editor of @
375 Parent commit (@-) : rlvkpnrz 9b9041eb (empty) description from editor of @-
376 [EOF]
377 ");
378 insta::assert_snapshot!(get_log_output(&work_dir), @"
379 @ 87c0f3c75a22 description from editor of @
380 │
381 │ further commit message of @
382 ○ 9b9041eb2f04 description from editor of @-
383 │
384 │ further commit message of @-
385 ○ 0ff65c91377a description from CLI
386 ◆ 000000000000
387 [EOF]
388 ");
389
390 // Fails if the edited message has a commit with multiple descriptions
391 std::fs::write(
392 &edit_script,
393 indoc! {"
394 write
395 JJ: describe 9b9041eb2f04 -------
396 first description from editor of @-
397
398 further commit message of @-
399
400 JJ: describe 9b9041eb2f04 -------
401 second description from editor of @-
402
403 further commit message of @-
404
405 JJ: describe 87c0f3c75a22 -------
406 updated description from editor of @
407
408 further commit message of @
409
410 JJ: Lines starting with \"JJ: \" (like this one) will be removed.
411 "},
412 )?;
413 let output = work_dir.run_jj(["describe", "@", "@-"]);
414 insta::assert_snapshot!(output, @"
415 ------- stderr -------
416 Error: The following commits were found in the edited message multiple times: 9b9041eb2f04
417 [EOF]
418 [exit status: 1]
419 ");
420
421 // Fails if the edited message has unexpected commit IDs
422 std::fs::write(
423 &edit_script,
424 indoc! {"
425 write
426 JJ: describe 000000000000 -------
427 unexpected commit ID
428
429 JJ: describe 9b9041eb2f04 -------
430 description from editor of @-
431
432 further commit message of @-
433
434 JJ: describe 87c0f3c75a22 -------
435 description from editor of @
436
437 further commit message of @
438
439 JJ: Lines starting with \"JJ: \" (like this one) will be removed.
440 "},
441 )?;
442 let output = work_dir.run_jj(["describe", "@", "@-"]);
443 insta::assert_snapshot!(output, @"
444 ------- stderr -------
445 Error: The following commits were not being edited, but were found in the edited message: 000000000000
446 [EOF]
447 [exit status: 1]
448 ");
449
450 // Fails if the edited message has missing commit messages
451 std::fs::write(
452 &edit_script,
453 indoc! {"
454 write
455 JJ: describe 87c0f3c75a22 -------
456 description from editor of @
457
458 further commit message of @
459
460 JJ: Lines starting with \"JJ: \" (like this one) will be removed.
461 "},
462 )?;
463 let output = work_dir.run_jj(["describe", "@", "@-"]);
464 insta::assert_snapshot!(output, @"
465 ------- stderr -------
466 Error: The description for the following commits were not found in the edited message: 9b9041eb2f04
467 [EOF]
468 [exit status: 1]
469 ");
470
471 // Fails if the edited message has a line which does not have any preceding
472 // `JJ: describe` headers
473 std::fs::write(
474 &edit_script,
475 indoc! {"
476 write
477 description from editor of @-
478
479 JJ: describe 9b9041eb2f04 -------
480 description from editor of @
481
482 JJ: Lines starting with \"JJ: \" (like this one) will be removed.
483 "},
484 )?;
485 let output = work_dir.run_jj(["describe", "@", "@-"]);
486 insta::assert_snapshot!(output, @r#"
487 ------- stderr -------
488 Error: Found the following line without a commit header: "description from editor of @-"
489 [EOF]
490 [exit status: 1]
491 "#);
492
493 // Fails if the editor fails
494 std::fs::write(&edit_script, "fail")?;
495 let output = work_dir.run_jj(["describe", "@", "@-"]);
496 insta::with_settings!({
497 filters => [
498 (r"\bEditor '[^']*'", "Editor '<redacted>'"),
499 (r"in .*(editor-)[^.]*(\.jjdescription)\b", "in <redacted>$1<redacted>$2"),
500 ("exit code", "exit status"), // Windows
501 ],
502 }, {
503 insta::assert_snapshot!(output, @"
504 ------- stderr -------
505 Error: Failed to edit description
506 Caused by: Editor '<redacted>' exited with exit status: 1
507 Hint: Edited description is left in <redacted>editor-<redacted>.jjdescription
508 [EOF]
509 [exit status: 1]
510 ");
511 });
512
513 // describe lines should take priority over ignore-rest
514 std::fs::write(
515 &edit_script,
516 indoc! {"
517 write
518 JJ: describe 9b9041eb2f04 -------
519 description from editor for @-
520
521 JJ: ignore-rest
522 content after ignore-rest should not be included
523
524 JJ: describe 0ff65c91377a -------
525 description from editor for @--
526
527 JJ: ignore-rest
528 each commit should skip their own ignore-rest
529 "},
530 )?;
531 let output = work_dir.run_jj(["describe", "@-", "@--"]);
532 insta::assert_snapshot!(output, @r"
533 ------- stderr -------
534 Updated 2 commits.
535 Rebased 1 descendant commits.
536 Working copy (@) now at: kkmpptxz 5a6249e9 (empty) description from editor of @
537 Parent commit (@-) : rlvkpnrz d1c1edbd (empty) description from editor for @-
538 [EOF]
539 ");
540 insta::assert_snapshot!(get_log_output(&work_dir), @"
541 @ 5a6249e9e71a description from editor of @
542 │
543 │ further commit message of @
544 ○ d1c1edbd5595 description from editor for @-
545 ○ a8bf976d72fb description from editor for @--
546 ◆ 000000000000
547 [EOF]
548 ");
549 Ok(())
550 }
551
552 #[test]
553 fn test_describe_with_draft_template() {
554 let mut test_env = TestEnvironment::default();
555 let edit_script = test_env.set_up_fake_editor();
556 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
557 let work_dir = test_env.work_dir("repo");
558
559 // Initial setup
560 work_dir.write_file("a.txt", "aaaa\nbbbb\ncccc\n");
561 work_dir.run_jj(["commit", "-m=first"]).success();
562 work_dir.write_file("a.txt", b"aaaa\ncccc\ndddd\n\xff\n");
563 work_dir.run_jj(["describe", "-m=second"]).success();
564 insta::assert_snapshot!(get_log_output(&work_dir), @"
565 @ c43cce883e27 second
566 ○ 8620a92b036c first
567 ◆ 000000000000
568 [EOF]
569 ");
570
571 // Dump the default commit description template
572 std::fs::write(&edit_script, "dump editor0").unwrap();
573 let output = work_dir.run_jj(["describe"]);
574 insta::assert_snapshot!(output, @"
575 ------- stderr -------
576 Nothing changed.
577 [EOF]
578 ");
579 insta::assert_snapshot!(
580 std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r#"
581 second
582
583 JJ: Change ID: rlvkpnrz
584 JJ: This commit contains the following changes:
585 JJ: M a.txt
586 JJ:
587 JJ: Lines starting with "JJ:" (like this one) will be removed.
588 "#);
589
590 // Builtin template with diff content
591 std::fs::write(&edit_script, "dump editor0").unwrap();
592 let output = work_dir.run_jj([
593 "describe",
594 "--config=templates.draft_commit_description='builtin_draft_commit_description_with_diff'",
595 ]);
596 insta::assert_snapshot!(output, @"
597 ------- stderr -------
598 Nothing changed.
599 [EOF]
600 ");
601 insta::assert_snapshot!(
602 std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r#"
603 second
604
605 JJ: Change ID: rlvkpnrz
606 JJ: This commit contains the following changes:
607 JJ: M a.txt
608
609 JJ: ignore-rest
610 diff --git a/a.txt b/a.txt
611 index edd13ee535..12e5763da1 100644
612 --- a/a.txt
613 +++ b/a.txt
614 @@ -1,3 +1,4 @@
615 aaaa
616 -bbbb
617 cccc
618 +dddd
619 +�
620
621 JJ: Lines starting with "JJ:" (like this one) will be removed.
622 "#);
623
624 // Newline auto-inserted when template produces content without newline
625 std::fs::write(&edit_script, "dump editor0").unwrap();
626 let output = work_dir.run_jj([
627 "describe",
628 "--config=templates.draft_commit_description='change_id'",
629 ]);
630 insta::assert_snapshot!(output, @"
631 ------- stderr -------
632 Working copy (@) now at: rlvkpnrz 1fd03b68 rlvkpnrzqnoowoytxnquwvuryrwnrmlp
633 Parent commit (@-) : qpvuntsm 8620a92b first
634 [EOF]
635 ");
636 insta::assert_snapshot!(
637 std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r#"
638 rlvkpnrzqnoowoytxnquwvuryrwnrmlp
639
640 JJ: Lines starting with "JJ:" (like this one) will be removed.
641 "#);
642
643 // Newline auto-inserted when template produces empty string
644 std::fs::write(&edit_script, "dump editor0").unwrap();
645 let output = work_dir.run_jj([
646 "describe",
647 r#"--config=templates.draft_commit_description='""'"#,
648 ]);
649 insta::assert_snapshot!(output, @"
650 ------- stderr -------
651 Working copy (@) now at: rlvkpnrz b59daf76 (no description set)
652 Parent commit (@-) : qpvuntsm 8620a92b first
653 [EOF]
654 ");
655 let editor0 = std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap();
656 insta::assert_snapshot!(format!("-----\n{editor0}-----\n"), @r#"
657 -----
658
659 JJ: Lines starting with "JJ:" (like this one) will be removed.
660 -----
661 "#);
662 }
663
664 #[test]
665 fn test_multiple_message_args() {
666 let test_env = TestEnvironment::default();
667 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
668 let work_dir = test_env.work_dir("repo");
669
670 // Set a description using `-m` flag
671 let output = work_dir.run_jj([
672 "describe",
673 "-m",
674 "First Paragraph from CLI",
675 "-m",
676 "Second Paragraph from CLI",
677 ]);
678 insta::assert_snapshot!(output, @"
679 ------- stderr -------
680 Working copy (@) now at: qpvuntsm 9b8ad205 (empty) First Paragraph from CLI
681 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
682 [EOF]
683 ");
684
685 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
686 insta::assert_snapshot!(output, @"
687 First Paragraph from CLI
688
689 Second Paragraph from CLI
690 [EOF]
691 ");
692
693 // Set the same description, with existing newlines
694 let output = work_dir.run_jj([
695 "describe",
696 "-m",
697 "First Paragraph from CLI\n",
698 "-m",
699 "Second Paragraph from CLI\n",
700 ]);
701 insta::assert_snapshot!(output, @"
702 ------- stderr -------
703 Nothing changed.
704 [EOF]
705 ");
706
707 // Use an empty -m flag between paragraphs to insert an extra blank line
708 let output = work_dir.run_jj([
709 "describe",
710 "-m",
711 "First Paragraph from CLI\n",
712 "--message",
713 "",
714 "-m",
715 "Second Paragraph from CLI",
716 ]);
717 insta::assert_snapshot!(output, @"
718 ------- stderr -------
719 Working copy (@) now at: qpvuntsm ac46ea93 (empty) First Paragraph from CLI
720 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
721 [EOF]
722 ");
723
724 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
725 insta::assert_snapshot!(output, @"
726 First Paragraph from CLI
727
728
729 Second Paragraph from CLI
730 [EOF]
731 ");
732 }
733
734 #[test]
735 fn test_describe_description_file_removed() {
736 let mut test_env = TestEnvironment::default();
737 let edit_script = test_env.set_up_fake_editor();
738 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
739 let work_dir = test_env.work_dir("repo");
740
741 // Description file misplaced by the user or a faulty editor
742 std::fs::write(edit_script, "delete").unwrap();
743 let output = work_dir.run_jj(["describe"]);
744 insta::with_settings!({
745 filters => [
746 (r"(access|in) .*(editor-)[^.]*(\.jjdescription)\b", "$1 <redacted>$2<redacted>$3"),
747 ("The system cannot find the file specified.", "No such file or directory"),
748 ],
749 }, {
750 insta::assert_snapshot!(output, @"
751 ------- stderr -------
752 Error: Failed to edit description
753 Caused by:
754 1: Cannot access <redacted>editor-<redacted>.jjdescription
755 2: No such file or directory (os error 2)
756 Hint: Edited description is left in <redacted>editor-<redacted>.jjdescription
757 [EOF]
758 [exit status: 1]
759 ");
760 });
761 }
762
763 #[test]
764 fn test_describe_stdin_description() {
765 let test_env = TestEnvironment::default();
766 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
767 let work_dir = test_env.work_dir("repo");
768 let output = work_dir.run_jj_with(|cmd| {
769 force_interactive(cmd)
770 .args(["describe", "--stdin"])
771 .write_stdin("first stdin\nsecond stdin")
772 });
773 insta::assert_snapshot!(output, @"
774 ------- stderr -------
775 Working copy (@) now at: qpvuntsm b9990801 (empty) first stdin
776 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
777 [EOF]
778 ");
779 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
780 insta::assert_snapshot!(output, @"
781 first stdin
782 second stdin
783 [EOF]
784 ");
785 }
786
787 #[test]
788 fn test_describe_default_description() -> TestResult {
789 let mut test_env = TestEnvironment::default();
790 let edit_script = test_env.set_up_fake_editor();
791 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
792 test_env.add_config(r#"template-aliases.default_commit_description = '"\n\nTESTED=TODO\n"'"#);
793 let work_dir = test_env.work_dir("repo");
794
795 work_dir.write_file("file1", "foo\n");
796 work_dir.write_file("file2", "bar\n");
797 std::fs::write(edit_script, ["dump editor"].join("\0"))?;
798 let output = work_dir.run_jj(["describe"]);
799 insta::assert_snapshot!(output, @"
800 ------- stderr -------
801 Working copy (@) now at: qpvuntsm 7276dfff TESTED=TODO
802 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
803 [EOF]
804 ");
805 insta::assert_snapshot!(
806 std::fs::read_to_string(test_env.env_root().join("editor"))?, @r#"
807
808
809 TESTED=TODO
810
811 JJ: Change ID: qpvuntsm
812 JJ: This commit contains the following changes:
813 JJ: A file1
814 JJ: A file2
815 JJ:
816 JJ: Lines starting with "JJ:" (like this one) will be removed.
817 "#);
818
819 Ok(())
820 }
821
822 #[test]
823 fn test_describe_avoids_unc() -> TestResult {
824 let mut test_env = TestEnvironment::default();
825 let edit_script = test_env.set_up_fake_editor();
826 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
827 let work_dir = test_env.work_dir("repo");
828
829 std::fs::write(edit_script, "dump-path path")?;
830 work_dir.run_jj(["describe"]).success();
831
832 let edited_path = PathBuf::from(std::fs::read_to_string(test_env.env_root().join("path"))?);
833 // While `assert!(!edited_path.starts_with("//?/"))` could work here in most
834 // cases, it fails when it is not safe to strip the prefix, such as paths
835 // over 260 chars.
836 assert_eq!(edited_path, dunce::simplified(&edited_path));
837 Ok(())
838 }
839
840 #[test]
841 fn test_describe_with_editor_and_message_args_opens_editor() -> TestResult {
842 let mut test_env = TestEnvironment::default();
843 let edit_script = test_env.set_up_fake_editor();
844 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
845 let work_dir = test_env.work_dir("repo");
846
847 std::fs::write(edit_script, ["dump editor"].join("\0"))?;
848 let output = work_dir.run_jj(["describe", "-m", "message from command line", "--editor"]);
849 insta::assert_snapshot!(output, @"
850 ------- stderr -------
851 Working copy (@) now at: qpvuntsm f9bee6de (empty) message from command line
852 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
853 [EOF]
854 ");
855 insta::assert_snapshot!(
856 std::fs::read_to_string(test_env.env_root().join("editor"))?, @r#"
857 message from command line
858
859 JJ: Change ID: qpvuntsm
860 JJ:
861 JJ: Lines starting with "JJ:" (like this one) will be removed.
862 "#);
863 Ok(())
864 }
865
866 #[test]
867 fn test_describe_change_with_existing_message_with_editor_and_message_args_opens_editor()
868 -> TestResult {
869 let mut test_env = TestEnvironment::default();
870 let edit_script = test_env.set_up_fake_editor();
871 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
872 let work_dir = test_env.work_dir("repo");
873
874 work_dir
875 .run_jj(["describe", "-m", "original message"])
876 .success();
877
878 std::fs::write(edit_script, ["dump editor"].join("\0"))?;
879 let output = work_dir.run_jj(["describe", "-m", "new message", "--editor"]);
880 insta::assert_snapshot!(output, @"
881 ------- stderr -------
882 Working copy (@) now at: qpvuntsm f8f14f7c (empty) new message
883 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
884 [EOF]
885 ");
886 insta::assert_snapshot!(
887 std::fs::read_to_string(test_env.env_root().join("editor"))?, @r#"
888 new message
889
890 JJ: Change ID: qpvuntsm
891 JJ:
892 JJ: Lines starting with "JJ:" (like this one) will be removed.
893 "#);
894 Ok(())
895 }
896
897 #[test]
898 fn test_add_trailer() {
899 let mut test_env = TestEnvironment::default();
900 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
901 let _edit_script = test_env.set_up_fake_editor();
902 let work_dir = test_env.work_dir("repo");
903
904 // Set a description using `-m` flag
905 let output = work_dir.run_jj([
906 "describe",
907 "-m",
908 "Message from CLI",
909 "--config",
910 r#"templates.commit_trailers='"Signed-off-by: " ++ committer'"#,
911 ]);
912 insta::assert_snapshot!(output, @"
913 ------- stderr -------
914 Working copy (@) now at: qpvuntsm 55c6f83d (empty) Message from CLI
915 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
916 [EOF]
917 ");
918
919 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
920 insta::assert_snapshot!(output, @"
921 Message from CLI
922
923 Signed-off-by: Test User <[email protected]>
924 [EOF]
925 ");
926
927 // multiple trailers may be used
928 let output = work_dir.run_jj([
929 "describe",
930 "--config",
931 r#"templates.commit_trailers='"CC: [email protected]\nChange-Id: I6a6a6964" ++ self.change_id().normal_hex()'"#,
932 ]);
933 insta::assert_snapshot!(output, @"
934 ------- stderr -------
935 Working copy (@) now at: qpvuntsm 2b2e302d (empty) Message from CLI
936 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
937 [EOF]
938 ");
939
940 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
941 insta::assert_snapshot!(output, @"
942 Message from CLI
943
944 Signed-off-by: Test User <[email protected]>
945 CC: [email protected]
946 Change-Id: I6a6a69649a45c67d3e96a7e5007c110ede34dec5
947 [EOF]
948 ");
949
950 // it won't create a duplicate entry
951 let output = work_dir.run_jj([
952 "describe",
953 "--config",
954 r#"templates.commit_trailers='"CC: [email protected]"'"#,
955 ]);
956 insta::assert_snapshot!(output, @"
957 ------- stderr -------
958 Nothing changed.
959 [EOF]
960 ");
961
962 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
963 insta::assert_snapshot!(output, @"
964 Message from CLI
965
966 Signed-off-by: Test User <[email protected]>
967 CC: [email protected]
968 Change-Id: I6a6a69649a45c67d3e96a7e5007c110ede34dec5
969 [EOF]
970 ");
971
972 // invalid generated trailers generate an error
973 let output = work_dir.run_jj([
974 "describe",
975 "--config",
976 r#"templates.commit_trailers='"this is an invalid trailer"'"#,
977 ]);
978 insta::assert_snapshot!(output, @"
979 ------- stderr -------
980 Error: Invalid trailer line: this is an invalid trailer
981 [EOF]
982 [exit status: 1]
983 ");
984
985 // it doesn't modify a commit with an empty description
986 let output = work_dir.run_jj(["new"]);
987 insta::assert_snapshot!(output, @"
988 ------- stderr -------
989 Working copy (@) now at: yostqsxw dbea21e1 (empty) (no description set)
990 Parent commit (@-) : qpvuntsm 2b2e302d (empty) Message from CLI
991 [EOF]
992 ");
993 let output = work_dir.run_jj([
994 "describe",
995 "--message=",
996 "--config",
997 r#"templates.commit_trailers='"CC: [email protected]"'"#,
998 ]);
999 insta::assert_snapshot!(output, @"
1000 ------- stderr -------
1001 Nothing changed.
1002 [EOF]
1003 ");
1004
1005 // Invalid trailer content
1006 work_dir.write_file("data.txt", b"\xff\n");
1007 let output = work_dir.run_jj([
1008 "describe",
1009 "-m=content",
1010 "--config",
1011 r#"templates.commit_trailers='indent("Content: ", diff.git())'"#,
1012 ]);
1013 insta::assert_snapshot!(output, @"
1014 ------- stderr -------
1015 Error: Trailers should be valid utf-8
1016 [EOF]
1017 [exit status: 1]
1018 ");
1019 }
1020
1021 #[test]
1022 fn test_add_trailer_committer() -> TestResult {
1023 let mut test_env = TestEnvironment::default();
1024 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1025 let edit_script = test_env.set_up_fake_editor();
1026 let work_dir = test_env.work_dir("repo");
1027 test_env.add_config(
1028 r#"[templates]
1029 commit_trailers = '''"Signed-off-by: " ++ committer.email()'''"#,
1030 );
1031
1032 let output = work_dir.run_jj(["describe", "-m", "Message from CLI"]);
1033 insta::assert_snapshot!(output, @"
1034 ------- stderr -------
1035 Working copy (@) now at: qpvuntsm 67458426 (empty) Message from CLI
1036 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
1037 [EOF]
1038 ");
1039
1040 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
1041 insta::assert_snapshot!(output, @"
1042 Message from CLI
1043
1044 Signed-off-by: [email protected]
1045 [EOF]
1046 ");
1047
1048 // committer is properly set in the trailer
1049 let output = work_dir.run_jj(["describe", "[email protected]"]);
1050 insta::assert_snapshot!(output, @"
1051 ------- stderr -------
1052 Working copy (@) now at: qpvuntsm 05ddee5c (empty) Message from CLI
1053 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
1054 [EOF]
1055 ");
1056
1057 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
1058 insta::assert_snapshot!(output, @"
1059 Message from CLI
1060
1061 Signed-off-by: [email protected]
1062 Signed-off-by: [email protected]
1063 [EOF]
1064 ");
1065
1066 // trailer is added with the expected committer in the editor
1067 std::fs::write(&edit_script, "dump editor0")?;
1068 let output = work_dir.run_jj(["describe", "--config", "[email protected]"]);
1069 insta::assert_snapshot!(output, @"
1070 ------- stderr -------
1071 Working copy (@) now at: qpvuntsm b7dafa2c (empty) Message from CLI
1072 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
1073 [EOF]
1074 ");
1075
1076 insta::assert_snapshot!(
1077 std::fs::read_to_string(test_env.env_root().join("editor0"))?, @r#"
1078 Message from CLI
1079
1080 Signed-off-by: [email protected]
1081 Signed-off-by: [email protected]
1082 Signed-off-by: [email protected]
1083
1084 JJ: Change ID: qpvuntsm
1085 JJ:
1086 JJ: Lines starting with "JJ:" (like this one) will be removed.
1087 "#);
1088
1089 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
1090 insta::assert_snapshot!(output, @"
1091 Message from CLI
1092
1093 Signed-off-by: [email protected]
1094 Signed-off-by: [email protected]
1095 Signed-off-by: [email protected]
1096 [EOF]
1097 ");
1098
1099 // trailer is added added when editing an empty description
1100 work_dir.run_jj(["new"]).success();
1101 std::fs::write(&edit_script, "dump editor0")?;
1102 let output = work_dir.run_jj(["describe"]);
1103 insta::assert_snapshot!(output, @"
1104 ------- stderr -------
1105 Working copy (@) now at: vruxwmqv b6148729 (empty) Signed-off-by: [email protected]
1106 Parent commit (@-) : qpvuntsm b7dafa2c (empty) Message from CLI
1107 [EOF]
1108 ");
1109
1110 let editor0 = std::fs::read_to_string(test_env.env_root().join("editor0"))?;
1111 insta::assert_snapshot!(
1112 format!("-----\n{editor0}-----\n"), @r#"
1113 -----
1114
1115
1116 Signed-off-by: [email protected]
1117
1118 JJ: Change ID: vruxwmqv
1119 JJ:
1120 JJ: Lines starting with "JJ:" (like this one) will be removed.
1121 -----
1122 "#);
1123 Ok(())
1124 }
1125
1126 #[must_use]
1127 fn get_log_output(work_dir: &TestWorkDir) -> CommandOutput {
1128 let template = r#"commit_id.short() ++ " " ++ description"#;
1129 work_dir.run_jj(["log", "-T", template])
1130 }