Skip to content

Commit 18fc87d

Browse files
committed
Set INSTA_UPDATE
Previously, INSTA_UPDATE=always would cause any Insta tests in the program under test to update their snapshots and return success. This has two problems: 1. The tests succeed when they should fail, so mutants are counted as missed when they may actually have decent coverage. 2. The source tree is polluted by updates from Insta. Now cargo-mutants specifically knows about Insta and sets the env variable.
1 parent 3c77cc9 commit 18fc87d

File tree

13 files changed

+126
-36
lines changed

13 files changed

+126
-36
lines changed

Cargo.lock

Lines changed: 37 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ exclude = [
8282
"testdata/tree/factorial",
8383
"testdata/tree/hang_avoided_by_attr/",
8484
"testdata/tree/hang_when_mutated",
85+
"testdata/tree/insta",
8586
"testdata/tree/integration_tests",
8687
"testdata/tree/override_dependency",
8788
"testdata/tree/patch_dependency",

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
- Changed: Find mutants before doing a baseline test, so that you will find out earlier if there's nothing to test.
88

9+
- New: Set `INSTA_UPDATE=no` so that tests that use the Insta library (as cargo-mutants does for its own tests) don't write updates back into the source directory, and don't falsely pass.
10+
911
## 1.0.1
1012

1113
Released 2022-09-12

src/cargo.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ pub fn run_cargo(
6161
// <https://doc.rust-lang.org/rustc/lints/levels.html#capping-lints>
6262
// TODO: Maybe this should append instead of overwriting it...?
6363
env.push(("RUSTFLAGS".into(), "--cap-lints=allow".into()));
64+
// The tests might use Insta <https://insta.rs>, and we don't want it to write
65+
// updates to the source tree, and we *certainly* don't want it to write
66+
// updates and then let the test pass.
67+
env.push(("INSTA_UPDATE".into(), "no".into()));
6468

6569
let message = format!("run {}", argv.join(" "),);
6670
log_file.message(&message);

testdata/tree/insta/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "cargo-mutants-testdata-insta"
3+
version = "0.0.0"
4+
edition = "2018"
5+
authors = ["Martin Pool"]
6+
publish = false
7+
8+
[dependencies.insta]
9+
version = "1.19.1"
10+
default-features = false

testdata/tree/insta/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# cargo-mutants `insta` test tree
2+
3+
An example of a crate that uses the [Insta](https://insta.rs) test framework.
4+
5+
Insta in some modes will either write `.snap.new` files into the source directory, or update existing snapshots. We don't want either of those to happen when testing mutants.

testdata/tree/insta/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn say_hello(name: &str) -> String {
2+
format!("Hello, {name}!")
3+
}

testdata/tree/insta/tests/insta.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use cargo_mutants_testdata_insta::say_hello;
2+
3+
#[test]
4+
fn say_hello_vs_insta_snapshot() {
5+
let name = "Robin";
6+
insta::assert_snapshot!(say_hello(name));
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: tests/insta.rs
3+
expression: say_hello(name)
4+
---
5+
Hello, Robin!

tests/cli.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,25 @@ fn strict_warnings_about_unused_variables_are_disabled_so_mutants_compile() {
11451145
.stdout(contains("1 mutant tested: 1 caught"));
11461146
}
11471147

1148+
/// `INSTA_UPDATE=always` in the environment will cause Insta to update
1149+
/// the snaphots, so the tests will pass, so mutants will not be caught.
1150+
/// This test checks that cargo-mutants sets the environment variable
1151+
/// so that mutants are caught properly.
1152+
#[test]
1153+
fn insta_test_failures_are_detected() {
1154+
for insta_update in ["auto", "always"] {
1155+
println!("INSTA_UPDATE={insta_update}");
1156+
let tmp_src_dir = copy_of_testdata("insta");
1157+
run_assert_cmd()
1158+
.arg("mutants")
1159+
.args(["--no-times", "--no-shuffle", "--caught"])
1160+
.env("INSTA_UPDATE", insta_update)
1161+
.current_dir(tmp_src_dir.path())
1162+
.assert()
1163+
.success();
1164+
}
1165+
}
1166+
11481167
fn check_text_list_output(dir: &Path, test_name: &str) {
11491168
// There is a `missed.txt` file with the right content, etc.
11501169
for name in ["missed", "caught", "timeout", "unviable"] {

0 commit comments

Comments
 (0)