Skip to content

Commit

Permalink
Added cargo insta test --check and CI detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Feb 2, 2023
1 parent 7c09822 commit 8f9a52c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion Makefile
Expand Up @@ -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"
Expand Down
22 changes: 20 additions & 2 deletions cargo-insta/src/cli.rs
Expand Up @@ -164,6 +164,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,
Expand Down Expand Up @@ -498,7 +501,12 @@ fn process_snapshots(
fn test_run(mut cmd: TestCommand, color: &str) -> Result<(), Box<dyn Error>> {
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;
Expand All @@ -514,6 +522,12 @@ fn test_run(mut cmd: TestCommand, color: &str) -> Result<(), Box<dyn Error>> {
}
}

// --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 {
Expand Down Expand Up @@ -798,7 +812,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");
Expand Down
6 changes: 5 additions & 1 deletion src/utils.rs
Expand Up @@ -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")]
Expand Down

0 comments on commit 8f9a52c

Please sign in to comment.