tests: use insta crate where appropriate

Thanks to @arxanas for the suggestion.
This commit is contained in:
Martin von Zweigbergk 2022-03-05 19:32:20 -08:00 committed by Martin von Zweigbergk
parent 0c1734a19d
commit 5721436558
4 changed files with 124 additions and 49 deletions

71
Cargo.lock generated
View file

@ -245,6 +245,19 @@ dependencies = [
"yaml-rust", "yaml-rust",
] ]
[[package]]
name = "console"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28b32d32ca44b70c3e4acd7db1babf555fa026e385fb95f18028f88848b3c31"
dependencies = [
"encode_unicode",
"libc",
"once_cell",
"terminal_size",
"winapi",
]
[[package]] [[package]]
name = "criterion" name = "criterion"
version = "0.3.5" version = "0.3.5"
@ -424,6 +437,12 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]] [[package]]
name = "fake-simd" name = "fake-simd"
version = "0.1.2" version = "0.1.2"
@ -560,6 +579,20 @@ dependencies = [
"unindent", "unindent",
] ]
[[package]]
name = "insta"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30a7e1911532a662f6b08b68f884080850f2fd9544963c3ab23a5af42bda1eac"
dependencies = [
"console",
"once_cell",
"serde 1.0.126",
"serde_json",
"serde_yaml",
"similar",
]
[[package]] [[package]]
name = "instant" name = "instant"
version = "0.1.9" version = "0.1.9"
@ -624,6 +657,7 @@ dependencies = [
"git2", "git2",
"hex", "hex",
"indoc", "indoc",
"insta",
"itertools", "itertools",
"jujutsu-lib", "jujutsu-lib",
"maplit", "maplit",
@ -833,6 +867,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "once_cell"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]] [[package]]
name = "oorandom" name = "oorandom"
version = "11.1.3" version = "11.1.3"
@ -1225,6 +1265,9 @@ name = "serde"
version = "1.0.126" version = "1.0.126"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03" checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03"
dependencies = [
"serde_derive",
]
[[package]] [[package]]
name = "serde-hjson" name = "serde-hjson"
@ -1270,6 +1313,18 @@ dependencies = [
"serde 1.0.126", "serde 1.0.126",
] ]
[[package]]
name = "serde_yaml"
version = "0.8.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a521f2940385c165a24ee286aa8599633d162077a54bdcae2a6fd5a7bfa7a0"
dependencies = [
"indexmap",
"ryu",
"serde 1.0.126",
"yaml-rust",
]
[[package]] [[package]]
name = "sha-1" name = "sha-1"
version = "0.8.2" version = "0.8.2"
@ -1282,6 +1337,12 @@ dependencies = [
"opaque-debug", "opaque-debug",
] ]
[[package]]
name = "similar"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e24979f63a11545f5f2c60141afe249d4f19f84581ea2138065e400941d83d3"
[[package]] [[package]]
name = "smawk" name = "smawk"
version = "0.3.1" version = "0.3.1"
@ -1340,6 +1401,16 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "terminal_size"
version = "0.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df"
dependencies = [
"libc",
"winapi",
]
[[package]] [[package]]
name = "termtree" name = "termtree"
version = "0.2.4" version = "0.2.4"

View file

@ -35,6 +35,7 @@ criterion = "0.3.5"
git2 = "0.13.25" git2 = "0.13.25"
hex = "0.4.3" hex = "0.4.3"
indoc = "1.0.3" indoc = "1.0.3"
insta = "1.13.0"
itertools = "0.10.3" itertools = "0.10.3"
jujutsu-lib = { version = "=0.2.0", path = "lib"} jujutsu-lib = { version = "=0.2.0", path = "lib"}
maplit = "1.0.2" maplit = "1.0.2"

View file

