Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to insta and cargo-insta are documented here.

## Unreleased

- Add `--disable-nextest-doctest` flag to `cargo insta test` to disable running doctests with
nextest. Shows a deprecation warning when nextest is used with doctests without this flag, to prepare `cargo insta` to no longer run
a separate doctest process when using nextest in the future. #803

## 1.43.2

- 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)
Expand Down
38 changes: 37 additions & 1 deletion cargo-insta/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ struct TestCommand {
/// Disable force-passing of snapshot tests (deprecated)
#[arg(long, hide = true)]
no_force_pass: bool,
/// Disable running doctests when using nextest test runner
#[arg(long)]
disable_nextest_doctest: bool,
#[command(flatten)]
target_args: TargetArgs,
#[command(flatten)]
Expand Down Expand Up @@ -673,6 +676,27 @@ fn review_snapshots(
Ok(())
}

/// Check if any of the packages have doctests
fn has_doctests(packages: &[Package]) -> bool {
for package in packages {
for target in &package.targets {
// Skip non-source targets
if target.kind.iter().any(|kind| kind == "custom-build") {
continue;
}

// Check if the target source file exists and contains doctests
if let Ok(content) = fs::read_to_string(&target.src_path) {
// Look for doc comment blocks with code blocks
if content.contains("/// ```") || content.contains("//! ```") {
return true;
}
}
}
}
false
}

/// Run the tests
fn test_run(mut cmd: TestCommand, color: ColorWhen) -> Result<(), Box<dyn Error>> {
let loc = handle_target_args(&cmd.target_args, &cmd.test_runner_options.package)?;
Expand Down Expand Up @@ -776,7 +800,19 @@ fn test_run(mut cmd: TestCommand, color: ColorWhen) -> Result<(), Box<dyn Error>
// Note that unlike `cargo test`, `cargo test --doctest` will run doctests
// 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 {
if matches!(cmd.test_runner, TestRunner::Nextest)
&& !prevents_doc_run
&& !cmd.disable_nextest_doctest
{
// Check if there are doctests and show warning
if has_doctests(&loc.packages) {
eprintln!(
"{}: insta won't run a separate doctest process when using nextest in the future. \
Pass `--disable-nextest-doctest` to update to this behavior now and silence this warning.",
style("warning").bold().yellow()
);
}

let (mut proc, _, _) = prepare_test_runner(
&cmd,
&TestRunner::CargoTest,
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_doctest;
mod test_workspace_source_path;
mod unreferenced;
mod workspace;
Expand Down
183 changes: 183 additions & 0 deletions cargo-insta/tests/functional/nextest_doctest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
use std::process::Stdio;

use crate::TestFiles;

/// Test that nextest with doctests shows a warning
#[test]
fn test_nextest_doctest_warning() {
let test_project = TestFiles::new()
.add_cargo_toml("test_nextest_doctest_warning")
.add_file(
"src/lib.rs",
r#"
/// This is a function with a doctest
///
/// ```
/// assert_eq!(test_nextest_doctest_warning::add(2, 2), 4);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
a + b
}

#[test]
fn test_simple() {
insta::assert_snapshot!("test_value", @"test_value");
}
"#
.to_string(),
)
.create_project();

// Run with nextest and capture stderr to check for warning
let output = test_project
.insta_cmd()
.args(["test", "--test-runner", "nextest", "--accept"])
.stderr(Stdio::piped())
.output()
.unwrap();

assert!(output.status.success());

let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains(
"warning: insta won't run a separate doctest process when using nextest in the future"
),
"Expected warning message not found in stderr:\n{stderr}"
);
assert!(
stderr.contains("Pass `--disable-nextest-doctest` to update to this behavior now and silence this warning"),
"Expected flag suggestion not found in stderr:\n{stderr}"
);
}

/// Test that nextest with --disable-nextest-doctest flag doesn't show warning
#[test]
fn test_nextest_doctest_flag_no_warning() {
let test_project = TestFiles::new()
.add_cargo_toml("test_nextest_doctest_flag")
.add_file(
"src/lib.rs",
r#"
/// This is a function with a doctest
///
/// ```
/// assert_eq!(test_nextest_doctest_flag::add(2, 2), 4);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
a + b
}

#[test]
fn test_simple() {
insta::assert_snapshot!("test_value", @"test_value");
}
"#
.to_string(),
)
.create_project();

// Run with nextest and the flag, capture stderr to verify no warning
let output = test_project
.insta_cmd()
.args([
"test",
"--test-runner",
"nextest",
"--disable-nextest-doctest",
"--accept",
])
.stderr(Stdio::piped())
.output()
.unwrap();

assert!(output.status.success());

let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
!stderr.contains("warning: insta won't run a separate doctest process"),
"Warning message should not appear when flag is used:\n{stderr}"
);
}

/// Test that no warning appears when there are no doctests
#[test]
fn test_nextest_no_doctests_no_warning() {
let test_project = TestFiles::new()
.add_cargo_toml("test_nextest_no_doctests")
.add_file(
"src/lib.rs",
r#"
// No doctests here
pub fn add(a: i32, b: i32) -> i32 {
a + b
}

#[test]
fn test_simple() {
insta::assert_snapshot!("test_value", @"test_value");
}
"#
.to_string(),
)
.create_project();

// Run with nextest when there are no doctests
let output = test_project
.insta_cmd()
.args(["test", "--test-runner", "nextest", "--accept"])
.stderr(Stdio::piped())
.output()
.unwrap();

assert!(output.status.success());

let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
!stderr.contains("warning: insta won't run a separate doctest process"),
"Warning should not appear when there are no doctests:\n{stderr}"
);
}

/// Test that cargo-test doesn't show the warning even with doctests
#[test]
fn test_cargo_test_no_warning() {
let test_project = TestFiles::new()
.add_cargo_toml("test_cargo_test_no_warning")
.add_file(
"src/lib.rs",
r#"
/// This is a function with a doctest
///
/// ```
/// assert_eq!(test_cargo_test_no_warning::add(2, 2), 4);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
a + b
}

#[test]
fn test_simple() {
insta::assert_snapshot!("test_value", @"test_value");
}
"#
.to_string(),
)
.create_project();

// Run with cargo-test (should not show warning)
let output = test_project
.insta_cmd()
.args(["test", "--test-runner", "cargo-test", "--accept"])
.stderr(Stdio::piped())
.output()
.unwrap();

assert!(output.status.success());

let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
!stderr.contains("warning: insta won't run a separate doctest process"),
"Warning should not appear with cargo-test runner:\n{stderr}"
);
}
Loading