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 eaf3368
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 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
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
2 changes: 1 addition & 1 deletion tests/test_basic.rs
Expand Up @@ -12,7 +12,7 @@ fn test_debug_vector() {

#[test]
fn test_unnamed_debug_vector() {
assert_debug_snapshot!(vec![1, 2, 3]);
assert_debug_snapshot!(vec![1, 2, 3, 4]);
assert_debug_snapshot!(vec![1, 2, 3, 4]);
assert_debug_snapshot!(vec![1, 2, 3, 4, 5]);
}
Expand Down

0 comments on commit eaf3368

Please sign in to comment.