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

Small refactorings & comments #654

Merged
merged 1 commit into from
Oct 10, 2024
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
53 changes: 23 additions & 30 deletions cargo-insta/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ struct TargetArgs {
#[arg(long)]
all: bool,
/// Also walk into ignored paths.
// TODO: this alias is confusing, I think we should remove — does "no" mean
// "don't ignore files" or "not ignored files"?
#[arg(long, alias = "no-ignore")]
include_ignored: bool,
/// Also include hidden paths.
Expand Down Expand Up @@ -668,7 +670,7 @@ fn test_run(mut cmd: TestCommand, color: ColorWhen) -> Result<(), Box<dyn Error>

// Legacy command
if cmd.delete_unreferenced_snapshots {
println!("Warning: `--delete-unreferenced-snapshots` is deprecated. Use `--unreferenced=delete` instead.");
eprintln!("Warning: `--delete-unreferenced-snapshots` is deprecated. Use `--unreferenced=delete` instead.");
cmd.unreferenced = UnreferencedSnapshots::Delete;
}

Expand Down Expand Up @@ -697,6 +699,7 @@ fn test_run(mut cmd: TestCommand, color: ColorWhen) -> Result<(), Box<dyn Error>
process_snapshots(true, None, &loc, Some(Operation::Reject))?;
}

// Run the tests
let status = proc.status()?;
let mut success = status.success();

Expand Down Expand Up @@ -888,6 +891,7 @@ fn handle_unreferenced_snapshots(
Ok(())
}

/// Create and setup a `Command`, translating our configs into env vars & cli options
#[allow(clippy::type_complexity)]
fn prepare_test_runner<'snapshot_ref>(
test_runner: TestRunner,
Expand All @@ -902,37 +906,29 @@ 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
}
}
// Fall back to `cargo test` if `cargo nextest` isn't installed and
// `test_runner_fallback` is true
let test_runner = if test_runner == TestRunner::Nextest
&& test_runner_fallback
&& std::process::Command::new("cargo")
.arg("nextest")
.arg("--version")
.output()
.map(|output| !output.status.success())
.unwrap_or(true)
{
TestRunner::Auto
} else {
test_runner
};
let mut proc = match test_runner {
let mut proc = process::Command::new(cargo);
match test_runner {
TestRunner::CargoTest | TestRunner::Auto => {
let mut proc = process::Command::new(cargo);
proc.arg("test");
proc
}
TestRunner::Nextest => {
let mut proc = process::Command::new(cargo);
proc.arg("nextest");
proc.arg("run");
proc
}
};

Expand Down Expand Up @@ -1077,17 +1073,14 @@ fn prepare_test_runner<'snapshot_ref>(
proc.arg("--target");
proc.arg(target);
}
proc.arg("--color");
proc.arg(color.to_string());
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");
}
if !cmd.cargo_options.is_empty() {
proc.args(&cmd.cargo_options);
}
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
4 changes: 3 additions & 1 deletion insta/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ impl<'a> SnapshotAssertionContext<'a> {
&self,
new_snapshot: Snapshot,
) -> Result<SnapshotUpdateBehavior, Box<dyn Error>> {
// TODO: this seems to be making `unseen` be true when there is an
// existing snapshot file; which seems wrong??
let unseen = self
.snapshot_file
.as_ref()
Expand All @@ -527,8 +529,8 @@ impl<'a> SnapshotAssertionContext<'a> {
// If snapshot_update is `InPlace` and we have an inline snapshot, then
// use `NewFile`, since we can't use `InPlace` for inline. `cargo-insta`
// then accepts all snapshots at the end of the test.

let snapshot_update =
// TOOD: could match on the snapshot kind instead of whether snapshot_file is None
if snapshot_update == SnapshotUpdateBehavior::InPlace && self.snapshot_file.is_none() {
SnapshotUpdateBehavior::NewFile
} else {
Expand Down
Loading