Skip to content

Commit

Permalink
Take advantage of some clap features
Browse files Browse the repository at this point in the history
Stacks on mitsuhiko#518
  • Loading branch information
max-sixty committed Jul 5, 2024
1 parent c2c1f88 commit eedaf2c
Showing 1 changed file with 45 additions and 35 deletions.
80 changes: 45 additions & 35 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.
#[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: Option<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,11 +170,25 @@ struct TestCommand {
/// Build for the target triple
#[arg(long)]
target: Option<String>,
}

#[derive(Args, Debug)]
#[command(rename_all = "kebab-case")]

struct TestCommand {
#[command(flatten)]
target_args: TargetArgs,
/// Disable force-passing of snapshot tests
#[arg(long)]
no_force_pass: bool,
/// Prevent running all tests regardless of failure
#[arg(long)]
fail_fast: bool,
/// Follow up with review.
#[arg(long)]
review: bool,
/// Accept all snapshots after test.
#[arg(long, conflicts_with = "review")]
#[arg(long, conflicts_with_all = ["review", "check"])]
accept: bool,
/// Accept all new (previously unseen).
#[arg(long)]
Expand Down Expand Up @@ -214,6 +220,8 @@ struct TestCommand {
/// Picks the test runner.
#[arg(long, default_value = "auto")]
test_runner: TestRunner,
#[command(flatten)]
test_runner_options: TestRunnerOptions,
/// Options passed to cargo test
#[arg(last = true)]
cargo_options: Vec<String>,
Expand Down Expand Up @@ -362,10 +370,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 @@ -641,7 +646,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 @@ -820,42 +830,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);
}
if let Some(ref spec) = cmd.exclude {
if let Some(ref spec) = cmd.test_runner_options.exclude {
proc.arg("--exclude");
proc.arg(spec);
}
Expand Down Expand Up @@ -903,32 +913,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

0 comments on commit eedaf2c

Please sign in to comment.