From d609d7d3a86339ce695d942aed6a1c98b83c1066 Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Sun, 1 Sep 2024 16:20:45 -0700 Subject: [PATCH] Add integration test for force updating (#580) From https://github.com/mitsuhiko/insta/pull/482 --- cargo-insta/tests/main.rs | 112 +++++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-) diff --git a/cargo-insta/tests/main.rs b/cargo-insta/tests/main.rs index bccb2834..a87dafa0 100644 --- a/cargo-insta/tests/main.rs +++ b/cargo-insta/tests/main.rs @@ -12,7 +12,8 @@ /// temporary workspace dirs. (We could try to enforce different names, or give /// up using a consistent target directory for a cache, but it would slow down /// repeatedly running the tests locally. To demonstrate the effect, name crates -/// the same...) +/// the same...). This also causes issues when running the same tests +/// concurrently. use std::collections::HashMap; use std::env; use std::fs; @@ -726,6 +727,115 @@ fn test_virtual_manifest_single_crate() { "### ); } +#[test] +fn test_force_update_snapshots() { + // We test with both 1.39 and `master`. These currently test the same code! + // But I copied the test from + // https://github.com/mitsuhiko/insta/pull/482/files where they'll test + // different code. If we don't end up merging that, we can remove one of the + // tests (but didn't think it was worthwhile to do the work to then undo it) + + fn create_test_force_update_project(name: &str, insta_dependency: &str) -> TestProject { + TestFiles::new() + .add_file( + "Cargo.toml", + format!( + r#" +[package] +name = "test_force_update_{}" +version = "0.1.0" +edition = "2021" + +[dependencies] +insta = {} +"#, + name, insta_dependency + ) + .to_string(), + ) + .add_file( + "src/lib.rs", + r#" +#[test] +fn test_snapshot_with_newline() { + insta::assert_snapshot!("force_update", "Hello, world!"); +} +"# + .to_string(), + ) + .add_file( + format!( + "src/snapshots/test_force_update_{}__force_update.snap", + name + ), + r#" +--- +source: src/lib.rs +expression: +--- +Hello, world! + + +"# + .to_string(), + ) + .create_project() + } + + let test_current_insta = + create_test_force_update_project("current", "{ path = '$PROJECT_PATH' }"); + let test_insta_1_39_0 = create_test_force_update_project("1_39_0", "\"1.39.0\""); + + // Test with current insta version + let output_current = test_current_insta + .cmd() + .args(["test", "--accept", "--force-update-snapshots"]) + .output() + .unwrap(); + + assert_success(&output_current); + + // Test with insta 1.39.0 + let output_1_39_0 = test_insta_1_39_0 + .cmd() + .args(["test", "--accept", "--force-update-snapshots"]) + .output() + .unwrap(); + + assert_success(&output_1_39_0); + + // Check that both versions updated the snapshot correctly + assert_snapshot!(test_current_insta.diff("src/snapshots/test_force_update_current__force_update.snap"), @r#" + --- Original: src/snapshots/test_force_update_current__force_update.snap + +++ Updated: src/snapshots/test_force_update_current__force_update.snap + @@ -1,8 +1,5 @@ + - + --- + source: src/lib.rs + -expression: + +expression: "\"Hello, world!\"" + --- + Hello, world! + - + - + "#); + + assert_snapshot!(test_insta_1_39_0.diff("src/snapshots/test_force_update_1_39_0__force_update.snap"), @r#" + --- Original: src/snapshots/test_force_update_1_39_0__force_update.snap + +++ Updated: src/snapshots/test_force_update_1_39_0__force_update.snap + @@ -1,8 +1,5 @@ + - + --- + source: src/lib.rs + -expression: + +expression: "\"Hello, world!\"" + --- + Hello, world! + - + - + "#); +} + #[test] fn test_force_update_inline_snapshot() { let test_project = TestFiles::new()