Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration test for force updating #580

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 111 additions & 1 deletion cargo-insta/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -716,6 +717,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()
Expand Down
Loading