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.
This commit is contained in:
Waleed Khan 2022-11-18 12:50:39 -08:00
parent 6d500ed66c
commit 9607d954e4
10 changed files with 95 additions and 27 deletions

View File

@ -83,6 +83,7 @@ impl TestEnvironment {
/// Run a `jj` command, check that it failed with code 1, and return its /// Run a `jj` command, check that it failed with code 1, and return its
/// stderr /// stderr
#[must_use]
pub fn jj_cmd_failure(&self, current_dir: &Path, args: &[&str]) -> String { pub fn jj_cmd_failure(&self, current_dir: &Path, args: &[&str]) -> String {
let assert = self.jj_cmd(current_dir, args).assert().code(1).stdout(""); let assert = self.jj_cmd(current_dir, args).assert().code(1).stdout("");
self.normalize_output(get_stderr_string(&assert)) 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 /// Run a `jj` command and check that it failed with code 2 (for invalid
/// usage) /// usage)
pub fn jj_cmd_cli_error(&self, current_dir: &Path, args: &[&str]) { #[must_use]
self.jj_cmd(current_dir, args).assert().code(2).stdout(""); 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 { pub fn env_root(&self) -> &Path {
@ -157,6 +160,7 @@ impl TestEnvironment {
} }
pub fn normalize_output(&self, text: String) -> String { pub fn normalize_output(&self, text: String) -> String {
let text = text.replace("jj.exe", "jj");
let regex = Regex::new(&format!( let regex = Regex::new(&format!(
r"{}(\S+)", r"{}(\S+)",
regex::escape(&self.env_root.display().to_string()) regex::escape(&self.env_root.display().to_string())

View File

@ -46,8 +46,14 @@ fn test_alias_calls_unknown_command() {
foo = ["nonexistent"] foo = ["nonexistent"]
"#, "#,
); );
// Should get an error about the unknown command let stderr = test_env.jj_cmd_cli_error(&repo_path, &["foo"]);
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] #[test]

View File

@ -18,16 +18,6 @@ use crate::common::{get_stderr_string, get_stdout_string, TestEnvironment};
pub mod common; 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] #[test]
fn test_branch_multiple_names() { fn test_branch_multiple_names() {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();

View File

@ -29,7 +29,15 @@ fn test_edit() {
std::fs::write(repo_path.join("file1"), "1").unwrap(); std::fs::write(repo_path.join("file1"), "1").unwrap();
// Errors out without argument // 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:
<REVISION>
Usage: jj edit <REVISION>
For more information try '--help'
"###);
// Makes the specified commit the working-copy commit // Makes the specified commit the working-copy commit
let stdout = test_env.jj_cmd_success(&repo_path, &["edit", "@-"]); let stdout = test_env.jj_cmd_success(&repo_path, &["edit", "@-"]);

View File

@ -235,7 +235,7 @@ fn test_help() {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["touchup", "-h"]); 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 Touch up the content changes in a revision
Usage: jj touchup [OPTIONS] Usage: jj touchup [OPTIONS]

View File

@ -68,7 +68,15 @@ fn test_move() {
"###); "###);
// Errors out without arguments // 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 <FROM>|--to <TO>>
Usage: jj move <--from <FROM>|--to <TO>> [PATHS]...
For more information try '--help'
"###);
// Errors out if source and destination are the same // Errors out if source and destination are the same
let stderr = test_env.jj_cmd_failure(&repo_path, &["move", "--to", "@"]); let stderr = test_env.jj_cmd_failure(&repo_path, &["move", "--to", "@"]);
insta::assert_snapshot!(stderr, @r###" insta::assert_snapshot!(stderr, @r###"

View File

@ -84,8 +84,14 @@ fn test_new_merge() {
"###); "###);
// `jj merge` with less than two arguments is an error // `jj merge` with less than two arguments is an error
test_env.jj_cmd_cli_error(&repo_path, &["merge"]); let stderr = test_env.jj_cmd_cli_error(&repo_path, &["merge"]);
test_env.jj_cmd_cli_error(&repo_path, &["merge", "main"]); 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 // merge with non-unique revisions
let stderr = test_env.jj_cmd_failure(&repo_path, &["new", "@", "c34d"]); let stderr = test_env.jj_cmd_failure(&repo_path, &["new", "@", "c34d"]);
@ -94,7 +100,10 @@ fn test_new_merge() {
"###); "###);
// merge with root // 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 { fn get_log_output(test_env: &TestEnvironment, repo_path: &Path) -> String {

View File

@ -40,13 +40,37 @@ fn test_rebase_invalid() {
create_commit(&test_env, &repo_path, "b", &["a"]); create_commit(&test_env, &repo_path, "b", &["a"]);
// Missing destination // 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 <DESTINATION>
Usage: jj rebase --destination <DESTINATION>
For more information try '--help'
"###);
// Both -r and -s // 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 <REVISION>' cannot be used with '--source <SOURCE>'
Usage: jj rebase --destination <DESTINATION> --revision <REVISION>
For more information try '--help'
"###);
// Both -b and -s // 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 <BRANCH>' cannot be used with '--source <SOURCE>'
Usage: jj rebase --destination <DESTINATION> --branch <BRANCH>
For more information try '--help'
"###);
// Rebase onto descendant with -r // Rebase onto descendant with -r
let stderr = test_env.jj_cmd_failure(&repo_path, &["rebase", "-r", "a", "-d", "b"]); let stderr = test_env.jj_cmd_failure(&repo_path, &["rebase", "-r", "a", "-d", "b"]);
@ -290,7 +314,11 @@ fn test_rebase_multiple_destinations() {
o 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] #[test]

View File

@ -191,5 +191,12 @@ fn test_restore_interactive() {
// Combining paths with -i is not yet supported // Combining paths with -i is not yet supported
std::fs::write(&edit_script, "").unwrap(); 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'
"###);
} }

View File

@ -45,12 +45,20 @@ fn test_untrack() {
// Errors out when not run at the head operation // Errors out when not run at the head operation
let stderr = test_env.jj_cmd_failure(&repo_path, &["untrack", "file1", "--at-op", "@-"]); 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. Error: This command must be able to update the working copy.
Hint: Don't use --at-op. Hint: Don't use --at-op.
"###); "###);
// Errors out when no path is specified // 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:
<PATHS>...
Usage: jj untrack <PATHS>...
For more information try '--help'
"###);
// Errors out when a specified file is not ignored // Errors out when a specified file is not ignored
let stderr = test_env.jj_cmd_failure(&repo_path, &["untrack", "file1", "file1.bak"]); let stderr = test_env.jj_cmd_failure(&repo_path, &["untrack", "file1", "file1.bak"]);
insta::assert_snapshot!(stderr, @r###" insta::assert_snapshot!(stderr, @r###"