tests: use insta to test editor contents

It's tedious to update editor expectation manually. Let's dump the actual
content and test it after each command invocation.
This commit is contained in:
Yuya Nishihara 2022-12-22 16:58:23 +09:00
parent 1cb1a1f1dc
commit 5076622598
7 changed files with 112 additions and 101 deletions

View file

@ -43,22 +43,19 @@ fn test_commit_with_editor() {
// set a new one
test_env.jj_cmd_success(&workspace_path, &["describe", "-m=initial"]);
let edit_script = test_env.set_up_fake_editor();
std::fs::write(
edit_script,
"expect
initial
JJ: Lines starting with \"JJ: \" (like this one) will be removed.
\0write
modified",
)
.unwrap();
std::fs::write(edit_script, ["dump editor0", "write\nmodified"].join("\0")).unwrap();
test_env.jj_cmd_success(&workspace_path, &["commit"]);
insta::assert_snapshot!(get_log_output(&test_env, &workspace_path), @r###"
@ 3df78bc2b9b5 (no description set)
o 30a8c2b3d6eb modified
o 000000000000 (no description set)
"###);
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r###"
initial
JJ: Lines starting with "JJ: " (like this one) will be removed.
"###);
}
#[test]

View file

@ -38,19 +38,17 @@ fn test_describe() {
// Check that the text file gets initialized with the current description and
// make no changes
std::fs::write(
&edit_script,
r#"expect
description from CLI
JJ: Lines starting with "JJ: " (like this one) will be removed.
"#,
)
.unwrap();
std::fs::write(&edit_script, "dump editor0").unwrap();
let stdout = test_env.jj_cmd_success(&repo_path, &["describe"]);
insta::assert_snapshot!(stdout, @r###"
Nothing changed.
"###);
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r###"
description from CLI
JJ: Lines starting with "JJ: " (like this one) will be removed.
"###);
// Set a description in editor
std::fs::write(&edit_script, "write\ndescription from editor").unwrap();

View file

@ -365,26 +365,24 @@ fn test_move_description() {
// If both descriptions were non-empty, we get asked for a combined description
test_env.jj_cmd_success(&repo_path, &["undo"]);
test_env.jj_cmd_success(&repo_path, &["describe", "-m", "source"]);
std::fs::write(
&edit_script,
r#"expect
JJ: Enter a description for the combined commit.
JJ: Description from the destination commit:
destination
JJ: Description from the source commit:
source
JJ: Lines starting with "JJ: " (like this one) will be removed.
"#,
)
.unwrap();
std::fs::write(&edit_script, "dump editor0").unwrap();
test_env.jj_cmd_success(&repo_path, &["move", "--to", "@-"]);
insta::assert_snapshot!(get_description(&test_env, &repo_path, "@-"), @r###"
destination
source
"###);
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r###"
JJ: Enter a description for the combined commit.
JJ: Description from the destination commit:
destination
JJ: Description from the source commit:
source
JJ: Lines starting with "JJ: " (like this one) will be removed.
"###);
// If the source's *content* doesn't become empty, then the source remains and
// both descriptions are unchanged

View file

@ -80,8 +80,15 @@ fn test_resolution() {
let editor_script = test_env.set_up_fake_editor();
// Check that output file starts out empty and resolve the conflict
std::fs::write(&editor_script, "expect\n\0write\nresolution\n").unwrap();
std::fs::write(
&editor_script,
["dump editor0", "write\nresolution\n"].join("\0"),
)
.unwrap();
test_env.jj_cmd_success(&repo_path, &["resolve"]);
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r###"
"###);
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff"]),
@r###"
Resolved conflict in file:
@ -105,17 +112,7 @@ fn test_resolution() {
@"");
std::fs::write(
&editor_script,
"expect
<<<<<<<
%%%%%%%
-base
+a
+++++++
b
>>>>>>>
\0write
resolution
",
["dump editor1", "write\nresolution\n"].join("\0"),
)
.unwrap();
test_env.jj_cmd_success(
@ -126,6 +123,16 @@ resolution
"merge-tools.fake-editor.merge-tool-edits-conflict-markers=true",
],
);
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor1")).unwrap(), @r###"
<<<<<<<
%%%%%%%
-base
+a
+++++++
b
>>>>>>>
"###);
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff"]),
@r###"
Resolved conflict in file:
@ -145,15 +152,9 @@ resolution
@"");
std::fs::write(
&editor_script,
"expect
<<<<<<<
%%%%%%%
-base
+a
+++++++
b
>>>>>>>
\0write
[
"dump editor2",
"write
<<<<<<<
%%%%%%%
-some
@ -162,6 +163,8 @@ b
conflict
>>>>>>>
",
]
.join("\0"),
)
.unwrap();
test_env.jj_cmd_success(
@ -172,6 +175,16 @@ conflict
"merge-tools.fake-editor.merge-tool-edits-conflict-markers=true",
],
);
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor2")).unwrap(), @r###"
<<<<<<<
%%%%%%%
-base
+a
+++++++
b
>>>>>>>
"###);
// Note the "Modified" below
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff"]),
@r###"
@ -197,8 +210,9 @@ conflict
@"");
std::fs::write(
&editor_script,
"expect
\0write
[
"dump editor3",
"write
<<<<<<<
%%%%%%%
-some
@ -207,9 +221,14 @@ conflict
conflict
>>>>>>>
",
]
.join("\0"),
)
.unwrap();
test_env.jj_cmd_success(&repo_path, &["resolve"]);
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor3")).unwrap(), @r###"
"###);
// Note the "Resolved" below
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff"]),
@r###"

View file

@ -35,23 +35,7 @@ fn test_split_by_paths() {
let edit_script = test_env.set_up_fake_editor();
std::fs::write(
edit_script,
"expect
JJ: Enter commit description for the first part (parent).
JJ: This part contains the following changes:
JJ: A file2
JJ: Lines starting with \"JJ: \" (like this one) will be removed.
\0next invocation
\0expect
JJ: Enter commit description for the second part (child).
JJ: This part contains the following changes:
JJ: A file1
JJ: A file3
JJ: Lines starting with \"JJ: \" (like this one) will be removed.
",
["dump editor0", "next invocation\n", "dump editor1"].join("\0"),
)
.unwrap();
let stdout = test_env.jj_cmd_success(&repo_path, &["split", "file2"]);
@ -60,6 +44,25 @@ JJ: Lines starting with \"JJ: \" (like this one) will be removed.
Second part: 45833353d94e (no description set)
Working copy now at: 45833353d94e (no description set)
"###);
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r###"
JJ: Enter commit description for the first part (parent).
JJ: This part contains the following changes:
JJ: A file2
JJ: Lines starting with "JJ: " (like this one) will be removed.
"###);
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor1")).unwrap(), @r###"
JJ: Enter commit description for the second part (child).
JJ: This part contains the following changes:
JJ: A file1
JJ: A file3
JJ: Lines starting with "JJ: " (like this one) will be removed.
"###);
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id.short()"]);
insta::assert_snapshot!(stdout, @r###"

View file

@ -281,26 +281,24 @@ fn test_squash_description() {
// If both descriptions were non-empty, we get asked for a combined description
test_env.jj_cmd_success(&repo_path, &["undo"]);
test_env.jj_cmd_success(&repo_path, &["describe", "-m", "source"]);
std::fs::write(
&edit_script,
r#"expect
JJ: Enter a description for the combined commit.
JJ: Description from the destination commit:
destination
JJ: Description from the source commit:
source
JJ: Lines starting with "JJ: " (like this one) will be removed.
"#,
)
.unwrap();
std::fs::write(&edit_script, "dump editor0").unwrap();
test_env.jj_cmd_success(&repo_path, &["squash"]);
insta::assert_snapshot!(get_description(&test_env, &repo_path, "@-"), @r###"
destination
source
"###);
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r###"
JJ: Enter a description for the combined commit.
JJ: Description from the destination commit:
destination
JJ: Description from the source commit:
source
JJ: Lines starting with "JJ: " (like this one) will be removed.
"###);
// If the source's *content* doesn't become empty, then the source remains and
// both descriptions are unchanged

View file

@ -247,26 +247,24 @@ fn test_unsquash_description() {
// If both descriptions were non-empty, we get asked for a combined description
test_env.jj_cmd_success(&repo_path, &["undo"]);
test_env.jj_cmd_success(&repo_path, &["describe", "@-", "-m", "source"]);
std::fs::write(
&edit_script,
r#"expect
JJ: Enter a description for the combined commit.
JJ: Description from the destination commit:
destination
JJ: Description from the source commit:
source
JJ: Lines starting with "JJ: " (like this one) will be removed.
"#,
)
.unwrap();
std::fs::write(&edit_script, "dump editor0").unwrap();
test_env.jj_cmd_success(&repo_path, &["unsquash"]);
insta::assert_snapshot!(get_description(&test_env, &repo_path, "@"), @r###"
destination
source
"###);
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r###"
JJ: Enter a description for the combined commit.
JJ: Description from the destination commit:
destination
JJ: Description from the source commit:
source
JJ: Lines starting with "JJ: " (like this one) will be removed.
"###);
// If the source's *content* doesn't become empty, then the source remains and
// both descriptions are unchanged