mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 18:27:38 +00:00
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:
parent
6d500ed66c
commit
9607d954e4
10 changed files with 95 additions and 27 deletions
|
@ -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())
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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:
|
||||
<REVISION>
|
||||
|
||||
Usage: jj edit <REVISION>
|
||||
|
||||
For more information try '--help'
|
||||
"###);
|
||||
|
||||
// Makes the specified commit the working-copy commit
|
||||
let stdout = test_env.jj_cmd_success(&repo_path, &["edit", "@-"]);
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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 <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
|
||||
let stderr = test_env.jj_cmd_failure(&repo_path, &["move", "--to", "@"]);
|
||||
insta::assert_snapshot!(stderr, @r###"
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 <DESTINATION>
|
||||
|
||||
Usage: jj rebase --destination <DESTINATION>
|
||||
|
||||
For more information try '--help'
|
||||
"###);
|
||||
|
||||
// Both -r and -s
|
||||
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
|
||||
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
|
||||
let stderr = test_env.jj_cmd_failure(&repo_path, &["rebase", "-r", "a", "-d", "b"]);
|
||||
|
@ -290,7 +314,11 @@ fn test_rebase_multiple_destinations() {
|
|||
o
|
||||
"###);
|
||||
|
||||
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]
|
||||
|
|
|
@ -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'
|
||||
"###);
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
<PATHS>...
|
||||
|
||||
Usage: jj untrack <PATHS>...
|
||||
|
||||
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###"
|
||||
|
|
Loading…
Reference in a new issue