From 4884685da274ffc7b8bdad6318dd103b7bcd4967 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Thu, 2 Feb 2023 10:53:57 +0100 Subject: [PATCH] Added cargo insta test --check and CI detection (#345) --- CHANGELOG.md | 4 ++++ Makefile | 4 +++- cargo-insta/src/cli.rs | 22 ++++++++++++++++++++-- src/utils.rs | 6 +++++- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2713238..de4e5ae6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to insta and cargo-insta are documented here. - Fix an issue where the inline snapshot patcher could panic in certain situations. (#341) +- `cargo insta test` now correctly detects CI environments like + `cargo test` does. In that case it will by fail rather than + create snapshot update files. (#345) +- Added `cargo insta test --check` to force check runs. (#345) ## 1.26.0 diff --git a/Makefile b/Makefile index b9678671..b73d1353 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,9 @@ test: cargotest cargo-insta-tests cargo-insta-tests: @echo "CARGO-INSTA INTEGRATION TESTS" - @cd cargo-insta/integration-tests; cargo run + # Turn off CI flag so that cargo insta test behaves as we expect + # under normal operation + @cd cargo-insta/integration-tests; CI=0 cargo run cargotest: @echo "CARGO TESTS" diff --git a/cargo-insta/src/cli.rs b/cargo-insta/src/cli.rs index 963e1bc5..8b64b544 100644 --- a/cargo-insta/src/cli.rs +++ b/cargo-insta/src/cli.rs @@ -167,6 +167,9 @@ pub struct TestCommand { /// Accept all new (previously unseen). #[structopt(long)] pub accept_unseen: bool, + /// Instructs the test command to just assert. + #[structopt(long)] + pub check: bool, /// Do not reject pending snapshots before run. #[structopt(long)] pub keep_pending: bool, @@ -501,7 +504,12 @@ fn process_snapshots( fn test_run(mut cmd: TestCommand, color: &str) -> Result<(), Box> { let loc = handle_target_args(&cmd.target_args)?; match loc.tool_config.snapshot_update() { - SnapshotUpdate::Auto | SnapshotUpdate::New | SnapshotUpdate::No => {} + SnapshotUpdate::Auto => { + if is_ci() { + cmd.check = true; + } + } + SnapshotUpdate::New | SnapshotUpdate::No => {} SnapshotUpdate::Always => { if !cmd.accept && !cmd.accept_unseen && !cmd.review { cmd.review = false; @@ -517,6 +525,12 @@ fn test_run(mut cmd: TestCommand, color: &str) -> Result<(), Box> { } } + // --check always implies --no-force-pass as otherwise this command does not + // make a lot of sense. + if cmd.check { + cmd.no_force_pass = true + } + // the tool config can also indicate that --accept-unseen should be picked // automatically unless instructed otherwise. if loc.tool_config.auto_accept_unseen() && !cmd.accept && !cmd.review { @@ -797,7 +811,11 @@ fn prepare_test_runner<'snapshot_ref>( } proc.env( "INSTA_UPDATE", - if cmd.accept_unseen { "unseen" } else { "new" }, + match (cmd.check, cmd.accept_unseen) { + (true, _) => "no", + (_, true) => "unseen", + (_, false) => "new", + }, ); if cmd.force_update_snapshots { proc.env("INSTA_FORCE_UPDATE_SNAPSHOTS", "1"); diff --git a/src/utils.rs b/src/utils.rs index 1bf1b5e7..d4d5a0d0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -8,7 +8,11 @@ use std::{ /// Are we running in in a CI environment? pub fn is_ci() -> bool { - env::var("CI").is_ok() || env::var("TF_BUILD").is_ok() + match env::var("CI").ok().as_deref() { + Some("false") | Some("0") | Some("") => false, + None => env::var("TF_BUILD").is_ok(), + Some(_) => true, + } } #[cfg(feature = "colors")]