Skip to content

Commit

Permalink
Add an option to fall back to cargo test (#545)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty authored Aug 8, 2024
1 parent 2ec44f2 commit 3db3ba8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
41 changes: 38 additions & 3 deletions cargo-insta/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,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 @@ -618,9 +620,20 @@ fn test_run(mut cmd: TestCommand, color: ColorWhen) -> Result<(), Box<dyn Error>
TestRunner::CargoTest => TestRunner::CargoTest,
TestRunner::Nextest => TestRunner::Nextest,
};

let (mut proc, snapshot_ref_file, prevents_doc_run) =
prepare_test_runner(test_runner, cmd.unreferenced, &cmd, color, &[], None)?;
// 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,
test_runner_fallback,
cmd.unreferenced,
&cmd,
color,
&[],
None,
)?;

if !cmd.keep_pending {
process_snapshots(true, None, &loc, Some(Operation::Reject))?;
Expand All @@ -637,6 +650,7 @@ fn test_run(mut cmd: TestCommand, color: ColorWhen) -> Result<(), Box<dyn Error>
if matches!(cmd.test_runner, TestRunner::Nextest) && !prevents_doc_run {
let (mut proc, _, _) = prepare_test_runner(
TestRunner::CargoTest,
false,
cmd.unreferenced,
&cmd,
color,
Expand Down Expand Up @@ -802,6 +816,7 @@ fn handle_unreferenced_snapshots(
#[allow(clippy::type_complexity)]
fn prepare_test_runner<'snapshot_ref>(
test_runner: TestRunner,
test_runner_fallback: bool,
unreferenced: UnreferencedSnapshots,
cmd: &TestCommand,
color: ColorWhen,
Expand All @@ -812,6 +827,26 @@ fn prepare_test_runner<'snapshot_ref>(
let cargo = cargo
.as_deref()
.unwrap_or_else(|| std::ffi::OsStr::new("cargo"));
let test_runner = match test_runner {
TestRunner::CargoTest | TestRunner::Auto => test_runner,
TestRunner::Nextest => {
// Fall back to `cargo test` if `cargo nextest` isn't installed and
// `test_runner_fallback` is true (but don't run the cargo command
// unless that's an option)
if !test_runner_fallback
|| std::process::Command::new("cargo")
.arg("nextest")
.arg("--version")
.output()
.map(|output| output.status.success())
.unwrap_or(false)
{
TestRunner::Nextest
} else {
TestRunner::Auto
}
}
};
let mut proc = match test_runner {
TestRunner::CargoTest | TestRunner::Auto => {
let mut proc = process::Command::new(cargo);
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 @@ -224,6 +224,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 3db3ba8

Please sign in to comment.