From 507662259817fc2ef07f62fa41364ed05238373f Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 22 Dec 2022 16:58:23 +0900 Subject: [PATCH] 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. --- tests/test_commit_command.rs | 17 ++++----- tests/test_describe_command.rs | 16 ++++----- tests/test_move_command.rs | 26 +++++++------- tests/test_resolve_command.rs | 65 ++++++++++++++++++++++------------ tests/test_split_command.rs | 37 ++++++++++--------- tests/test_squash_command.rs | 26 +++++++------- tests/test_unsquash_command.rs | 26 +++++++------- 7 files changed, 112 insertions(+), 101 deletions(-) diff --git a/tests/test_commit_command.rs b/tests/test_commit_command.rs index 043f8b3d2..64f8c51d2 100644 --- a/tests/test_commit_command.rs +++ b/tests/test_commit_command.rs @@ -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] diff --git a/tests/test_describe_command.rs b/tests/test_describe_command.rs index 7298e39ca..d122e7aae 100644 --- a/tests/test_describe_command.rs +++ b/tests/test_describe_command.rs @@ -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(); diff --git a/tests/test_move_command.rs b/tests/test_move_command.rs index 8d55d7617..450e8f0dc 100644 --- a/tests/test_move_command.rs +++ b/tests/test_move_command.rs @@ -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 diff --git a/tests/test_resolve_command.rs b/tests/test_resolve_command.rs index 7062e56e3..9806d4b42 100644 --- a/tests/test_resolve_command.rs +++ b/tests/test_resolve_command.rs @@ -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###" diff --git a/tests/test_split_command.rs b/tests/test_split_command.rs index f04e2f639..f15ac7479 100644 --- a/tests/test_split_command.rs +++ b/tests/test_split_command.rs @@ -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###" diff --git a/tests/test_squash_command.rs b/tests/test_squash_command.rs index e6e426b5b..a9e5e2d8b 100644 --- a/tests/test_squash_command.rs +++ b/tests/test_squash_command.rs @@ -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 diff --git a/tests/test_unsquash_command.rs b/tests/test_unsquash_command.rs index b6022caad..17177954c 100644 --- a/tests/test_unsquash_command.rs +++ b/tests/test_unsquash_command.rs @@ -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