Skip to content

Commit

Permalink
tests: add [EOF] marker to command output when displaying
Browse files Browse the repository at this point in the history
It's important to test that command output is (usually) terminated with newline,
but insta::assert_snapshot!() is lax about that.
  • Loading branch information
yuja committed Feb 17, 2025
1 parent 6b0717c commit 6113486
Show file tree
Hide file tree
Showing 67 changed files with 7,531 additions and 4,423 deletions.
6 changes: 5 additions & 1 deletion cli/tests/common/test_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,11 @@ impl CommandOutputString {

impl Display for CommandOutputString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.normalized)
if self.is_empty() {
return Ok(());
}
// Append "[EOF]" marker to test line ending
writeln!(f, "{}[EOF]", self.normalized)
}
}

Expand Down
99 changes: 64 additions & 35 deletions cli/tests/test_abandon_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn test_basics() {
create_commit(&test_env, &repo_path, "d", &["c"]);
create_commit(&test_env, &repo_path, "e", &["a", "d"]);
// Test the setup
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r"
@ [znk] e
├─╮
│ ○ [vru] d
Expand All @@ -51,19 +51,21 @@ fn test_basics() {
○ │ [rlv] a
├─╯
◆ [zzz]
"###);
[EOF]
");

let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", "--retain-bookmarks", "d"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
insta::assert_snapshot!(stderr, @r"
Abandoned commit vruxwmqv b7c62f28 d | d
Rebased 1 descendant commits onto parents of abandoned commits
Working copy now at: znkkpsqq 11a2e10e e | e
Parent commit : rlvkpnrz 2443ea76 a | a
Parent commit : royxmykx fe2e8e8b c d | c
Added 0 files, modified 0 files, removed 1 files
"###);
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
[EOF]
");
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r"
@ [znk] e
├─╮
│ ○ [roy] c d
Expand All @@ -72,22 +74,24 @@ fn test_basics() {
○ │ [rlv] a
├─╯
◆ [zzz]
"###);
[EOF]
");

test_env.jj_cmd_ok(&repo_path, &["undo"]);
let (stdout, stderr) = test_env.jj_cmd_ok(
&repo_path,
&["abandon", "--retain-bookmarks"], /* abandons `e` */
);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
insta::assert_snapshot!(stderr, @r"
Abandoned commit znkkpsqq 5557ece3 e | e
Working copy now at: nkmrtpmo d4f8ea73 (empty) (no description set)
Parent commit : rlvkpnrz 2443ea76 a e?? | a
Parent commit : vruxwmqv b7c62f28 d e?? | d
Added 0 files, modified 0 files, removed 1 files
"###);
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
[EOF]
");
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r"
@ [nkm]
├─╮
│ ○ [vru] d e??
Expand All @@ -97,7 +101,8 @@ fn test_basics() {
○ │ [rlv] a e??
├─╯
◆ [zzz]
"###);
[EOF]
");

test_env.jj_cmd_ok(&repo_path, &["undo"]);
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", "descendants(d)"]);
Expand All @@ -111,6 +116,7 @@ fn test_basics() {
Parent commit : rlvkpnrz 2443ea76 a | a
Parent commit : royxmykx fe2e8e8b c | c
Added 0 files, modified 0 files, removed 2 files
[EOF]
");
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r"
@ [xtn]
Expand All @@ -121,6 +127,7 @@ fn test_basics() {
○ │ [rlv] a
├─╯
◆ [zzz]
[EOF]
");

// Test abandoning the same commit twice directly
Expand All @@ -130,6 +137,7 @@ fn test_basics() {
insta::assert_snapshot!(stderr, @r"
Abandoned commit zsuskuln 1394f625 b | b
Deleted bookmarks: b
[EOF]
");
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r"
@ [znk] e
Expand All @@ -139,6 +147,7 @@ fn test_basics() {
○ │ [rlv] a
├─╯
◆ [zzz]
[EOF]
");

// Test abandoning the same commit twice indirectly
Expand All @@ -154,6 +163,7 @@ fn test_basics() {
Parent commit : rlvkpnrz 2443ea76 a | a
Parent commit : royxmykx fe2e8e8b c | c
Added 0 files, modified 0 files, removed 2 files
[EOF]
");
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r"
@ [xlz]
Expand All @@ -164,12 +174,14 @@ fn test_basics() {
○ │ [rlv] a
├─╯
◆ [zzz]
[EOF]
");

let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", "none()"]);
insta::assert_snapshot!(stderr, @r###"
insta::assert_snapshot!(stderr, @r"
No revisions to abandon.
"###);
[EOF]
");
}

// This behavior illustrates https://github.com/jj-vcs/jj/issues/2600.
Expand All @@ -190,7 +202,7 @@ fn test_bug_2600() {
create_commit(&test_env, &repo_path, "c", &["b"]);

// Test the setup
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r"
@ [znk] c
○ [vru] b
├─╮
Expand All @@ -199,7 +211,8 @@ fn test_bug_2600() {
○ [zsu] base
○ [rlv] nottherootcommit
◆ [zzz]
"###);
[EOF]
");
let setup_opid = test_env.current_operation_id(&repo_path);

test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]);
Expand All @@ -212,6 +225,7 @@ fn test_bug_2600() {
Working copy now at: znkkpsqq 86e31bec c | c
Parent commit : vruxwmqv fd6eb121 b | b
Added 0 files, modified 0 files, removed 1 files
[EOF]
");
// Commits "a" and "b" should both have "nottherootcommit" as parent, and "b"
// should keep "a" as second parent.
Expand All @@ -223,6 +237,7 @@ fn test_bug_2600() {
├─╯
○ [rlv] nottherootcommit
◆ [zzz]
[EOF]
");

test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]);
Expand All @@ -235,6 +250,7 @@ fn test_bug_2600() {
Working copy now at: znkkpsqq 683b9435 c | c
Parent commit : vruxwmqv c10cb7b4 b | b
Added 0 files, modified 0 files, removed 1 files
[EOF]
");
// Commit "b" should have "base" as parent. It should not have two parent
// pointers to that commit even though it was a merge commit before we abandoned
Expand All @@ -245,6 +261,7 @@ fn test_bug_2600() {
○ [zsu] base
○ [rlv] nottherootcommit
◆ [zzz]
[EOF]
");

test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]);
Expand All @@ -258,6 +275,7 @@ fn test_bug_2600() {
Parent commit : zsuskuln 73c929fc base | base
Parent commit : royxmykx 98f3b9ba a | a
Added 0 files, modified 0 files, removed 1 files
[EOF]
");
// Commit "c" should inherit the parents from the abndoned commit "b".
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r"
Expand All @@ -268,11 +286,12 @@ fn test_bug_2600() {
○ [zsu] base
○ [rlv] nottherootcommit
◆ [zzz]
[EOF]
");

test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]);
// ========= Reminder of the setup ===========
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r"
@ [znk] c
○ [vru] b
├─╮
Expand All @@ -281,31 +300,35 @@ fn test_bug_2600() {
○ [zsu] base
○ [rlv] nottherootcommit
◆ [zzz]
"###);
[EOF]
");
let (stdout, stderr) =
test_env.jj_cmd_ok(&repo_path, &["abandon", "--retain-bookmarks", "a", "b"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
insta::assert_snapshot!(stderr, @r"
Abandoned the following commits:
vruxwmqv 8c0dced0 b | b
royxmykx 98f3b9ba a | a
Rebased 1 descendant commits onto parents of abandoned commits
Working copy now at: znkkpsqq 84fac1f8 c | c
Parent commit : zsuskuln 73c929fc a b base | base
Added 0 files, modified 0 files, removed 2 files
"###);
[EOF]
");
// Commit "c" should have "base" as parent. As when we abandoned "a", it should
// not have two parent pointers to the same commit.
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r"
@ [znk] c
○ [zsu] a b base
○ [rlv] nottherootcommit
◆ [zzz]
"###);
[EOF]
");
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["bookmark", "list", "b"]);
insta::assert_snapshot!(stdout, @r###"
insta::assert_snapshot!(stdout, @r"
b: zsuskuln 73c929fc base
"###);
[EOF]
");
insta::assert_snapshot!(stderr, @"");
}

Expand All @@ -322,21 +345,23 @@ fn test_bug_2600_rootcommit_special_case() {
create_commit(&test_env, &repo_path, "c", &["b"]);

// Setup
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r"
@ [vru] c
○ [roy] b
├─╮
│ ○ [zsu] a
├─╯
○ [rlv] base
◆ [zzz]
"###);
[EOF]
");

// Now, the test
let stderr = test_env.jj_cmd_failure(&repo_path, &["abandon", "base"]);
insta::assert_snapshot!(stderr, @r###"
insta::assert_snapshot!(stderr, @r"
Error: The Git backend does not support creating merge commits with the root commit as one of the parents.
"###);
[EOF]
");
}

#[test]
Expand All @@ -348,11 +373,11 @@ fn test_double_abandon() {
create_commit(&test_env, &repo_path, "a", &[]);
// Test the setup
insta::assert_snapshot!(
test_env.jj_cmd_success(&repo_path, &["log", "--no-graph", "-r", "a"])
, @r###"
test_env.jj_cmd_success(&repo_path, &["log", "--no-graph", "-r", "a"]), @r"
rlvkpnrz [email protected] 2001-02-03 08:05:09 a 2443ea76
a
"###);
[EOF]
");

let commit_id = test_env
.jj_cmd_success(
Expand All @@ -369,13 +394,15 @@ fn test_double_abandon() {
Working copy now at: royxmykx f37b4afd (empty) (no description set)
Parent commit : zzzzzzzz 00000000 (empty) (no description set)
Added 0 files, modified 0 files, removed 1 files
[EOF]
");
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", &commit_id]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
insta::assert_snapshot!(stderr, @r"
Abandoned commit rlvkpnrz hidden 2443ea76 a
Nothing changed.
"###);
[EOF]
");
}

#[test]
Expand All @@ -394,22 +421,24 @@ fn test_abandon_restore_descendants() {
let (stdout, stderr) =
test_env.jj_cmd_ok(&repo_path, &["abandon", "-r@-", "--restore-descendants"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r#"
insta::assert_snapshot!(stderr, @r"
Abandoned commit rlvkpnrz 225adef1 (no description set)
Rebased 1 descendant commits (while preserving their content) onto parents of abandoned commits
Working copy now at: kkmpptxz a734deb0 (no description set)
Parent commit : qpvuntsm 485d52a9 (no description set)
"#);
[EOF]
");
let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "--git"]);
insta::assert_snapshot!(stdout, @r#"
insta::assert_snapshot!(stdout, @r"
diff --git a/file b/file
index 257cc5642c..76018072e0 100644
--- a/file
+++ b/file
@@ -1,1 +1,1 @@
-foo
+baz
"#);
[EOF]
");
}

fn get_log_output(test_env: &TestEnvironment, repo_path: &Path) -> CommandOutputString {
Expand Down
Loading

0 comments on commit 6113486

Please sign in to comment.