ok/jj
1
0
Fork 0
forked from mirrors/jj

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
}
/// 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 {
&self.env_root
}

View file

@ -12,20 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use jujutsu::testutils::{get_stdout_string, TestEnvironment};
use jujutsu::testutils::TestEnvironment;
#[test]
fn smoke_test() {
let test_env = TestEnvironment::default();
test_env
.jj_cmd(test_env.env_root(), &["init", "repo", "--git"])
.assert()
.success();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
// Check the output of `jj status` right after initializing repo
let assert = test_env.jj_cmd(&repo_path, &["status"]).assert().success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
let stdout = test_env.jj_cmd_success(&repo_path, &["status"]);
insta::assert_snapshot!(stdout, @r###"
Parent commit: 000000000000
Working copy : 230dd059e1b0
The working copy is clean
@ -37,9 +34,8 @@ fn smoke_test() {
std::fs::write(repo_path.join("file3"), "file3").unwrap();
// The working copy's ID should have changed
let assert = test_env.jj_cmd(&repo_path, &["status"]).assert().success();
let stdout_string = get_stdout_string(&assert);
insta::assert_snapshot!(stdout_string, @r###"
let stdout = test_env.jj_cmd_success(&repo_path, &["status"]);
insta::assert_snapshot!(stdout, @r###"
Parent commit: 000000000000
Working copy : d38745675403
Working copy changes:
@ -49,22 +45,16 @@ fn smoke_test() {
"###);
// Running `jj status` again gives the same output
test_env
.jj_cmd(&repo_path, &["status"])
.assert()
.success()
.stdout(stdout_string);
let stdout_again = test_env.jj_cmd_success(&repo_path, &["status"]);
assert_eq!(stdout_again, stdout);
// Add a commit description
let assert = test_env
.jj_cmd(&repo_path, &["describe", "-m", "add some files"])
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @"Working copy now at: 701b3d5a2eb3 add some files
let stdout = test_env.jj_cmd_success(&repo_path, &["describe", "-m", "add some files"]);
insta::assert_snapshot!(stdout, @"Working copy now at: 701b3d5a2eb3 add some files
");
// Close the commit
let assert = test_env.jj_cmd(&repo_path, &["close"]).assert().success();
insta::assert_snapshot!(get_stdout_string(&assert), @"Working copy now at: a13f828fab1a
let stdout = test_env.jj_cmd_success(&repo_path, &["close"]);
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");
git2::Repository::init(&git_repo_path).unwrap();
test_env
.jj_cmd(
test_env.jj_cmd_success(
test_env.env_root(),
&["git", "clone", "git-repo", "jj-repo"],
)
.assert()
.success();
);
let workspace_root = test_env.env_root().join("jj-repo");
// No branches to push yet
let assert = test_env
.jj_cmd(&workspace_root, &["git", "push"])
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @"Nothing changed.
let stdout = test_env.jj_cmd_success(&workspace_root, &["git", "push"]);
insta::assert_snapshot!(stdout, @"Nothing changed.
");
// When pushing everything, won't push an open commit even if there's a branch
// on it
test_env
.jj_cmd(&workspace_root, &["branch", "my-branch"])
.assert()
.success();
let assert = test_env
.jj_cmd(&workspace_root, &["git", "push"])
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
test_env.jj_cmd_success(&workspace_root, &["branch", "my-branch"]);
let stdout = test_env.jj_cmd_success(&workspace_root, &["git", "push"]);
insta::assert_snapshot!(stdout, @r###"
Skipping branch 'my-branch' since it points to an open commit.
Nothing changed.
"###);
@ -62,28 +50,13 @@ fn test_git_push() {
// Try pushing a conflict
std::fs::write(workspace_root.join("file"), "first").unwrap();
test_env
.jj_cmd(&workspace_root, &["close", "-m", "first"])
.assert()
.success();
test_env.jj_cmd_success(&workspace_root, &["close", "-m", "first"]);
std::fs::write(workspace_root.join("file"), "second").unwrap();
test_env
.jj_cmd(&workspace_root, &["close", "-m", "second"])
.assert()
.success();
test_env.jj_cmd_success(&workspace_root, &["close", "-m", "second"]);
std::fs::write(workspace_root.join("file"), "third").unwrap();
test_env
.jj_cmd(&workspace_root, &["rebase", "-d", "@--"])
.assert()
.success();
test_env
.jj_cmd(&workspace_root, &["branch", "my-branch"])
.assert()
.success();
test_env
.jj_cmd(&workspace_root, &["close", "-m", "third"])
.assert()
.success();
test_env.jj_cmd_success(&workspace_root, &["rebase", "-d", "@--"]);
test_env.jj_cmd_success(&workspace_root, &["branch", "my-branch"]);
test_env.jj_cmd_success(&workspace_root, &["close", "-m", "third"]);
let assert = test_env
.jj_cmd(&workspace_root, &["git", "push"])
.assert()

View file

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

View file

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

View file

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

View file

@ -19,10 +19,7 @@ use jujutsu::testutils::{get_stdout_string, TestEnvironment};
#[test]
fn test_untrack() {
let test_env = TestEnvironment::default();
test_env
.jj_cmd(test_env.env_root(), &["init", "repo"])
.assert()
.success();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo"]);
let repo_path = test_env.env_root().join("repo");
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
// 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();
let files_before =
get_stdout_string(&test_env.jj_cmd(&repo_path, &["files"]).assert().success());
let files_before = test_env.jj_cmd_success(&repo_path, &["files"]);
// Errors out when a specified file is not ignored
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, \
then try again.\n",
);
let files_after =
get_stdout_string(&test_env.jj_cmd(&repo_path, &["files"]).assert().success());
let files_after = test_env.jj_cmd_success(&repo_path, &["files"]);
// There should be no changes to the state when there was an error
assert_eq!(files_after, files_before);
// Can untrack a single file
assert!(files_before.contains("file1.bak\n"));
test_env
.jj_cmd(&repo_path, &["untrack", "file1.bak"])
.assert()
.success()
.stdout("");
let files_after =
get_stdout_string(&test_env.jj_cmd(&repo_path, &["files"]).assert().success());
let stdout = test_env.jj_cmd_success(&repo_path, &["untrack", "file1.bak"]);
assert_eq!(stdout, "");
let files_after = test_env.jj_cmd_success(&repo_path, &["files"]);
// The file is no longer tracked
assert!(!files_after.contains("file1.bak"));
// Other files that match the ignore pattern are not untracked
@ -87,12 +78,8 @@ fn test_untrack() {
// Can untrack after adding to ignore patterns
std::fs::write(repo_path.join(".gitignore"), ".bak\ntarget/\n").unwrap();
test_env
.jj_cmd(&repo_path, &["untrack", "target"])
.assert()
.success()
.stdout("");
let files_after =
get_stdout_string(&test_env.jj_cmd(&repo_path, &["files"]).assert().success());
let stdout = test_env.jj_cmd_success(&repo_path, &["untrack", "target"]);
assert_eq!(stdout, "");
let files_after = test_env.jj_cmd_success(&repo_path, &["files"]);
assert!(!files_after.contains("target"));
}