From 2b8fce7be80305d5a9441efb5211a8c3041e5ef6 Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Sat, 12 Oct 2024 09:37:20 -0700 Subject: [PATCH] Consolidate `Cargo.toml` creation in functional tests (#662) --- cargo-insta/tests/functional/binary.rs | 139 ++++---------------- cargo-insta/tests/functional/inline.rs | 60 +++------ cargo-insta/tests/functional/main.rs | 146 ++++++---------------- cargo-insta/tests/functional/workspace.rs | 14 +-- 4 files changed, 83 insertions(+), 276 deletions(-) diff --git a/cargo-insta/tests/functional/binary.rs b/cargo-insta/tests/functional/binary.rs index 1f17ad33..5440c79b 100644 --- a/cargo-insta/tests/functional/binary.rs +++ b/cargo-insta/tests/functional/binary.rs @@ -6,21 +6,9 @@ use crate::TestFiles; #[test] fn test_binary_pending() { let test_project = TestFiles::new() + .add_cargo_toml("test_binary_pending") .add_file( - "Cargo.toml", - r#" -[package] -name = "test_binary_pending" -version = "0.1.0" -edition = "2021" - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) - .add_file( - "src/main.rs", + "src/lib.rs", r#" #[test] fn test_binary_snapshot() { @@ -43,7 +31,7 @@ fn test_binary_snapshot() { + Cargo.lock Cargo.toml src - src/main.rs + src/lib.rs + src/snapshots + src/snapshots/test_binary_pending__binary_snapshot.snap.new + src/snapshots/test_binary_pending__binary_snapshot.snap.new.txt @@ -54,21 +42,9 @@ fn test_binary_snapshot() { #[test] fn test_binary_accept() { let test_project = TestFiles::new() + .add_cargo_toml("test_binary_accept") .add_file( - "Cargo.toml", - r#" -[package] -name = "test_binary_accept" -version = "0.1.0" -edition = "2021" - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) - .add_file( - "src/main.rs", + "src/lib.rs", r#" #[test] fn test_binary_snapshot() { @@ -95,7 +71,7 @@ fn test_binary_snapshot() { + Cargo.lock Cargo.toml src - src/main.rs + src/lib.rs + src/snapshots + src/snapshots/test_binary_accept__binary_snapshot.snap + src/snapshots/test_binary_accept__binary_snapshot.snap.txt @@ -108,21 +84,9 @@ fn test_binary_snapshot() { #[test] fn test_binary_change_extension() { let test_project = TestFiles::new() + .add_cargo_toml("test_binary_change_extension") .add_file( - "Cargo.toml", - r#" -[package] -name = "test_binary_change_extension" -version = "0.1.0" -edition = "2021" - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) - .add_file( - "src/main.rs", + "src/lib.rs", r#" #[test] fn test_binary_snapshot() { @@ -142,7 +106,7 @@ fn test_binary_snapshot() { assert!(&output.status.success()); test_project.update_file( - "src/main.rs", + "src/lib.rs", r#" #[test] fn test_binary_snapshot() { @@ -164,7 +128,7 @@ fn test_binary_snapshot() { + Cargo.lock Cargo.toml src - src/main.rs + src/lib.rs + src/snapshots + src/snapshots/test_binary_change_extension__binary_snapshot.snap + src/snapshots/test_binary_change_extension__binary_snapshot.snap.new @@ -188,7 +152,7 @@ fn test_binary_snapshot() { + Cargo.lock Cargo.toml src - src/main.rs + src/lib.rs + src/snapshots + src/snapshots/test_binary_change_extension__binary_snapshot.snap + src/snapshots/test_binary_change_extension__binary_snapshot.snap.json @@ -200,21 +164,9 @@ fn test_binary_snapshot() { #[test] fn test_binary_pending_snapshot_removal() { let test_project = TestFiles::new() + .add_cargo_toml("test_binary_pending_snapshot_removal") .add_file( - "Cargo.toml", - r#" -[package] -name = "test_binary_pending_snapshot_removal" -version = "0.1.0" -edition = "2021" - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) - .add_file( - "src/main.rs", + "src/lib.rs", r#" #[test] fn test_binary_snapshot() { @@ -229,7 +181,7 @@ fn test_binary_snapshot() { assert!(!&output.status.success()); - test_project.update_file("src/main.rs", "".to_string()); + test_project.update_file("src/lib.rs", "".to_string()); let output = test_project.insta_cmd().args(["test"]).output().unwrap(); @@ -243,7 +195,7 @@ fn test_binary_snapshot() { + Cargo.lock Cargo.toml src - src/main.rs + src/lib.rs + src/snapshots "); } @@ -253,21 +205,9 @@ fn test_binary_snapshot() { #[test] fn test_change_text_to_binary() { let test_project = TestFiles::new() + .add_cargo_toml("test_change_text_to_binary") .add_file( - "Cargo.toml", - r#" -[package] -name = "test_change_text_to_binary" -version = "0.1.0" -edition = "2021" - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) - .add_file( - "src/main.rs", + "src/lib.rs", r#" #[test] fn test() { @@ -293,13 +233,13 @@ fn test() { + Cargo.lock Cargo.toml src - src/main.rs + src/lib.rs + src/snapshots + src/snapshots/test_change_text_to_binary__test.snap "); test_project.update_file( - "src/main.rs", + "src/lib.rs", r#" #[test] fn test() { @@ -324,7 +264,7 @@ fn test() { + Cargo.lock Cargo.toml src - src/main.rs + src/lib.rs + src/snapshots + src/snapshots/test_change_text_to_binary__test.snap + src/snapshots/test_change_text_to_binary__test.snap.txt @@ -336,21 +276,9 @@ fn test() { #[test] fn test_change_binary_to_text() { let test_project = TestFiles::new() + .add_cargo_toml("test_change_binary_to_text") .add_file( - "Cargo.toml", - r#" -[package] -name = "test_change_binary_to_text" -version = "0.1.0" -edition = "2021" - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) - .add_file( - "src/main.rs", + "src/lib.rs", r#" #[test] fn test() { @@ -376,14 +304,14 @@ fn test() { + Cargo.lock Cargo.toml src - src/main.rs + src/lib.rs + src/snapshots + src/snapshots/test_change_binary_to_text__some_name.snap + src/snapshots/test_change_binary_to_text__some_name.snap.json "); test_project.update_file( - "src/main.rs", + "src/lib.rs", r#" #[test] fn test() { @@ -408,7 +336,7 @@ fn test() { + Cargo.lock Cargo.toml src - src/main.rs + src/lib.rs + src/snapshots + src/snapshots/test_change_binary_to_text__some_name.snap "); @@ -417,22 +345,7 @@ fn test() { #[test] fn test_binary_unreferenced_delete() { let test_project = TestFiles::new() - .add_file( - "Cargo.toml", - r#" -[package] -name = "test_binary_unreferenced_delete" -version = "0.1.0" -edition = "2021" - -[lib] -doctest = false - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) + .add_cargo_toml("test_binary_unreferenced_delete") .add_file( "src/lib.rs", r#" diff --git a/cargo-insta/tests/functional/inline.rs b/cargo-insta/tests/functional/inline.rs index 4d5aaaad..26579121 100644 --- a/cargo-insta/tests/functional/inline.rs +++ b/cargo-insta/tests/functional/inline.rs @@ -20,7 +20,7 @@ serde = { version = "1.0", features = ["derive"] } .to_string(), ) .add_file( - "src/main.rs", + "src/lib.rs", r#" use serde::Serialize; @@ -53,9 +53,9 @@ fn test_json_snapshot() { assert!(&output.status.success()); - assert_snapshot!(test_project.diff("src/main.rs"), @r##" - --- Original: src/main.rs - +++ Updated: src/main.rs + assert_snapshot!(test_project.diff("src/lib.rs"), @r##" + --- Original: src/lib.rs + +++ Updated: src/lib.rs @@ -15,5 +15,10 @@ }; insta::assert_json_snapshot!(&user, { @@ -89,7 +89,7 @@ serde = { version = "1.0", features = ["derive"] } .to_string(), ) .add_file( - "src/main.rs", + "src/lib.rs", r#" use serde::Serialize; @@ -122,9 +122,9 @@ fn test_yaml_snapshot() { assert!(&output.status.success()); - assert_snapshot!(test_project.diff("src/main.rs"), @r###" - --- Original: src/main.rs - +++ Updated: src/main.rs + assert_snapshot!(test_project.diff("src/lib.rs"), @r###" + --- Original: src/lib.rs + +++ Updated: src/lib.rs @@ -15,5 +15,8 @@ }; insta::assert_yaml_snapshot!(&user, { @@ -141,21 +141,9 @@ fn test_yaml_snapshot() { #[test] fn test_utf8_inline() { let test_project = TestFiles::new() + .add_cargo_toml("test_utf8_inline") .add_file( - "Cargo.toml", - r#" -[package] -name = "test_utf8_inline" -version = "0.1.0" -edition = "2021" - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) - .add_file( - "src/main.rs", + "src/lib.rs", r#" #[test] fn test_non_basic_plane() { @@ -197,9 +185,9 @@ fn test_trailing_comma_in_inline_snapshot() { assert!(&output.status.success()); - assert_snapshot!(test_project.diff("src/main.rs"), @r##" - --- Original: src/main.rs - +++ Updated: src/main.rs + assert_snapshot!(test_project.diff("src/lib.rs"), @r##" + --- Original: src/lib.rs + +++ Updated: src/lib.rs @@ -1,21 +1,19 @@ #[test] @@ -310,21 +298,9 @@ fn test_old_yaml_format() { #[test] fn test_hashtag_escape_in_inline_snapshot() { let test_project = TestFiles::new() + .add_cargo_toml("test_hashtag_escape") .add_file( - "Cargo.toml", - r#" -[package] -name = "test_hashtag_escape" -version = "0.1.0" -edition = "2021" - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) - .add_file( - "src/main.rs", + "src/lib.rs", r#####" #[test] fn test_hashtag_escape() { @@ -344,9 +320,9 @@ fn test_hashtag_escape() { assert!(&output.status.success()); - assert_snapshot!(test_project.diff("src/main.rs"), @r####" - --- Original: src/main.rs - +++ Updated: src/main.rs + assert_snapshot!(test_project.diff("src/lib.rs"), @r####" + --- Original: src/lib.rs + +++ Updated: src/lib.rs @@ -2,5 +2,8 @@ #[test] fn test_hashtag_escape() { diff --git a/cargo-insta/tests/functional/main.rs b/cargo-insta/tests/functional/main.rs index d92cd865..5eb02f34 100644 --- a/cargo-insta/tests/functional/main.rs +++ b/cargo-insta/tests/functional/main.rs @@ -106,6 +106,29 @@ impl TestFiles { self } + /// Adds a standard `Cargo.toml` (some tests may need to add_file themselves + /// with a different format) + fn add_cargo_toml(self, name: &str) -> Self { + self.add_file( + "Cargo.toml", + format!( + r#" +[package] +name = "{}" +version = "0.1.0" +edition = "2021" + +[lib] +doctest = false + +[dependencies] +insta = {{ path = '$PROJECT_PATH' }} +"#, + name + ), + ) + } + fn create_project(self) -> TestProject { TestProject::new(self.files) } @@ -314,22 +337,22 @@ Hello, world! let test_insta_1_40_0 = create_test_force_update_project("1_40_0", "\"1.40.0\""); // Test with current insta version - let output_current = test_current_insta + assert!(&test_current_insta .insta_cmd() .args(["test", "--accept", "--force-update-snapshots"]) .output() - .unwrap(); - - assert!(&output_current.status.success()); + .unwrap() + .status + .success()); // Test with insta 1.40.0 - let output_1_40_0 = test_insta_1_40_0 + assert!(&test_insta_1_40_0 .insta_cmd() .args(["test", "--accept", "--force-update-snapshots"]) .output() - .unwrap(); - - assert!(&output_1_40_0.status.success()); + .unwrap() + .status + .success()); // Check that both versions updated the snapshot correctly assert_snapshot!(test_current_insta.diff("src/snapshots/test_force_update_current__force_update.snap"), @r#" @@ -368,19 +391,7 @@ Hello, world! #[test] fn test_force_update_inline_snapshot_linebreaks() { let test_project = TestFiles::new() - .add_file( - "Cargo.toml", - r#" -[package] -name = "force-update-inline-linebreaks" -version = "0.1.0" -edition = "2021" - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) + .add_cargo_toml("force-update-inline-linebreaks") .add_file( "src/lib.rs", r#####" @@ -425,19 +436,7 @@ fn test_linebreaks() { #[test] fn test_force_update_inline_snapshot_hashes() { let test_project = TestFiles::new() - .add_file( - "Cargo.toml", - r#" -[package] -name = "force-update-inline-hashes" -version = "0.1.0" -edition = "2021" - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) + .add_cargo_toml("force-update-inline-hashes") .add_file( "src/lib.rs", r#####" @@ -531,22 +530,7 @@ fn test_matches_fully_linebreaks() { // Until #563 merges, we should be OK with different leading newlines, even // in exact / full match mode. let test_project = TestFiles::new() - .add_file( - "Cargo.toml", - r#" -[package] -name = "exact-match-inline" -version = "0.1.0" -edition = "2021" - -[lib] -doctest = false - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) + .add_cargo_toml("exact-match-inline") .add_file( "src/lib.rs", r#####" @@ -589,22 +573,7 @@ fn test_additional_linebreak() { #[test] fn test_snapshot_name_clash() { let test_project = TestFiles::new() - .add_file( - "Cargo.toml", - r#" -[package] -name = "snapshot_name_clash_test" -version = "0.1.0" -edition = "2021" - -[lib] -doctest = false - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) + .add_cargo_toml("snapshot_name_clash_test") .add_file( "src/lib.rs", r#" @@ -643,22 +612,7 @@ fn foo_always_missing() { #[test] fn test_unreferenced_delete() { let test_project = TestFiles::new() - .add_file( - "Cargo.toml", - r#" -[package] -name = "test_unreferenced_delete" -version = "0.1.0" -edition = "2021" - -[lib] -doctest = false - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) + .add_cargo_toml("test_unreferenced_delete") .add_file( "src/lib.rs", r#" @@ -743,19 +697,7 @@ Unused snapshot #[test] fn test_hidden_snapshots() { let test_project = TestFiles::new() - .add_file( - "Cargo.toml", - r#" -[package] -name = "test_hidden_snapshots" -version = "0.1.0" -edition = "2021" - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) + .add_cargo_toml("test_hidden_snapshots") .add_file( "src/lib.rs", r#" @@ -823,19 +765,7 @@ Hidden snapshot #[test] fn test_ignored_snapshots() { let test_project = TestFiles::new() - .add_file( - "Cargo.toml", - r#" -[package] -name = "test_ignored_snapshots" -version = "0.1.0" -edition = "2021" - -[dependencies] -insta = { path = '$PROJECT_PATH' } -"# - .to_string(), - ) + .add_cargo_toml("test_ignored_snapshots") .add_file( "src/lib.rs", r#" diff --git a/cargo-insta/tests/functional/workspace.rs b/cargo-insta/tests/functional/workspace.rs index f70c8d50..0b4841e7 100644 --- a/cargo-insta/tests/functional/workspace.rs +++ b/cargo-insta/tests/functional/workspace.rs @@ -389,19 +389,7 @@ fn test_insta_workspace_root() { } let test_project = TestFiles::new() - .add_file( - "Cargo.toml", - r#" - [package] - name = "insta_workspace_root_test" - version = "0.1.0" - edition = "2021" - - [dependencies] - insta = { path = '$PROJECT_PATH' } - "# - .to_string(), - ) + .add_cargo_toml("insta_workspace_root_test") .add_file( "src/lib.rs", r#"