Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Jul 31, 2024
1 parent eacbefa commit 169a57d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cargo-insta/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ struct TestCommand {
/// Picks the test runner.
#[arg(long, default_value = "auto")]
test_runner: TestRunner,
#[arg(long)]
test_runner_fallback: Option<bool>,
/// Delete unreferenced snapshots after a successful test run.
#[arg(long, hide = true)]
delete_unreferenced_snapshots: bool,
Expand Down Expand Up @@ -616,6 +618,10 @@ fn test_run(mut cmd: TestCommand, color: ColorWhen) -> Result<(), Box<dyn Error>
TestRunner::CargoTest => TestRunner::CargoTest,
TestRunner::Nextest => TestRunner::Nextest,
};
// Prioritize the command line over the tool config
let test_runner_fallback = cmd
.test_runner_fallback
.unwrap_or(loc.tool_config.test_runner_fallback());

let (mut proc, snapshot_ref_file, prevents_doc_run) =
prepare_test_runner(test_runner, cmd.unreferenced, &cmd, color, &[], None)?;
Expand Down
18 changes: 18 additions & 0 deletions insta/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ pub struct ToolConfig {
#[cfg(feature = "glob")]
glob_fail_fast: bool,
#[cfg(feature = "_cargo_insta_internal")]
test_runner_fallback: bool,
#[cfg(feature = "_cargo_insta_internal")]
test_runner: TestRunner,
#[cfg(feature = "_cargo_insta_internal")]
test_unreferenced: UnreferencedSnapshots,
Expand Down Expand Up @@ -235,6 +237,15 @@ impl ToolConfig {
.map_err(|_| Error::Env("INSTA_TEST_RUNNER"))?
},
#[cfg(feature = "_cargo_insta_internal")]
test_runner_fallback: match env::var("INSTA_TEST_RUNNER_FALLBACK").as_deref() {
Err(_) | Ok("") => resolve(&cfg, &["test", "runner_fallback"])
.and_then(|x| x.as_bool())
.unwrap_or(false),
Ok("1") => true,
Ok("0") => false,
_ => return Err(Error::Env("INSTA_RUNNER_FALLBACK")),
},
#[cfg(feature = "_cargo_insta_internal")]
test_unreferenced: {
resolve(&cfg, &["test", "unreferenced"])
.and_then(|x| x.as_str())
Expand Down Expand Up @@ -265,6 +276,8 @@ impl ToolConfig {
})
}

// TODO: Do we want all these methods, vs. just allowing access to the fields?

/// Is insta told to force update snapshots?
pub fn force_update_snapshots(&self) -> bool {
self.force_update_snapshots
Expand Down Expand Up @@ -304,6 +317,11 @@ impl ToolConfig {
self.test_runner
}

/// Whether to fallback to `cargo test` if the test runner isn't available
pub fn test_runner_fallback(&self) -> bool {
self.test_runner_fallback
}

pub fn test_unreferenced(&self) -> UnreferencedSnapshots {
self.test_unreferenced
}
Expand Down
3 changes: 3 additions & 0 deletions insta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@
//! test:
//! # also set by INSTA_TEST_RUNNER
//! runner: "auto" | "cargo-test" | "nextest"
//! # whether to fallback to `cargo-test` if `nextest` is not available,
//! # also set by INSTA_TEST_RUNNER_FALLBACK, default false
//! test_runner_fallback: true/false
//! # automatically assume --review was passed to cargo insta test
//! auto_review: true/false
//! # automatically assume --accept-unseen was passed to cargo insta test
Expand Down

0 comments on commit 169a57d

Please sign in to comment.