tests: add a convenience function for running jj successfully

We very often expect success, and we sometimes want to get the stdout,
too. Let's add a convenience function for that. It saves a lot of
lines of code.
This commit is contained in:
Martin von Zweigbergk 2022-03-26 14:06:48 -07:00 committed by Martin von Zweigbergk
parent 03aad3ac2e
commit 23c7581ce1
7 changed files with 78 additions and 162 deletions

View file

@ -64,6 +64,12 @@ impl TestEnvironment {
cmd cmd
} }
/// 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 = self.jj_cmd(current_dir, args).assert().success();
get_stdout_string(&assert)
}
pub fn env_root(&self) -> &Path { pub fn env_root(&self) -> &Path {
&self.env_root &self.env_root
} }

View file

@ -12,20 +12,17 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use jujutsu::testutils::{get_stdout_string, TestEnvironment}; use jujutsu::testutils::TestEnvironment;
#[test] #[test]
fn smoke_test() { fn smoke_test() {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
test_env test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
.jj_cmd(test_env.env_root(), &["init", "repo", "--git"])
.assert()
.success();
let repo_path = test_env.env_root().join("repo"); let repo_path = test_env.env_root().join("repo");
// Check the output of `jj status` right after initializing repo // Check the output of `jj status` right after initializing repo
let assert = test_env.jj_cmd(&repo_path, &["status"]).assert().success(); let stdout = test_env.jj_cmd_success(&repo_path, &["status"]);
insta::assert_snapshot!(get_stdout_string(&assert), @r###" insta::assert_snapshot!(stdout, @r###"
Parent commit: 000000000000 Parent commit: 000000000000
Working copy : 230dd059e1b0 Working copy : 230dd059e1b0
The working copy is clean The working copy is clean
@ -37,9 +34,8 @@ fn smoke_test() {
std::fs::write(repo_path.join("file3"), "file3").unwrap(); std::fs::write(repo_path.join("file3"), "file3").unwrap();
// The working copy's ID should have changed // The working copy's ID should have changed
let assert = test_env.jj_cmd(&repo_path, &["status"]).assert().success(); let stdout = test_env.jj_cmd_success(&repo_path, &["status"]);
let stdout_string = get_stdout_string(&assert); insta::assert_snapshot!(stdout, @r###"
insta::assert_snapshot!(stdout_string, @r###"
Parent commit: 000000000000 Parent commit: 000000000000
Working copy : d38745675403 Working copy : d38745675403
Working copy changes: Working copy changes:
@ -49,22 +45,16 @@ fn smoke_test() {
"###); "###);
// Running `jj status` again gives the same output // Running `jj status` again gives the same output
test_env let stdout_again = test_env.jj_cmd_success(&repo_path, &["status"]);
.jj_cmd(&repo_path, &["status"]) assert_eq!(stdout_again, stdout);
.assert()
.success()
.stdout(stdout_string);
// Add a commit description // Add a commit description
let assert = test_env let stdout = test_env.jj_cmd_success(&repo_path, &["describe", "-m", "add some files"]);
.jj_cmd(&repo_path, &["describe", "-m", "add some files"]) insta::assert_snapshot!(stdout, @"Working copy now at: 701b3d5a2eb3 add some files
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @"Working copy now at: 701b3d5a2eb3 add some files
"); ");
// Close the commit // Close the commit
let assert = test_env.jj_cmd(&repo_path, &["close"]).assert().success(); let stdout = test_env.jj_cmd_success(&repo_path, &["close"]);
insta::assert_snapshot!(get_stdout_string(&assert), @"Working copy now at: a13f828fab1a insta::assert_snapshot!(stdout, @"Working copy now at: a13f828fab1a
"); ");
} }

View file

@ -20,34 +20,22 @@ fn test_git_push() {
let git_repo_path = test_env.env_root().join("git-repo"); let git_repo_path = test_env.env_root().join("git-repo");
git2::Repository::init(&git_repo_path).unwrap(); git2::Repository::init(&git_repo_path).unwrap();
test_env test_env.jj_cmd_success(
.jj_cmd( test_env.env_root(),
test_env.env_root(), &["git", "clone", "git-repo", "jj-repo"],
&["git", "clone", "git-repo", "jj-repo"], );
)
.assert()
.success();
let workspace_root = test_env.env_root().join("jj-repo"); let workspace_root = test_env.env_root().join("jj-repo");
// No branches to push yet // No branches to push yet
let assert = test_env let stdout = test_env.jj_cmd_success(&workspace_root, &["git", "push"]);
.jj_cmd(&workspace_root, &["git", "push"]) insta::assert_snapshot!(stdout, @"Nothing changed.
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @"Nothing changed.
"); ");
// When pushing everything, won't push an open commit even if there's a branch // When pushing everything, won't push an open commit even if there's a branch
// on it // on it
test_env test_env.jj_cmd_success(&workspace_root, &["branch", "my-branch"]);
.jj_cmd(&workspace_root, &["branch", "my-branch"]) let stdout = test_env.jj_cmd_success(&workspace_root, &["git", "push"]);
.assert() insta::assert_snapshot!(stdout, @r###"
.success();
let assert = test_env
.jj_cmd(&workspace_root, &["git", "push"])
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
Skipping branch 'my-branch' since it points to an open commit. Skipping branch 'my-branch' since it points to an open commit.
Nothing changed. Nothing changed.
"###); "###);
@ -62,28 +50,13 @@ fn test_git_push() {
// Try pushing a conflict // Try pushing a conflict
std::fs::write(workspace_root.join("file"), "first").unwrap(); std::fs::write(workspace_root.join("file"), "first").unwrap();
test_env test_env.jj_cmd_success(&workspace_root, &["close", "-m", "first"]);
.jj_cmd(&workspace_root, &["close", "-m", "first"])
.assert()
.success();
std::fs::write(workspace_root.join("file"), "second").unwrap(); std::fs::write(workspace_root.join("file"), "second").unwrap();
test_env test_env.jj_cmd_success(&workspace_root, &["close", "-m", "second"]);
.jj_cmd(&workspace_root, &["close", "-m", "second"])
.assert()
.success();
std::fs::write(workspace_root.join("file"), "third").unwrap(); std::fs::write(workspace_root.join("file"), "third").unwrap();
test_env test_env.jj_cmd_success(&workspace_root, &["rebase", "-d", "@--"]);
.jj_cmd(&workspace_root, &["rebase", "-d", "@--"]) test_env.jj_cmd_success(&workspace_root, &["branch", "my-branch"]);
.assert() test_env.jj_cmd_success(&workspace_root, &["close", "-m", "third"]);
.success();
test_env
.jj_cmd(&workspace_root, &["branch", "my-branch"])
.assert()
.success();
test_env
.jj_cmd(&workspace_root, &["close", "-m", "third"])
.assert()
.success();
let assert = test_env let assert = test_env
.jj_cmd(&workspace_root, &["git", "push"]) .jj_cmd(&workspace_root, &["git", "push"])
.assert() .assert()

View file

@ -14,17 +14,14 @@
use std::io::Write; use std::io::Write;
use jujutsu::testutils::{get_stdout_string, TestEnvironment}; use jujutsu::testutils::TestEnvironment;
#[test] #[test]
fn test_gitignores() { fn test_gitignores() {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
let workspace_root = test_env.env_root().join("repo"); let workspace_root = test_env.env_root().join("repo");
git2::Repository::init(&workspace_root).unwrap(); git2::Repository::init(&workspace_root).unwrap();
test_env test_env.jj_cmd_success(&workspace_root, &["init", "--git-repo", "."]);
.jj_cmd(&workspace_root, &["init", "--git-repo", "."])
.assert()
.success();
// Say in core.excludesFiles that we don't want file1, file2, or file3 // Say in core.excludesFiles that we don't want file1, file2, or file3
let mut file = std::fs::OpenOptions::new() let mut file = std::fs::OpenOptions::new()
@ -59,11 +56,8 @@ fn test_gitignores() {
std::fs::write(workspace_root.join("file2"), "contents").unwrap(); std::fs::write(workspace_root.join("file2"), "contents").unwrap();
std::fs::write(workspace_root.join("file3"), "contents").unwrap(); std::fs::write(workspace_root.join("file3"), "contents").unwrap();
let assert = test_env let stdout = test_env.jj_cmd_success(&workspace_root, &["diff", "-s"]);
.jj_cmd(&workspace_root, &["diff", "-s"]) insta::assert_snapshot!(stdout, @r###"
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
A .gitignore A .gitignore
A file0 A file0
A file3 A file3

View file

@ -19,20 +19,13 @@ use jujutsu::testutils::{get_stdout_string, TestEnvironment};
#[test] #[test]
fn test_no_commit_working_copy() { fn test_no_commit_working_copy() {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
test_env test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
.jj_cmd(test_env.env_root(), &["init", "repo", "--git"])
.assert()
.success();
let repo_path = test_env.env_root().join("repo"); let repo_path = test_env.env_root().join("repo");
std::fs::write(repo_path.join("file"), "initial").unwrap(); std::fs::write(repo_path.join("file"), "initial").unwrap();
let assert = test_env let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
.jj_cmd(&repo_path, &["log", "-T", "commit_id"]) insta::assert_snapshot!(stdout, @r###"
.assert()
.success();
let stdout_string = get_stdout_string(&assert);
insta::assert_snapshot!(stdout_string, @r###"
@ 438471f3fbf1004298d8fb01eeb13663a051a643 @ 438471f3fbf1004298d8fb01eeb13663a051a643
o 0000000000000000000000000000000000000000 o 0000000000000000000000000000000000000000
"###); "###);
@ -40,21 +33,15 @@ fn test_no_commit_working_copy() {
// Modify the file. With --no-commit-working-copy, we still get the same commit // Modify the file. With --no-commit-working-copy, we still get the same commit
// ID. // ID.
std::fs::write(repo_path.join("file"), "modified").unwrap(); std::fs::write(repo_path.join("file"), "modified").unwrap();
test_env let stdout_again = test_env.jj_cmd_success(
.jj_cmd( &repo_path,
&repo_path, &["log", "-T", "commit_id", "--no-commit-working-copy"],
&["log", "-T", "commit_id", "--no-commit-working-copy"], );
) assert_eq!(stdout_again, stdout);
.assert()
.success()
.stdout(stdout_string);
// But without --no-commit-working-copy, we get a new commit ID. // But without --no-commit-working-copy, we get a new commit ID.
let assert = test_env let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
.jj_cmd(&repo_path, &["log", "-T", "commit_id"]) insta::assert_snapshot!(stdout, @r###"
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
@ fab22d1acf5bb9c5aa48cb2c3dd2132072a359ca @ fab22d1acf5bb9c5aa48cb2c3dd2132072a359ca
o 0000000000000000000000000000000000000000 o 0000000000000000000000000000000000000000
"###); "###);
@ -98,17 +85,11 @@ color="always""#,
) )
.unwrap(); .unwrap();
config_file.flush().unwrap(); config_file.flush().unwrap();
test_env test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
.jj_cmd(test_env.env_root(), &["init", "repo", "--git"])
.assert()
.success();
let repo_path = test_env.env_root().join("repo"); let repo_path = test_env.env_root().join("repo");
let assert = test_env let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
.jj_cmd(&repo_path, &["log", "-T", "commit_id"]) insta::assert_snapshot!(stdout, @r###"
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22 @ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
o 0000000000000000000000000000000000000000 o 0000000000000000000000000000000000000000
"###); "###);

View file

@ -12,16 +12,13 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use jujutsu::testutils::{get_stdout_string, TestEnvironment}; use jujutsu::testutils::TestEnvironment;
#[test] #[test]
fn test_init_git_internal() { fn test_init_git_internal() {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
let assert = test_env let stdout = test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
.jj_cmd(test_env.env_root(), &["init", "repo", "--git"]) insta::assert_snapshot!(stdout, @r###"Initialized repo in "repo"
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"Initialized repo in "repo"
"###); "###);
let workspace_root = test_env.env_root().join("repo"); let workspace_root = test_env.env_root().join("repo");
@ -69,19 +66,16 @@ fn test_init_git_external() {
.unwrap(); .unwrap();
git_repo.set_head("refs/heads/my-branch").unwrap(); git_repo.set_head("refs/heads/my-branch").unwrap();
let assert = test_env let stdout = test_env.jj_cmd_success(
.jj_cmd( test_env.env_root(),
test_env.env_root(), &[
&[ "init",
"init", "repo",
"repo", "--git-repo",
"--git-repo", git_repo_path.to_str().unwrap(),
git_repo_path.to_str().unwrap(), ],
], );
) insta::assert_snapshot!(stdout, @r###"
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
Working copy now at: f6950fc115ae Working copy now at: f6950fc115ae
Added 1 files, modified 0 files, removed 0 files Added 1 files, modified 0 files, removed 0 files
Initialized repo in "repo" Initialized repo in "repo"
@ -102,11 +96,8 @@ fn test_init_git_external() {
.ends_with("/git-repo/.git")); .ends_with("/git-repo/.git"));
// Check that the Git repo's HEAD got checked out // Check that the Git repo's HEAD got checked out
let assert = test_env let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-r", "@-"]);
.jj_cmd(&repo_path, &["log", "-r", "@-"]) insta::assert_snapshot!(stdout, @r###"
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
o 8d698d4a8ee1 d3866db7e30a git.user@example.com 1970-01-01 01:02:03.000 +01:00 my-branch HEAD@git o 8d698d4a8ee1 d3866db7e30a git.user@example.com 1970-01-01 01:02:03.000 +01:00 my-branch HEAD@git
~ My commit message ~ My commit message
"###); "###);
@ -117,12 +108,9 @@ fn test_init_git_colocated() {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
let workspace_root = test_env.env_root().join("repo"); let workspace_root = test_env.env_root().join("repo");
git2::Repository::init(&workspace_root).unwrap(); git2::Repository::init(&workspace_root).unwrap();
let assert = test_env let stdout = test_env.jj_cmd_success(&workspace_root, &["init", "--git-repo", "."]);
.jj_cmd(&workspace_root, &["init", "--git-repo", "."])
.assert()
.success();
// TODO: We should say "." instead of "" here // TODO: We should say "." instead of "" here
insta::assert_snapshot!(get_stdout_string(&assert), @r###"Initialized repo in "" insta::assert_snapshot!(stdout, @r###"Initialized repo in ""
"###); "###);
let jj_path = workspace_root.join(".jj"); let jj_path = workspace_root.join(".jj");
@ -142,11 +130,8 @@ fn test_init_git_colocated() {
#[test] #[test]
fn test_init_local() { fn test_init_local() {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
let assert = test_env let stdout = test_env.jj_cmd_success(test_env.env_root(), &["init", "repo"]);
.jj_cmd(test_env.env_root(), &["init", "repo"]) insta::assert_snapshot!(stdout, @r###"Initialized repo in "repo"
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"Initialized repo in "repo"
"###); "###);
let workspace_root = test_env.env_root().join("repo"); let workspace_root = test_env.env_root().join("repo");

View file

@ -19,10 +19,7 @@ use jujutsu::testutils::{get_stdout_string, TestEnvironment};
#[test] #[test]
fn test_untrack() { fn test_untrack() {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
test_env test_env.jj_cmd_success(test_env.env_root(), &["init", "repo"]);
.jj_cmd(test_env.env_root(), &["init", "repo"])
.assert()
.success();
let repo_path = test_env.env_root().join("repo"); let repo_path = test_env.env_root().join("repo");
std::fs::write(repo_path.join("file1"), "initial").unwrap(); std::fs::write(repo_path.join("file1"), "initial").unwrap();
@ -35,10 +32,9 @@ fn test_untrack() {
// Run a command so all the files get tracked, then add "*.bak" to the ignore // Run a command so all the files get tracked, then add "*.bak" to the ignore
// patterns // patterns
test_env.jj_cmd(&repo_path, &["st"]).assert().success(); test_env.jj_cmd_success(&repo_path, &["st"]);
std::fs::write(repo_path.join(".gitignore"), "*.bak\n").unwrap(); std::fs::write(repo_path.join(".gitignore"), "*.bak\n").unwrap();
let files_before = let files_before = test_env.jj_cmd_success(&repo_path, &["files"]);
get_stdout_string(&test_env.jj_cmd(&repo_path, &["files"]).assert().success());
// Errors out when a specified file is not ignored // Errors out when a specified file is not ignored
let assert = test_env let assert = test_env
@ -49,20 +45,15 @@ fn test_untrack() {
"Error: 'file1' would be added back because it's not ignored. Make sure it's ignored, \ "Error: 'file1' would be added back because it's not ignored. Make sure it's ignored, \
then try again.\n", then try again.\n",
); );
let files_after = let files_after = test_env.jj_cmd_success(&repo_path, &["files"]);
get_stdout_string(&test_env.jj_cmd(&repo_path, &["files"]).assert().success());
// There should be no changes to the state when there was an error // There should be no changes to the state when there was an error
assert_eq!(files_after, files_before); assert_eq!(files_after, files_before);
// Can untrack a single file // Can untrack a single file
assert!(files_before.contains("file1.bak\n")); assert!(files_before.contains("file1.bak\n"));
test_env let stdout = test_env.jj_cmd_success(&repo_path, &["untrack", "file1.bak"]);
.jj_cmd(&repo_path, &["untrack", "file1.bak"]) assert_eq!(stdout, "");
.assert() let files_after = test_env.jj_cmd_success(&repo_path, &["files"]);
.success()
.stdout("");
let files_after =
get_stdout_string(&test_env.jj_cmd(&repo_path, &["files"]).assert().success());
// The file is no longer tracked // The file is no longer tracked
assert!(!files_after.contains("file1.bak")); assert!(!files_after.contains("file1.bak"));
// Other files that match the ignore pattern are not untracked // Other files that match the ignore pattern are not untracked
@ -87,12 +78,8 @@ fn test_untrack() {
// Can untrack after adding to ignore patterns // Can untrack after adding to ignore patterns
std::fs::write(repo_path.join(".gitignore"), ".bak\ntarget/\n").unwrap(); std::fs::write(repo_path.join(".gitignore"), ".bak\ntarget/\n").unwrap();
test_env let stdout = test_env.jj_cmd_success(&repo_path, &["untrack", "target"]);
.jj_cmd(&repo_path, &["untrack", "target"]) assert_eq!(stdout, "");
.assert() let files_after = test_env.jj_cmd_success(&repo_path, &["files"]);
.success()
.stdout("");
let files_after =
get_stdout_string(&test_env.jj_cmd(&repo_path, &["files"]).assert().success());
assert!(!files_after.contains("target")); assert!(!files_after.contains("target"));
} }