diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 1402fef3f3..028bbec348 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -135,22 +135,30 @@ impl TestEnvironment { cmd } + /// Run a `jj` command, check that it was successful, and return its + /// `(stdout, stderr)`. + pub fn jj_cmd_ok(&self, current_dir: &Path, args: &[&str]) -> (String, String) { + let assert = self.jj_cmd(current_dir, args).assert().success(); + let stdout = self.normalize_output(&get_stdout_string(&assert)); + let stderr = self.normalize_output(&get_stderr_string(&assert)); + (stdout, stderr) + } + /// Run a `jj` command, check that it was successful, and return its stdout pub fn jj_cmd_success(&self, current_dir: &Path, args: &[&str]) -> String { - let assert = if self.debug_allow_stderr { - let a = self.jj_cmd(current_dir, args).assert().success(); - let stderr = self.normalize_output(&get_stderr_string(&a)); + if self.debug_allow_stderr { + let (stdout, stderr) = self.jj_cmd_ok(current_dir, args); if !stderr.is_empty() { eprintln!( "==== STDERR from running jj with {args:?} args in {current_dir:?} \ ====\n{stderr}==== END STDERR ====" ); } - a + stdout } else { - self.jj_cmd(current_dir, args).assert().success().stderr("") - }; - self.normalize_output(&get_stdout_string(&assert)) + let assert = self.jj_cmd(current_dir, args).assert().success().stderr(""); + self.normalize_output(&get_stdout_string(&assert)) + } } /// Run a `jj` command, check that it failed with code 1, and return its diff --git a/tests/test_branch_command.rs b/tests/test_branch_command.rs index 2342202bc0..a4db7d36ea 100644 --- a/tests/test_branch_command.rs +++ b/tests/test_branch_command.rs @@ -14,7 +14,7 @@ use std::path::Path; -use crate::common::{get_stderr_string, get_stdout_string, TestEnvironment}; +use crate::common::TestEnvironment; pub mod common; @@ -24,13 +24,11 @@ fn test_branch_multiple_names() { test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); let repo_path = test_env.env_root().join("repo"); - let assert = test_env - .jj_cmd(&repo_path, &["branch", "set", "foo", "bar"]) - .assert() - .success(); - insta::assert_snapshot!(get_stdout_string(&assert), @""); - insta::assert_snapshot!(get_stderr_string(&assert), @"warning: Updating multiple branches (2). -"); + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "set", "foo", "bar"]); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r###" + warning: Updating multiple branches (2). + "###); insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###" @ bar foo 230dd059e1b0 diff --git a/tests/test_config_command.rs b/tests/test_config_command.rs index 41bd09e7e5..f07d6374a4 100644 --- a/tests/test_config_command.rs +++ b/tests/test_config_command.rs @@ -41,15 +41,12 @@ fn test_config_list_single() { #[test] fn test_config_list_nonexistent() { let test_env = TestEnvironment::default(); - let assert = test_env - .jj_cmd( - test_env.env_root(), - &["config", "list", "nonexistent-test-key"], - ) - .assert() - .success() - .stdout(""); - insta::assert_snapshot!(common::get_stderr_string(&assert), @r###" + let (stdout, stderr) = test_env.jj_cmd_ok( + test_env.env_root(), + &["config", "list", "nonexistent-test-key"], + ); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r###" No matching config key for nonexistent-test-key "###); } diff --git a/tests/test_git_colocated.rs b/tests/test_git_colocated.rs index c481dc6f8c..1a00c5b83f 100644 --- a/tests/test_git_colocated.rs +++ b/tests/test_git_colocated.rs @@ -16,7 +16,7 @@ use std::path::Path; use git2::Oid; -use crate::common::{get_stderr_string, TestEnvironment}; +use crate::common::TestEnvironment; pub mod common; @@ -252,12 +252,9 @@ fn test_git_colocated_conflicting_git_refs() { git2::Repository::init(&workspace_root).unwrap(); test_env.jj_cmd_success(&workspace_root, &["init", "--git-repo", "."]); test_env.jj_cmd_success(&workspace_root, &["branch", "create", "main"]); - let assert = test_env - .jj_cmd(&workspace_root, &["branch", "create", "main/sub"]) - .assert() - .success() - .stdout(""); - insta::assert_snapshot!(get_stderr_string(&assert), @r###" + let (stdout, stderr) = test_env.jj_cmd_ok(&workspace_root, &["branch", "create", "main/sub"]); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r###" Failed to export some branches: main/sub Hint: Git doesn't allow a branch name that looks like a parent directory of diff --git a/tests/test_git_import_export.rs b/tests/test_git_import_export.rs index 5f09dc3543..c8ec4b73f1 100644 --- a/tests/test_git_import_export.rs +++ b/tests/test_git_import_export.rs @@ -16,7 +16,7 @@ use std::path::Path; use itertools::Itertools as _; use jujutsu_lib::backend::{CommitId, ObjectId as _}; -use crate::common::{get_stderr_string, TestEnvironment}; +use crate::common::TestEnvironment; pub mod common; @@ -75,12 +75,9 @@ fn test_git_export_conflicting_git_refs() { test_env.jj_cmd_success(&repo_path, &["branch", "create", "main"]); test_env.jj_cmd_success(&repo_path, &["branch", "create", "main/sub"]); - let assert = test_env - .jj_cmd(&repo_path, &["git", "export"]) - .assert() - .success() - .stdout(""); - insta::assert_snapshot!(get_stderr_string(&assert), @r###" + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["git", "export"]); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r###" Failed to export some branches: main/sub Hint: Git doesn't allow a branch name that looks like a parent directory of diff --git a/tests/test_global_opts.rs b/tests/test_global_opts.rs index 08139346c8..fe48f6800a 100644 --- a/tests/test_global_opts.rs +++ b/tests/test_global_opts.rs @@ -428,12 +428,7 @@ fn test_verbose_logging_enabled() { // Test that the verbose flag enabled verbose logging let test_env = TestEnvironment::default(); - let assert = test_env - .jj_cmd(test_env.env_root(), &["version", "-v"]) - .assert() - .success(); - - let stderr = get_stderr_string(&assert); + let (_stdout, stderr) = test_env.jj_cmd_ok(test_env.env_root(), &["version", "-v"]); // Split the first log line into a timestamp and the rest. // The timestamp is constant sized so this is a robust operation. // Example timestamp: 2022-11-20T06:24:05.477703Z diff --git a/tests/test_init_command.rs b/tests/test_init_command.rs index 06117d807a..c9ac19b154 100644 --- a/tests/test_init_command.rs +++ b/tests/test_init_command.rs @@ -14,7 +14,7 @@ use std::path::PathBuf; -use crate::common::{get_stderr_string, get_stdout_string, TestEnvironment}; +use crate::common::TestEnvironment; pub mod common; @@ -182,14 +182,11 @@ fn test_init_git_internal_but_could_be_colocated() { let workspace_root = test_env.env_root().join("repo"); init_git_repo(&workspace_root); - let assert = test_env - .jj_cmd(&workspace_root, &["init", "--git"]) - .assert() - .success(); - insta::assert_snapshot!(get_stdout_string(&assert), @r###" + let (stdout, stderr) = test_env.jj_cmd_ok(&workspace_root, &["init", "--git"]); + insta::assert_snapshot!(stdout, @r###" Initialized repo in "." "###); - insta::assert_snapshot!(get_stderr_string(&assert), @r###" + insta::assert_snapshot!(stderr, @r###" Empty repo created. Hint: To create a repo backed by the existing Git repo, run `jj init --git-repo=.` instead. "###); diff --git a/tests/test_log_command.rs b/tests/test_log_command.rs index 4d3d5bdad3..ddea1bf6a7 100644 --- a/tests/test_log_command.rs +++ b/tests/test_log_command.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use common::{get_stderr_string, get_stdout_string, TestEnvironment}; +use common::{get_stdout_string, TestEnvironment}; pub mod common; @@ -756,63 +756,51 @@ fn test_log_warn_path_might_be_revset() { std::fs::write(repo_path.join("file1"), "foo\n").unwrap(); // Don't warn if the file actually exists. - let assert = test_env - .jj_cmd(&repo_path, &["log", "file1", "-T", "description"]) - .assert() - .success(); - insta::assert_snapshot!(get_stdout_string(&assert), @r###" + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["log", "file1", "-T", "description"]); + insta::assert_snapshot!(stdout, @r###" @ │ ~ "###); - insta::assert_snapshot!(get_stderr_string(&assert), @""); + insta::assert_snapshot!(stderr, @""); // Warn for `jj log .` specifically, for former Mercurial users. - let assert = test_env - .jj_cmd(&repo_path, &["log", ".", "-T", "description"]) - .assert() - .success(); - insta::assert_snapshot!(get_stdout_string(&assert), @r###" + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["log", ".", "-T", "description"]); + insta::assert_snapshot!(stdout, @r###" @ │ ~ "###); - insta::assert_snapshot!(get_stderr_string(&assert), @r###"warning: The argument "." is being interpreted as a path, but this is often not useful because all non-empty commits touch '.'. If you meant to show the working copy commit, pass -r '@' instead."###); + insta::assert_snapshot!(stderr, @r###" + warning: The argument "." is being interpreted as a path, but this is often not useful because all non-empty commits touch '.'. If you meant to show the working copy commit, pass -r '@' instead. + "###); // ...but checking `jj log .` makes sense in a subdirectory. let subdir = repo_path.join("dir"); std::fs::create_dir_all(&subdir).unwrap(); - let assert = test_env.jj_cmd(&subdir, &["log", "."]).assert().success(); - insta::assert_snapshot!(get_stdout_string(&assert), @""); - insta::assert_snapshot!(get_stderr_string(&assert), @""); + let (stdout, stderr) = test_env.jj_cmd_ok(&subdir, &["log", "."]); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @""); // Warn for `jj log @` instead of `jj log -r @`. - let assert = test_env - .jj_cmd(&repo_path, &["log", "@", "-T", "description"]) - .assert() - .success(); - insta::assert_snapshot!(get_stdout_string(&assert), @""); - insta::assert_snapshot!(get_stderr_string(&assert), @r###" + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["log", "@", "-T", "description"]); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r###" warning: The argument "@" is being interpreted as a path. To specify a revset, pass -r "@" instead. "###); // Warn when there's no path with the provided name. - let assert = test_env - .jj_cmd(&repo_path, &["log", "file2", "-T", "description"]) - .assert() - .success(); - insta::assert_snapshot!(get_stdout_string(&assert), @""); - insta::assert_snapshot!(get_stderr_string(&assert), @r###" + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["log", "file2", "-T", "description"]); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r###" warning: The argument "file2" is being interpreted as a path. To specify a revset, pass -r "file2" instead. "###); // If an explicit revision is provided, then suppress the warning. - let assert = test_env - .jj_cmd(&repo_path, &["log", "@", "-r", "@", "-T", "description"]) - .assert() - .success(); - insta::assert_snapshot!(get_stdout_string(&assert), @""); - insta::assert_snapshot!(get_stderr_string(&assert), @r###" + let (stdout, stderr) = + test_env.jj_cmd_ok(&repo_path, &["log", "@", "-r", "@", "-T", "description"]); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r###" "###); } diff --git a/tests/test_revset_output.rs b/tests/test_revset_output.rs index a3e9d792be..1f7eac1ae6 100644 --- a/tests/test_revset_output.rs +++ b/tests/test_revset_output.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use common::{get_stderr_string, get_stdout_string, TestEnvironment}; +use common::TestEnvironment; pub mod common; @@ -383,15 +383,12 @@ fn test_bad_alias_decl() { ); // Invalid declaration should be warned and ignored. - let assert = test_env - .jj_cmd(&repo_path, &["log", "-r", "my-root"]) - .assert() - .success(); - insta::assert_snapshot!(get_stdout_string(&assert), @r###" + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["log", "-r", "my-root"]); + insta::assert_snapshot!(stdout, @r###" ◉ zzzzzzzzzzzz 1970-01-01 00:00:00.000 +00:00 000000000000 (empty) (no description set) "###); - insta::assert_snapshot!(get_stderr_string(&assert), @r###" + insta::assert_snapshot!(stderr, @r###" Failed to load "revset-aliases."bad"": --> 1:1 | 1 | "bad" diff --git a/tests/test_split_command.rs b/tests/test_split_command.rs index c60a03f2d9..48d14fcf91 100644 --- a/tests/test_split_command.rs +++ b/tests/test_split_command.rs @@ -14,7 +14,7 @@ use std::path::Path; -use crate::common::{get_stderr_string, get_stdout_string, TestEnvironment}; +use crate::common::TestEnvironment; pub mod common; @@ -110,18 +110,15 @@ fn test_split_by_paths() { // Insert an empty commit before @- with "split nonexistent" test_env.set_up_fake_editor(); - let assert = test_env - .jj_cmd(&repo_path, &["split", "-r", "@-", "nonexistent"]) - .assert() - .success(); - insta::assert_snapshot!(get_stdout_string(&assert), @r###" + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["split", "-r", "@-", "nonexistent"]); + insta::assert_snapshot!(stdout, @r###" Rebased 1 descendant commits First part: 0647b2cbd0da (no description set) Second part: d5d77af65446 (no description set) Working copy now at: 86f228dc3a50 (no description set) Parent commit : d5d77af65446 (no description set) "###); - insta::assert_snapshot!(get_stderr_string(&assert), @r###" + insta::assert_snapshot!(stderr, @r###" The given paths do not match any file: nonexistent "###); diff --git a/tests/test_squash_command.rs b/tests/test_squash_command.rs index ea01b3fa67..4767bad150 100644 --- a/tests/test_squash_command.rs +++ b/tests/test_squash_command.rs @@ -14,7 +14,7 @@ use std::path::Path; -use crate::common::{get_stderr_string, get_stdout_string, TestEnvironment}; +use crate::common::TestEnvironment; pub mod common; @@ -249,14 +249,11 @@ fn test_squash_partial() { // We get a warning if we pass a positional argument that looks like a revset test_env.jj_cmd_success(&repo_path, &["undo"]); - let assert = test_env - .jj_cmd(&repo_path, &["squash", "b"]) - .assert() - .success(); - insta::assert_snapshot!(get_stderr_string(&assert), @r###" + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["squash", "b"]); + insta::assert_snapshot!(stderr, @r###" warning: The argument "b" is being interpreted as a path. To specify a revset, pass -r "b" instead. "###); - insta::assert_snapshot!(get_stdout_string(&assert), @r###" + insta::assert_snapshot!(stdout, @r###" Working copy now at: 1c4e5596a511 (no description set) Parent commit : 16cc94b4efe9 (no description set) "###); diff --git a/tests/test_templater.rs b/tests/test_templater.rs index 93e155d25f..25f04086da 100644 --- a/tests/test_templater.rs +++ b/tests/test_templater.rs @@ -14,7 +14,7 @@ use std::path::Path; -use crate::common::{get_stderr_string, get_stdout_string, TestEnvironment}; +use crate::common::TestEnvironment; pub mod common; @@ -853,12 +853,10 @@ fn test_templater_bad_alias_decl() { ); // Invalid declaration should be warned and ignored. - let assert = test_env - .jj_cmd(&repo_path, &["log", "--no-graph", "-r@-", "-Tmy_commit_id"]) - .assert() - .success(); - insta::assert_snapshot!(get_stdout_string(&assert), @"000000000000"); - insta::assert_snapshot!(get_stderr_string(&assert), @r###" + let (stdout, stderr) = + test_env.jj_cmd_ok(&repo_path, &["log", "--no-graph", "-r@-", "-Tmy_commit_id"]); + insta::assert_snapshot!(stdout, @"000000000000"); + insta::assert_snapshot!(stderr, @r###" Failed to load "template-aliases.badfn(a, a)": --> 1:7 | 1 | badfn(a, a)