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",
]
[[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]]
name = "criterion"
version = "0.3.5"
@ -424,6 +437,12 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]]
name = "fake-simd"
version = "0.1.2"
@ -560,6 +579,20 @@ dependencies = [
"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]]
name = "instant"
version = "0.1.9"
@ -624,6 +657,7 @@ dependencies = [
"git2",
"hex",
"indoc",
"insta",
"itertools",
"jujutsu-lib",
"maplit",
@ -833,6 +867,12 @@ dependencies = [
"libc",
]
[[package]]
name = "once_cell"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]]
name = "oorandom"
version = "11.1.3"
@ -1225,6 +1265,9 @@ name = "serde"
version = "1.0.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde-hjson"
@ -1270,6 +1313,18 @@ dependencies = [
"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]]
name = "sha-1"
version = "0.8.2"
@ -1282,6 +1337,12 @@ dependencies = [
"opaque-debug",
]
[[package]]
name = "similar"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e24979f63a11545f5f2c60141afe249d4f19f84581ea2138065e400941d83d3"
[[package]]
name = "smawk"
version = "0.3.1"
@ -1340,6 +1401,16 @@ dependencies = [
"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]]
name = "termtree"
version = "0.2.4"

View file

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

View file

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use jujutsu::testutils::TestEnvironment;
use jujutsu::testutils::{get_stdout_string, TestEnvironment};
#[test]
fn smoke_test() {
@ -24,15 +24,15 @@ fn smoke_test() {
let repo_path = test_env.env_root().join("repo");
// Check the output of `jj status` right after initializing repo
let expected_output = "Parent commit: 000000000000
Working copy : 1d1984a23811
The working copy is clean
";
test_env
let assert = test_env
.jj_cmd(&repo_path, &["status"])
.assert()
.success()
.stdout(expected_output);
.success();
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`
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();
// The working copy's ID should have changed
let expected_output = "Parent commit: 000000000000
let assert = test_env
.jj_cmd(&repo_path, &["status"])
.assert()
.success();
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
";
test_env
.jj_cmd(&repo_path, &["status"])
.assert()
.success()
.stdout(expected_output);
"###);
// Running `jj status` again gives the same output
test_env
.jj_cmd(&repo_path, &["status"])
.assert()
.success()
.stdout(expected_output);
.stdout(stdout_string);
// Add a commit description
let expected_output = "Working copy now at: 6f13b3e41065 add some files
";
test_env
let assert = test_env
.jj_cmd(&repo_path, &["describe", "-m", "add some files"])
.assert()
.success()
.stdout(expected_output);
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @"Working copy now at: 6f13b3e41065 add some files
");
// Close the commit
let expected_output = "Working copy now at: 6ff8a22d8ce1
";
test_env
let assert = test_env
.jj_cmd(&repo_path, &["close"])
.assert()
.success()
.stdout(expected_output);
.success();
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
// limitations under the License.
use jujutsu::testutils::TestEnvironment;
use jujutsu::testutils::{get_stdout_string, TestEnvironment};
#[test]
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");
std::fs::write(repo_path.join("file"), "initial").unwrap();
let expected_output = "@ 1e9ff0ea7220c37a1d2c4aab153e238c12ff3cd0
o 0000000000000000000000000000000000000000
";
test_env
let assert = test_env
.jj_cmd(&repo_path, &["log", "-T", "commit_id"])
.assert()
.success()
.stdout(expected_output);
.success();
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
// ID.
@ -44,35 +46,37 @@ o 0000000000000000000000000000000000000000
)
.assert()
.success()
.stdout(expected_output);
.stdout(stdout_string);
// But without --no-commit-working-copy, we get a new commit ID.
let expected_output = "@ cc12440b719c67fcd8c55848eb345f67b6e2d9f1
o 0000000000000000000000000000000000000000
";
test_env
let assert = test_env
.jj_cmd(&repo_path, &["log", "-T", "commit_id"])
.assert()
.success()
.stdout(expected_output);
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
@ cc12440b719c67fcd8c55848eb345f67b6e2d9f1
o 0000000000000000000000000000000000000000
"###);
}
#[test]
fn test_repo_arg_with_init() {
let test_env = TestEnvironment::default();
test_env
let assert = test_env
.jj_cmd(test_env.env_root(), &["init", "-R=.", "repo"])
.assert()
.failure()
.stdout("Error: '--repository' cannot be used with 'init'\n");
.failure();
insta::assert_snapshot!(get_stdout_string(&assert), @"Error: '--repository' cannot be used with 'init'
");
}
#[test]
fn test_repo_arg_with_git_clone() {
let test_env = TestEnvironment::default();
test_env
let assert = test_env
.jj_cmd(test_env.env_root(), &["git", "clone", "-R=.", "remote"])
.assert()
.failure()
.stdout("Error: '--repository' cannot be used with 'git clone'\n");
.failure();
insta::assert_snapshot!(get_stdout_string(&assert), @"Error: '--repository' cannot be used with 'git clone'
");
}