@ -12,7 +12,7 @@
// 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::TestEnvironment; use jujutsu::testutils::{get_stdout_string, TestEnvironment};
#[test] #[test]
fn smoke_test() { fn smoke_test() {
@ -24,15 +24,15 @@ fn smoke_test() {
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 expected_output = "Parent commit: 000000000000 let assert = test_env
Working copy : 1d1984a23811
The working copy is clean
";
test_env
.jj_cmd(&repo_path, &["status"]) .jj_cmd(&repo_path, &["status"])
.assert() .assert()
.success() .success();
.stdout(expected_output); insta::assert_snapshot!(get_stdout_string(&assert), @r###"
Parent commit: 000000000000
Working copy : 1d1984a23811
The working copy is clean
"###);
// Write some files and check the output of `jj status` // Write some files and check the output of `jj status`
std::fs::write(repo_path.join("file1"), "file1").unwrap(); std::fs::write(repo_path.join("file1"), "file1").unwrap();
@ -40,41 +40,40 @@ The working copy is clean
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 expected_output = "Parent commit: 000000000000 let assert = test_env
Working copy : 5e60c5091e43
Working copy changes:
A file1
A file2
A file3
";
test_env
.jj_cmd(&repo_path, &["status"]) .jj_cmd(&repo_path, &["status"])
.assert() .assert()
.success() .success();
.stdout(expected_output); let stdout_string = get_stdout_string(&assert);
insta::assert_snapshot!(stdout_string, @r###"
Parent commit: 000000000000
Working copy : 5e60c5091e43
Working copy changes:
A file1
A file2
A file3
"###);
// Running `jj status` again gives the same output // Running `jj status` again gives the same output
test_env test_env
.jj_cmd(&repo_path, &["status"]) .jj_cmd(&repo_path, &["status"])
.assert() .assert()
.success() .success()
.stdout(expected_output); .stdout(stdout_string);
// Add a commit description // Add a commit description
let expected_output = "Working copy now at: 6f13b3e41065 add some files let assert = test_env
";
test_env
.jj_cmd(&repo_path, &["describe", "-m", "add some files"]) .jj_cmd(&repo_path, &["describe", "-m", "add some files"])
.assert() .assert()
.success() .success();
.stdout(expected_output); insta::assert_snapshot!(get_stdout_string(&assert), @"Working copy now at: 6f13b3e41065 add some files
");
// Close the commit // Close the commit
let expected_output = "Working copy now at: 6ff8a22d8ce1 let assert = test_env
";
test_env
.jj_cmd(&repo_path, &["close"]) .jj_cmd(&repo_path, &["close"])
.assert() .assert()
.success() .success();
.stdout(expected_output); insta::assert_snapshot!(get_stdout_string(&assert), @"Working copy now at: 6ff8a22d8ce1
");
} }

View file

@ -12,7 +12,7 @@
// 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::TestEnvironment; use jujutsu::testutils::{get_stdout_string, TestEnvironment};
#[test] #[test]
fn test_no_commit_working_copy() { fn test_no_commit_working_copy() {
@ -25,14 +25,16 @@ fn test_no_commit_working_copy() {
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 expected_output = "@ 1e9ff0ea7220c37a1d2c4aab153e238c12ff3cd0 let assert = test_env
o 0000000000000000000000000000000000000000
";
test_env
.jj_cmd(&repo_path, &["log", "-T", "commit_id"]) .jj_cmd(&repo_path, &["log", "-T", "commit_id"])
.assert() .assert()
.success() .success();
.stdout(expected_output); let stdout_string = get_stdout_string(&assert);
insta::assert_snapshot!(stdout_string, @r###"
@ 1e9ff0ea7220c37a1d2c4aab153e238c12ff3cd0
o 0000000000000000000000000000000000000000
"###);
// 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.
@ -44,35 +46,37 @@ o 0000000000000000000000000000000000000000
) )
.assert() .assert()
.success() .success()
.stdout(expected_output); .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 expected_output = "@ cc12440b719c67fcd8c55848eb345f67b6e2d9f1 let assert = test_env
o 0000000000000000000000000000000000000000
";
test_env
.jj_cmd(&repo_path, &["log", "-T", "commit_id"]) .jj_cmd(&repo_path, &["log", "-T", "commit_id"])
.assert() .assert()
.success() .success();
.stdout(expected_output); insta::assert_snapshot!(get_stdout_string(&assert), @r###"
@ cc12440b719c67fcd8c55848eb345f67b6e2d9f1
o 0000000000000000000000000000000000000000
"###);
} }
#[test] #[test]
fn test_repo_arg_with_init() { fn test_repo_arg_with_init() {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
test_env let assert = test_env
.jj_cmd(test_env.env_root(), &["init", "-R=.", "repo"]) .jj_cmd(test_env.env_root(), &["init", "-R=.", "repo"])
.assert() .assert()
.failure() .failure();
.stdout("Error: '--repository' cannot be used with 'init'\n"); insta::assert_snapshot!(get_stdout_string(&assert), @"Error: '--repository' cannot be used with 'init'
");
} }
#[test] #[test]
fn test_repo_arg_with_git_clone() { fn test_repo_arg_with_git_clone() {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
test_env let assert = test_env
.jj_cmd(test_env.env_root(), &["git", "clone", "-R=.", "remote"]) .jj_cmd(test_env.env_root(), &["git", "clone", "-R=.", "remote"])
.assert() .assert()
.failure() .failure();
.stdout("Error: '--repository' cannot be used with 'git clone'\n"); insta::assert_snapshot!(get_stdout_string(&assert), @"Error: '--repository' cannot be used with 'git clone'
");
} }