From 9607d954e4e0677a2710d36e44051a0c43801490 Mon Sep 17 00:00:00 2001 From: Waleed Khan Date: Fri, 18 Nov 2022 12:50:39 -0800 Subject: [PATCH] tests: assert result of `TestEnvironment::jj_cmd_cli_error` In the test case `test_branch_mutually_exclusive_actions`, we weren't actually testing anything useful, because the interface has since changed to use subcommands instead of options. The test has been deleted in this commit, and `TestEnvironment::jj_cmd_cli_error` has been changed to return a `#[must_use]` `String` representing stderr. I also added `#[must_use]` to `TestEnvironment::jj_cmd_failure` while I was here. --- tests/common/mod.rs | 8 ++++++-- tests/test_alias.rs | 10 ++++++++-- tests/test_branch_command.rs | 10 ---------- tests/test_edit_command.rs | 10 +++++++++- tests/test_global_opts.rs | 2 +- tests/test_move_command.rs | 10 +++++++++- tests/test_new_command.rs | 15 ++++++++++++--- tests/test_rebase_command.rs | 36 +++++++++++++++++++++++++++++++---- tests/test_restore_command.rs | 9 ++++++++- tests/test_untrack_command.rs | 12 ++++++++++-- 10 files changed, 95 insertions(+), 27 deletions(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index f6fcc22da..3b1ef433a 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -83,6 +83,7 @@ impl TestEnvironment { /// Run a `jj` command, check that it failed with code 1, and return its /// stderr + #[must_use] pub fn jj_cmd_failure(&self, current_dir: &Path, args: &[&str]) -> String { let assert = self.jj_cmd(current_dir, args).assert().code(1).stdout(""); self.normalize_output(get_stderr_string(&assert)) @@ -90,8 +91,10 @@ impl TestEnvironment { /// Run a `jj` command and check that it failed with code 2 (for invalid /// usage) - pub fn jj_cmd_cli_error(&self, current_dir: &Path, args: &[&str]) { - self.jj_cmd(current_dir, args).assert().code(2).stdout(""); + #[must_use] + pub fn jj_cmd_cli_error(&self, current_dir: &Path, args: &[&str]) -> String { + let assert = self.jj_cmd(current_dir, args).assert().code(2).stdout(""); + self.normalize_output(get_stderr_string(&assert)) } pub fn env_root(&self) -> &Path { @@ -157,6 +160,7 @@ impl TestEnvironment { } pub fn normalize_output(&self, text: String) -> String { + let text = text.replace("jj.exe", "jj"); let regex = Regex::new(&format!( r"{}(\S+)", regex::escape(&self.env_root.display().to_string()) diff --git a/tests/test_alias.rs b/tests/test_alias.rs index 6cd403ca0..77afa8229 100644 --- a/tests/test_alias.rs +++ b/tests/test_alias.rs @@ -46,8 +46,14 @@ fn test_alias_calls_unknown_command() { foo = ["nonexistent"] "#, ); - // Should get an error about the unknown command - test_env.jj_cmd_cli_error(&repo_path, &["foo"]); + let stderr = test_env.jj_cmd_cli_error(&repo_path, &["foo"]); + insta::assert_snapshot!(stderr, @r###" + error: The subcommand 'nonexistent' wasn't recognized + + Usage: jj [OPTIONS] [COMMAND] + + For more information try '--help' + "###); } #[test] diff --git a/tests/test_branch_command.rs b/tests/test_branch_command.rs index 8da98ccba..f86052804 100644 --- a/tests/test_branch_command.rs +++ b/tests/test_branch_command.rs @@ -18,16 +18,6 @@ use crate::common::{get_stderr_string, get_stdout_string, TestEnvironment}; pub mod common; -#[test] -fn test_branch_mutually_exclusive_actions() { - let test_env = TestEnvironment::default(); - test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); - let repo_path = test_env.env_root().join("repo"); - - test_env.jj_cmd_cli_error(&repo_path, &["branch", "--forget", "--delete", "foo"]); - test_env.jj_cmd_cli_error(&repo_path, &["branch", "--allow-backwards", "foo"]); -} - #[test] fn test_branch_multiple_names() { let test_env = TestEnvironment::default(); diff --git a/tests/test_edit_command.rs b/tests/test_edit_command.rs index 01b2e3840..a0d6f96dc 100644 --- a/tests/test_edit_command.rs +++ b/tests/test_edit_command.rs @@ -29,7 +29,15 @@ fn test_edit() { std::fs::write(repo_path.join("file1"), "1").unwrap(); // Errors out without argument - test_env.jj_cmd_cli_error(&repo_path, &["edit"]); + let stderr = test_env.jj_cmd_cli_error(&repo_path, &["edit"]); + insta::assert_snapshot!(stderr, @r###" + error: The following required arguments were not provided: + + + Usage: jj edit + + For more information try '--help' + "###); // Makes the specified commit the working-copy commit let stdout = test_env.jj_cmd_success(&repo_path, &["edit", "@-"]); diff --git a/tests/test_global_opts.rs b/tests/test_global_opts.rs index 04c52d831..ba9fe0bf8 100644 --- a/tests/test_global_opts.rs +++ b/tests/test_global_opts.rs @@ -235,7 +235,7 @@ fn test_help() { let test_env = TestEnvironment::default(); let stdout = test_env.jj_cmd_success(test_env.env_root(), &["touchup", "-h"]); - insta::assert_snapshot!(stdout.replace(".exe", ""), @r###" + insta::assert_snapshot!(stdout, @r###" Touch up the content changes in a revision Usage: jj touchup [OPTIONS] diff --git a/tests/test_move_command.rs b/tests/test_move_command.rs index cc6bda222..163e2066d 100644 --- a/tests/test_move_command.rs +++ b/tests/test_move_command.rs @@ -68,7 +68,15 @@ fn test_move() { "###); // Errors out without arguments - test_env.jj_cmd_cli_error(&repo_path, &["move"]); + let stderr = test_env.jj_cmd_cli_error(&repo_path, &["move"]); + insta::assert_snapshot!(stderr, @r###" + error: The following required arguments were not provided: + <--from |--to > + + Usage: jj move <--from |--to > [PATHS]... + + For more information try '--help' + "###); // Errors out if source and destination are the same let stderr = test_env.jj_cmd_failure(&repo_path, &["move", "--to", "@"]); insta::assert_snapshot!(stderr, @r###" diff --git a/tests/test_new_command.rs b/tests/test_new_command.rs index 4b19e488c..f56843fa8 100644 --- a/tests/test_new_command.rs +++ b/tests/test_new_command.rs @@ -84,8 +84,14 @@ fn test_new_merge() { "###); // `jj merge` with less than two arguments is an error - test_env.jj_cmd_cli_error(&repo_path, &["merge"]); - test_env.jj_cmd_cli_error(&repo_path, &["merge", "main"]); + let stderr = test_env.jj_cmd_cli_error(&repo_path, &["merge"]); + insta::assert_snapshot!(stderr, @r###" + Error: Merge requires at least two revisions + "###); + let stderr = test_env.jj_cmd_cli_error(&repo_path, &["merge", "main"]); + insta::assert_snapshot!(stderr, @r###" + Error: Merge requires at least two revisions + "###); // merge with non-unique revisions let stderr = test_env.jj_cmd_failure(&repo_path, &["new", "@", "c34d"]); @@ -94,7 +100,10 @@ fn test_new_merge() { "###); // merge with root - test_env.jj_cmd_failure(&repo_path, &["new", "@", "root"]); + let stderr = test_env.jj_cmd_failure(&repo_path, &["new", "@", "root"]); + insta::assert_snapshot!(stderr, @r###" + Error: Cannot merge with root revision + "###); } fn get_log_output(test_env: &TestEnvironment, repo_path: &Path) -> String { diff --git a/tests/test_rebase_command.rs b/tests/test_rebase_command.rs index d2b4ad540..0811645d8 100644 --- a/tests/test_rebase_command.rs +++ b/tests/test_rebase_command.rs @@ -40,13 +40,37 @@ fn test_rebase_invalid() { create_commit(&test_env, &repo_path, "b", &["a"]); // Missing destination - test_env.jj_cmd_cli_error(&repo_path, &["rebase"]); + let stderr = test_env.jj_cmd_cli_error(&repo_path, &["rebase"]); + insta::assert_snapshot!(stderr, @r###" + error: The following required arguments were not provided: + --destination + + Usage: jj rebase --destination + + For more information try '--help' + "###); // Both -r and -s - test_env.jj_cmd_cli_error(&repo_path, &["rebase", "-r", "a", "-s", "a", "-d", "b"]); + let stderr = + test_env.jj_cmd_cli_error(&repo_path, &["rebase", "-r", "a", "-s", "a", "-d", "b"]); + insta::assert_snapshot!(stderr, @r###" + error: The argument '--revision ' cannot be used with '--source ' + + Usage: jj rebase --destination --revision + + For more information try '--help' + "###); // Both -b and -s - test_env.jj_cmd_cli_error(&repo_path, &["rebase", "-b", "a", "-s", "a", "-d", "b"]); + let stderr = + test_env.jj_cmd_cli_error(&repo_path, &["rebase", "-b", "a", "-s", "a", "-d", "b"]); + insta::assert_snapshot!(stderr, @r###" + error: The argument '--branch ' cannot be used with '--source ' + + Usage: jj rebase --destination --branch + + For more information try '--help' + "###); // Rebase onto descendant with -r let stderr = test_env.jj_cmd_failure(&repo_path, &["rebase", "-r", "a", "-d", "b"]); @@ -290,7 +314,11 @@ fn test_rebase_multiple_destinations() { o "###); - test_env.jj_cmd_failure(&repo_path, &["rebase", "-r", "a", "-d", "b", "-d", "root"]); + let stderr = + test_env.jj_cmd_failure(&repo_path, &["rebase", "-r", "a", "-d", "b", "-d", "root"]); + insta::assert_snapshot!(stderr, @r###" + Error: Cannot merge with root revision + "###); } #[test] diff --git a/tests/test_restore_command.rs b/tests/test_restore_command.rs index 79518080e..877bfc0ba 100644 --- a/tests/test_restore_command.rs +++ b/tests/test_restore_command.rs @@ -191,5 +191,12 @@ fn test_restore_interactive() { // Combining paths with -i is not yet supported std::fs::write(&edit_script, "").unwrap(); - test_env.jj_cmd_cli_error(&repo_path, &["restore", "-i", "file2"]); + let stderr = test_env.jj_cmd_cli_error(&repo_path, &["restore", "-i", "file2"]); + insta::assert_snapshot!(stderr, @r###" + error: The argument '--interactive' cannot be used with '[PATHS]...' + + Usage: jj restore --interactive [PATHS]... + + For more information try '--help' + "###); } diff --git a/tests/test_untrack_command.rs b/tests/test_untrack_command.rs index 23bc0114c..0bd40c3d0 100644 --- a/tests/test_untrack_command.rs +++ b/tests/test_untrack_command.rs @@ -45,12 +45,20 @@ fn test_untrack() { // Errors out when not run at the head operation let stderr = test_env.jj_cmd_failure(&repo_path, &["untrack", "file1", "--at-op", "@-"]); - insta::assert_snapshot!(stderr.replace("jj.exe", "jj"), @r###" + insta::assert_snapshot!(stderr, @r###" Error: This command must be able to update the working copy. Hint: Don't use --at-op. "###); // Errors out when no path is specified - test_env.jj_cmd_cli_error(&repo_path, &["untrack"]); + let stderr = test_env.jj_cmd_cli_error(&repo_path, &["untrack"]); + insta::assert_snapshot!(stderr, @r###" + error: The following required arguments were not provided: + ... + + Usage: jj untrack ... + + For more information try '--help' + "###); // Errors out when a specified file is not ignored let stderr = test_env.jj_cmd_failure(&repo_path, &["untrack", "file1", "file1.bak"]); insta::assert_snapshot!(stderr, @r###"