Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to fall back to cargo test #545

Merged
merged 33 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0e84072
[draft] Upgrade `structopt` to `clap`
max-sixty Jul 1, 2024
c2c1f88
take advantage of some clap features to simplify the code
max-sixty Jul 4, 2024
32a427f
Take advantage of some `clap` features
max-sixty Jul 5, 2024
25a616d
Merge branch 'master' into clap
max-sixty Jul 7, 2024
9a84fe9
Merge branch 'master' into clap-more
max-sixty Jul 7, 2024
233381d
Merge branch 'master' into clap
max-sixty Jul 7, 2024
c89b310
Upgrade to syn 2
max-sixty Jul 1, 2024
0370230
max-sixty Jul 7, 2024
6287ee1
Merge branch 'syn2' into clap
max-sixty Jul 7, 2024
7d1e176
Bump `cargo-insta` toolchaine to 1.64 (leaves `insta` at 1.51)
max-sixty Jul 7, 2024
d424408
Merge branch 'clap' into clap-more
max-sixty Jul 7, 2024
e44b795
Merge branch 'master' into clap
max-sixty Jul 15, 2024
69f642a
Merge branch 'clap' into clap-more
max-sixty Jul 15, 2024
347b11a
Merge branch 'master' into clap
max-sixty Jul 31, 2024
5fc57a1
Merge branch 'clap' into clap-more
max-sixty Jul 31, 2024
f3cbd5b
Fix `test_runner` in config
max-sixty Jul 31, 2024
eacbefa
max-sixty Jul 31, 2024
169a57d
.
max-sixty Jul 31, 2024
9ac7b0e
Add an option to fall back to `cargo test`
max-sixty Jul 31, 2024
84903f4
Merge branch 'master' into clap
mitsuhiko Aug 1, 2024
5df91c6
Update Cargo.toml
mitsuhiko Aug 1, 2024
42e8e47
Merge branch 'master' into clap-more
mitsuhiko Aug 1, 2024
60d8b4b
cargo.lock merge
max-sixty Aug 1, 2024
b386077
Merge branch 'master' into clap
max-sixty Aug 1, 2024
dc24480
Merge branch 'master' into clap
max-sixty Aug 1, 2024
86d8f4e
max-sixty Aug 1, 2024
169e899
max-sixty Aug 1, 2024
71e0eb1
max-sixty Aug 1, 2024
def2e95
max-sixty Aug 1, 2024
9341bd3
Merge branch 'clap' into clap-more
max-sixty Aug 1, 2024
b106307
Merge branch 'clap-more' into tool-config-test-runner
max-sixty Aug 1, 2024
b15c98b
Merge branch 'tool-config-test-runner' into test-runner-fallback
max-sixty Aug 1, 2024
8eb15bb
Merge branch 'master' into test-runner-fallback
max-sixty Aug 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 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,9 +618,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 @@ -631,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 @@ -796,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 @@ -806,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
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
Loading