Skip to content

Commit

Permalink
Add an option to fall back to cargo test
Browse files Browse the repository at this point in the history
This allows a project to default to `nextest` in its insta config, while falling back to `cargo test` when that isn't available.

Stacks on mitsuhiko#544

Needs an integration test, will add when mitsuhiko#537 merges
  • Loading branch information
max-sixty committed Jul 31, 2024
1 parent 169a57d commit 9ac7b0e
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions cargo-insta/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,15 @@ fn test_run(mut cmd: TestCommand, color: ColorWhen) -> Result<(), Box<dyn Error>
.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)?;
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 +644,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 +810,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 +821,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

0 comments on commit 9ac7b0e

Please sign in to comment.