Skip to content

Commit d609d7d

Browse files
authored
Add integration test for force updating (#580)
From #482
1 parent 02665ea commit d609d7d

File tree

1 file changed

+111
-1
lines changed

1 file changed

+111
-1
lines changed

cargo-insta/tests/main.rs

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
/// temporary workspace dirs. (We could try to enforce different names, or give
1313
/// up using a consistent target directory for a cache, but it would slow down
1414
/// repeatedly running the tests locally. To demonstrate the effect, name crates
15-
/// the same...)
15+
/// the same...). This also causes issues when running the same tests
16+
/// concurrently.
1617
use std::collections::HashMap;
1718
use std::env;
1819
use std::fs;
@@ -726,6 +727,115 @@ fn test_virtual_manifest_single_crate() {
726727
"### );
727728
}
728729

730+
#[test]
731+
fn test_force_update_snapshots() {
732+
// We test with both 1.39 and `master`. These currently test the same code!
733+
// But I copied the test from
734+
// https://github.com/mitsuhiko/insta/pull/482/files where they'll test
735+
// different code. If we don't end up merging that, we can remove one of the
736+
// tests (but didn't think it was worthwhile to do the work to then undo it)
737+
738+
fn create_test_force_update_project(name: &str, insta_dependency: &str) -> TestProject {
739+
TestFiles::new()
740+
.add_file(
741+
"Cargo.toml",
742+
format!(
743+
r#"
744+
[package]
745+
name = "test_force_update_{}"
746+
version = "0.1.0"
747+
edition = "2021"
748+
749+
[dependencies]
750+
insta = {}
751+
"#,
752+
name, insta_dependency
753+
)
754+
.to_string(),
755+
)
756+
.add_file(
757+
"src/lib.rs",
758+
r#"
759+
#[test]
760+
fn test_snapshot_with_newline() {
761+
insta::assert_snapshot!("force_update", "Hello, world!");
762+
}
763+
"#
764+
.to_string(),
765+
)
766+
.add_file(
767+
format!(
768+
"src/snapshots/test_force_update_{}__force_update.snap",
769+
name
770+
),
771+
r#"
772+
---
773+
source: src/lib.rs
774+
expression:
775+
---
776+
Hello, world!
777+
778+
779+
"#
780+
.to_string(),
781+
)
782+
.create_project()
783+
}
784+
785+
let test_current_insta =
786+
create_test_force_update_project("current", "{ path = '$PROJECT_PATH' }");
787+
let test_insta_1_39_0 = create_test_force_update_project("1_39_0", "\"1.39.0\"");
788+
789+
// Test with current insta version
790+
let output_current = test_current_insta
791+
.cmd()
792+
.args(["test", "--accept", "--force-update-snapshots"])
793+
.output()
794+
.unwrap();
795+
796+
assert_success(&output_current);
797+
798+
// Test with insta 1.39.0
799+
let output_1_39_0 = test_insta_1_39_0
800+
.cmd()
801+
.args(["test", "--accept", "--force-update-snapshots"])
802+
.output()
803+
.unwrap();
804+
805+
assert_success(&output_1_39_0);
806+
807+
// Check that both versions updated the snapshot correctly
808+
assert_snapshot!(test_current_insta.diff("src/snapshots/test_force_update_current__force_update.snap"), @r#"
809+
--- Original: src/snapshots/test_force_update_current__force_update.snap
810+
+++ Updated: src/snapshots/test_force_update_current__force_update.snap
811+
@@ -1,8 +1,5 @@
812+
-
813+
---
814+
source: src/lib.rs
815+
-expression:
816+
+expression: "\"Hello, world!\""
817+
---
818+
Hello, world!
819+
-
820+
-
821+
"#);
822+
823+
assert_snapshot!(test_insta_1_39_0.diff("src/snapshots/test_force_update_1_39_0__force_update.snap"), @r#"
824+
--- Original: src/snapshots/test_force_update_1_39_0__force_update.snap
825+
+++ Updated: src/snapshots/test_force_update_1_39_0__force_update.snap
826+
@@ -1,8 +1,5 @@
827+
-
828+
---
829+
source: src/lib.rs
830+
-expression:
831+
+expression: "\"Hello, world!\""
832+
---
833+
Hello, world!
834+
-
835+
-
836+
"#);
837+
}
838+
729839
#[test]
730840
fn test_force_update_inline_snapshot() {
731841
let test_project = TestFiles::new()

0 commit comments

Comments
 (0)