Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
with:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@nextest
- name: Test
run: make test

Expand All @@ -37,6 +38,7 @@ jobs:
with:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@nextest
- name: Test
run: cargo run -p cargo-insta -- test

Expand Down Expand Up @@ -64,6 +66,7 @@ jobs:
with:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@nextest
- run: cargo update
- name: Test
run: make test
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to insta and cargo-insta are documented here.

## Unreleased

- `cargo-insta` now passes all arguments following `cargo insta test --` directly to the test runner.
When using `nextest`, the test runner interprets arguments after an additional `--` separator as arguments
for test binaries. This allows setting nextest options like `--status-level` and test binary arguments in a
single command. Example: `cargo insta test -- --status-level fail -- --test-arg`. #795

**Note on backward compatibility:** With a single `--` separator, nextest treats unrecognized arguments
before its separator as test name filters that are passed to test binaries. This means
`cargo insta test -- test_name` continues to work as before. However, this only applies to arguments
that nextest doesn't recognize - nextest-specific flags would be consumed by nextest itself.
- Fix panics when `cargo metadata` fails to execute or parse (e.g., when cargo is not in PATH or returns invalid output). Now falls back to using the manifest directory as the workspace root. #798 (@adriangb)
- Changed diff line numbers to 1-based indexing.

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cargotest:
@cargo test -p insta --all-features
@cargo test -p insta --no-default-features
@cargo test -p insta --features redactions -- --test-threads 1
@command -v cargo-nextest >/dev/null 2>&1 || cargo install cargo-nextest --locked
@cargo test -p cargo-insta

check-minver:
Expand Down
72 changes: 63 additions & 9 deletions cargo-insta/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ struct ProcessCommand {
quiet: bool,
}

#[derive(Args, Debug)]
#[derive(Args, Debug, Clone)]
#[command(rename_all = "kebab-case", next_help_heading = "Test Runner Options")]
struct TestRunnerOptions {
/// Test only this package's library unit tests
Expand Down Expand Up @@ -175,7 +175,7 @@ struct TestRunnerOptions {
target: Option<String>,
}

#[derive(Args, Debug)]
#[derive(Args, Debug, Clone)]
#[command(rename_all = "kebab-case")]
struct TestCommand {
/// Accept all snapshots after test.
Expand Down Expand Up @@ -226,7 +226,21 @@ struct TestCommand {
target_args: TargetArgs,
#[command(flatten)]
test_runner_options: TestRunnerOptions,
/// Options passed to cargo test
/// Arguments passed to the test runner.
///
/// Use `--` to separate insta arguments from test runner arguments.
///
/// With cargo test:
/// `cargo insta test -- --nocapture`
///
/// With nextest, all arguments are passed to nextest, which interprets
/// its own `--` separator:
/// `cargo insta test -- --status-level fail -- --nocapture`
/// (nextest sees: `--status-level fail -- --nocapture`)
///
/// Single separator with nextest currently passes args to test binaries
/// by leveraging nextest's test filter behavior - unrecognized args before
/// nextest's separator are treated as test name filters.
#[arg(last = true)]
cargo_options: Vec<String>,
}
Expand Down Expand Up @@ -777,8 +791,18 @@ fn test_run(mut cmd: TestCommand, color: ColorWhen) -> Result<(), Box<dyn Error>
// even on crates that specify `doctests = false`. But I don't think there's
// a way to replicate the `cargo test` behavior.
if matches!(cmd.test_runner, TestRunner::Nextest) && !prevents_doc_run {
// When running doctests, we need to pass only the test binary args, not nextest args
// Create a modified command with only test binary args
let mut doctest_cmd = cmd.clone();

// If there's an additional separator, extract only the test binary args
if let Some(separator_pos) = cmd.cargo_options.iter().position(|arg| arg == "--") {
doctest_cmd.cargo_options = cmd.cargo_options[separator_pos + 1..].to_vec();
}
// If no additional separator, pass all args (maintains backward compatibility)

let (mut proc, _, _) = prepare_test_runner(
&cmd,
&doctest_cmd,
&TestRunner::CargoTest,
color,
&["--doc"],
Expand Down Expand Up @@ -1138,12 +1162,42 @@ fn prepare_test_runner<'snapshot_ref>(
}
proc.args(["--color", color.to_string().as_str()]);
proc.args(extra_args);
// Items after this are passed to the test runner
proc.arg("--");
if !cmd.no_quiet && matches!(test_runner, TestRunner::CargoTest) {
proc.arg("-q");

// Argument routing strategy:
// - Nextest: Pass all args BEFORE our separator. Nextest interprets its own -- within those args.
// This provides backward compatibility because nextest treats unrecognized args before its
// separator as test name filters that get passed to test binaries.
// - Cargo test: Pass all args AFTER our separator to test binaries.
if matches!(test_runner, TestRunner::Nextest) {
// Pass all args to nextest before our separator - nextest handles its own separator
proc.args(&cmd.cargo_options);
proc.arg("--");
} else {
// For cargo test, args go after the separator
proc.arg("--");
if !cmd.no_quiet {
proc.arg("-q");
}
proc.args(&cmd.cargo_options);
}

// Show deprecation warning for single -- with nextest
if matches!(test_runner, TestRunner::Nextest)
&& !cmd.cargo_options.is_empty()
&& !cmd.cargo_options.iter().any(|arg| arg == "--")
{
eprintln!(
"{} The single `--` separator with nextest will change behavior in a future version.\n\
\n Currently: `cargo insta test -- test-args` → passes to test binaries\n\
\n (nextest treats unrecognized args as test name filters)\n\
\n Future: `cargo insta test -- test-args` → passes to nextest\n\
\n (allows full control of nextest options)\n\
\n For explicit control use: `cargo insta test -- nextest-args -- test-binary-args`\n\
\n Note: This change only affects nextest users. Cargo test behavior remains unchanged.",
style("warning:").yellow().bold()
);
}
proc.args(&cmd.cargo_options);

// Currently libtest uses a different approach to color, so we need to pass
// it again to get output from the test runner as well as cargo. See
// https://github.com/rust-lang/cargo/issues/1983 for more
Expand Down
1 change: 1 addition & 0 deletions cargo-insta/tests/functional/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ mod binary;
mod delete_pending;
mod glob_filter;
mod inline;
mod nextest_args;
mod test_workspace_source_path;
mod unreferenced;
mod workspace;
Expand Down
Loading
Loading