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

Fix test_runner in config #544

Merged
merged 32 commits into from
Aug 2, 2024
Merged
Changes from all commits
Commits
Show all changes
32 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
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
f03e2f9
Merge branch 'master' into clap-more
max-sixty Aug 1, 2024
6ce5e17
max-sixty Aug 1, 2024
b30191d
Merge branch 'clap-more' into tool-config-test-runner
max-sixty Aug 1, 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
110 changes: 63 additions & 47 deletions cargo-insta/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ struct TargetArgs {
/// Explicit path to the workspace root
#[arg(long, value_name = "PATH")]
workspace_root: Option<PathBuf>,
/// Sets the extensions to consider. Defaults to `.snap`
#[arg(short = 'e', long, value_name = "EXTENSIONS", num_args = 1.., value_delimiter = ',')]
/// Sets the extensions to consider. Defaults to `snap`.
#[arg(short = 'e', long, value_name = "EXTENSIONS", num_args = 1.., value_delimiter = ',', default_value = "snap")]
extensions: Vec<String>,
/// Work on all packages in the workspace
#[arg(long)]
Expand Down Expand Up @@ -117,10 +117,8 @@ struct ProcessCommand {
}

#[derive(Args, Debug)]
#[command(rename_all = "kebab-case")]
struct TestCommand {
#[command(flatten)]
target_args: TargetArgs,
#[command(rename_all = "kebab-case", next_help_heading = "Test Runner Options")]
struct TestRunnerOptions {
/// Test only this package's library unit tests
#[arg(long)]
lib: bool,
Expand Down Expand Up @@ -148,12 +146,6 @@ struct TestCommand {
/// Exclude packages from the test
#[arg(long, value_name = "SPEC")]
exclude: Vec<String>,
/// Disable force-passing of snapshot tests
#[arg(long)]
no_force_pass: bool,
/// Prevent running all tests regardless of failure
#[arg(long)]
fail_fast: bool,
/// Space-separated list of features to activate
#[arg(long, value_name = "FEATURES")]
features: Option<String>,
Expand All @@ -178,42 +170,57 @@ struct TestCommand {
/// Build for the target triple
#[arg(long)]
target: Option<String>,
}

#[derive(Args, Debug)]
#[command(rename_all = "kebab-case")]
struct TestCommand {
/// Accept all snapshots after test.
#[arg(long, conflicts_with_all = ["review", "check"])]
accept: bool,
/// Instructs the test command to just assert.
#[arg(long, conflicts_with_all = ["review"])]
check: bool,
/// Follow up with review.
#[arg(long)]
review: bool,
/// Accept all snapshots after test.
#[arg(long, conflicts_with = "review")]
accept: bool,
/// Accept all new (previously unseen).
#[arg(long)]
accept_unseen: bool,
/// Instructs the test command to just assert.
#[arg(long)]
check: bool,
/// Do not reject pending snapshots before run.
#[arg(long)]
keep_pending: bool,
/// Update all snapshots even if they are still matching.
#[arg(long)]
force_update_snapshots: bool,
/// Require metadata as well as snapshots' contents to match (experimental).
#[arg(long)]
require_full_match: bool,
/// Handle unreferenced snapshots after a successful test run.
#[arg(long, default_value = "ignore")]
unreferenced: UnreferencedSnapshots,
/// Delete unreferenced snapshots after a successful test run.
#[arg(long, hide = true)]
delete_unreferenced_snapshots: bool,
/// Filters to apply to the insta glob feature.
#[arg(long)]
glob_filter: Vec<String>,
/// Require metadata as well as snapshots' contents to match.
#[arg(long)]
require_full_match: bool,
/// Prevent running all tests regardless of failure
#[arg(long)]
fail_fast: bool,
/// Do not pass the quiet flag (`-q`) to tests.
#[arg(short = 'Q', long)]
no_quiet: bool,
/// Picks the test runner.
#[arg(long, default_value = "auto")]
test_runner: TestRunner,
/// Delete unreferenced snapshots after a successful test run.
#[arg(long, hide = true)]
delete_unreferenced_snapshots: bool,
/// Disable force-passing of snapshot tests
#[arg(long)]
no_force_pass: bool,
#[command(flatten)]
target_args: TargetArgs,
#[command(flatten)]
test_runner_options: TestRunnerOptions,
/// Options passed to cargo test
#[arg(last = true)]
cargo_options: Vec<String>,
Expand Down Expand Up @@ -362,10 +369,7 @@ fn get_find_flags(tool_config: &ToolConfig, target_args: &TargetArgs) -> FindFla
}

fn handle_target_args(target_args: &TargetArgs) -> Result<LocationInfo<'_>, Box<dyn Error>> {
let mut exts: Vec<&str> = target_args.extensions.iter().map(|x| x.as_str()).collect();
if exts.is_empty() {
exts.push("snap");
}
let exts: Vec<&str> = target_args.extensions.iter().map(|x| x.as_str()).collect();

// if a workspace root is provided we first check if it points to a `Cargo.toml`. If it
// does we instead treat it as manifest path. If both are provided we fail with an error
Expand Down Expand Up @@ -606,8 +610,15 @@ fn test_run(mut cmd: TestCommand, color: ColorWhen) -> Result<(), Box<dyn Error>
cmd.unreferenced = UnreferencedSnapshots::Delete;
}

// Prioritize the command line over the tool config
let test_runner = match cmd.test_runner {
TestRunner::Auto => loc.tool_config.test_runner(),
TestRunner::CargoTest => TestRunner::CargoTest,
TestRunner::Nextest => TestRunner::Nextest,
};

let (mut proc, snapshot_ref_file, prevents_doc_run) =
prepare_test_runner(cmd.test_runner, cmd.unreferenced, &cmd, color, &[], None)?;
prepare_test_runner(test_runner, cmd.unreferenced, &cmd, color, &[], None)?;

if !cmd.keep_pending {
process_snapshots(true, None, &loc, Some(Operation::Reject))?;
Expand Down Expand Up @@ -641,7 +652,12 @@ fn test_run(mut cmd: TestCommand, color: ColorWhen) -> Result<(), Box<dyn Error>
// tests ran successfully
if success {
if let Some(ref path) = snapshot_ref_file {
handle_unreferenced_snapshots(path.borrow(), &loc, cmd.unreferenced, &cmd.package[..])?;
handle_unreferenced_snapshots(
path.borrow(),
&loc,
cmd.unreferenced,
&cmd.test_runner_options.package[..],
)?;
}
}

Expand Down Expand Up @@ -823,42 +839,42 @@ fn prepare_test_runner<'snapshot_ref>(
if cmd.target_args.all || cmd.target_args.workspace {
proc.arg("--all");
}
if cmd.lib {
if cmd.test_runner_options.lib {
proc.arg("--lib");
prevents_doc_run = true;
}
if let Some(ref bin) = cmd.bin {
if let Some(ref bin) = cmd.test_runner_options.bin {
proc.arg("--bin");
proc.arg(bin);
prevents_doc_run = true;
}
if cmd.bins {
if cmd.test_runner_options.bins {
proc.arg("--bins");
prevents_doc_run = true;
}
if let Some(ref example) = cmd.example {
if let Some(ref example) = cmd.test_runner_options.example {
proc.arg("--example");
proc.arg(example);
prevents_doc_run = true;
}
if cmd.examples {
if cmd.test_runner_options.examples {
proc.arg("--examples");
prevents_doc_run = true;
}
for test in &cmd.test {
for test in &cmd.test_runner_options.test {
proc.arg("--test");
proc.arg(test);
prevents_doc_run = true;
}
if cmd.tests {
if cmd.test_runner_options.tests {
proc.arg("--tests");
prevents_doc_run = true;
}
for pkg in &cmd.package {
for pkg in &cmd.test_runner_options.package {
proc.arg("--package");
proc.arg(pkg);
}
for spec in &cmd.exclude {
for spec in &cmd.test_runner_options.exclude {
proc.arg("--exclude");
proc.arg(spec);
}
Expand Down Expand Up @@ -903,32 +919,32 @@ fn prepare_test_runner<'snapshot_ref>(
if !glob_filter.is_empty() {
proc.env("INSTA_GLOB_FILTER", glob_filter);
}
if cmd.release {
if cmd.test_runner_options.release {
proc.arg("--release");
}
if let Some(ref profile) = cmd.profile {
if let Some(ref profile) = cmd.test_runner_options.profile {
proc.arg("--profile");
proc.arg(profile);
}
if cmd.all_targets {
if cmd.test_runner_options.all_targets {
proc.arg("--all-targets");
}
if let Some(n) = cmd.jobs {
if let Some(n) = cmd.test_runner_options.jobs {
// use -j instead of --jobs since both nextest and cargo test use it
proc.arg("-j");
proc.arg(n.to_string());
}
if let Some(ref features) = cmd.features {
if let Some(ref features) = cmd.test_runner_options.features {
proc.arg("--features");
proc.arg(features);
}
if cmd.all_features {
if cmd.test_runner_options.all_features {
proc.arg("--all-features");
}
if cmd.no_default_features {
if cmd.test_runner_options.no_default_features {
proc.arg("--no-default-features");
}
if let Some(ref target) = cmd.target {
if let Some(ref target) = cmd.test_runner_options.target {
proc.arg("--target");
proc.arg(target);
}
Expand Down
Loading