From 41ae64668daa269353e5eabe5a0b2298697e4f2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A8=E3=83=AA=E3=82=A2=E3=82=B9?= <me@elias.sh> Date: Wed, 31 May 2023 18:19:26 +0100 Subject: [PATCH] Running cargo semver-checks with no other arguments should default to checking (#459) * cargo semver-checks with no other arguments should default to checking * Update tests/feature_config.rs Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> * addresses comments on PR * cargo fmt * Update src/main.rs Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> * address comments from pr * trailing line between constant and method --------- Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> --- src/main.rs | 28 +++---- tests/feature_config.rs | 176 +++++++++++++++++++++++++++++----------- 2 files changed, 140 insertions(+), 64 deletions(-) diff --git a/src/main.rs b/src/main.rs index 57cd57ae..62663573 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,7 +48,7 @@ fn main() -> anyhow::Result<()> { )?; } - let mut config = GlobalConfig::new().set_level(args.verbosity.log_level()); + let mut config = GlobalConfig::new().set_level(args.check_release.verbosity.log_level()); config.shell_note("Use `--explain <id>` to see more details")?; std::process::exit(0); } else if let Some(id) = args.explain.as_deref() { @@ -75,19 +75,15 @@ fn main() -> anyhow::Result<()> { std::process::exit(0); } - match args.command { - Some(SemverChecksCommands::CheckRelease(args)) => { - let check: cargo_semver_checks::Check = args.into(); - let report = check.check_release()?; - if report.success() { - std::process::exit(0) - } else { - std::process::exit(1); - } - } - None => { - anyhow::bail!("subcommand required"); - } + let check: cargo_semver_checks::Check = match args.command { + Some(SemverChecksCommands::CheckRelease(args)) => args.into(), + None => args.check_release.into(), + }; + let report = check.check_release()?; + if report.success() { + std::process::exit(0); + } else { + std::process::exit(1); } } @@ -111,8 +107,8 @@ struct SemverChecks { #[arg(long, global = true, exclusive = true)] list: bool, - #[command(flatten)] - verbosity: clap_verbosity_flag::Verbosity<clap_verbosity_flag::InfoLevel>, + #[clap(flatten)] + check_release: CheckRelease, #[command(subcommand)] command: Option<SemverChecksCommands>, diff --git a/tests/feature_config.rs b/tests/feature_config.rs index 15e0ee3a..32c0d0e4 100644 --- a/tests/feature_config.rs +++ b/tests/feature_config.rs @@ -1,14 +1,14 @@ use assert_cmd::{assert::Assert, Command}; struct CargoSemverChecks { - cmd: Command, args: Vec<String>, } impl CargoSemverChecks { + const SUBCOMMAND_ARGS_INDEX: usize = 1; + fn new(current_path: &str, baseline_path: &str) -> Self { Self { - cmd: Command::cargo_bin("cargo-semver-checks").unwrap(), args: vec![ String::from("semver-checks"), String::from("check-release"), @@ -18,13 +18,27 @@ impl CargoSemverChecks { } } + fn command(&self) -> Command { + Command::cargo_bin("cargo-semver-checks").unwrap() + } + fn add_arg(&mut self, arg: &str) -> &mut Self { self.args.push(String::from(arg)); self } - fn run(&mut self) -> Assert { - self.cmd.args(&self.args).assert() + fn run_all(&self) -> Vec<Assert> { + vec![self.run_without_subcommand(), self.run_with_subcommand()] + } + + fn run_without_subcommand(&self) -> Assert { + let mut args = self.args.clone(); + args.remove(Self::SUBCOMMAND_ARGS_INDEX); + self.command().args(&args).assert() + } + + fn run_with_subcommand(&self) -> Assert { + self.command().args(&self.args).assert() } } @@ -35,8 +49,11 @@ fn simple_only_explicit_feature() { "test_crates/features_simple/old/Cargo.toml", ) .add_arg("--only-explicit-features") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); } #[test] @@ -46,8 +63,11 @@ fn simple_default_features() { "test_crates/features_simple/old/Cargo.toml", ) .add_arg("--default-features") - .run() - .failure(); + .run_all() + .into_iter() + .for_each(|a| { + a.failure(); + }); } #[test] @@ -59,8 +79,11 @@ fn simple_heuristic_features() { // make sure 'foo' is added to current .add_arg("--baseline-features") .add_arg("foo") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); } #[test] @@ -70,8 +93,11 @@ fn simple_all_features() { "test_crates/features_simple/old/Cargo.toml", ) .add_arg("--all-features") - .run() - .failure(); + .run_all() + .into_iter() + .for_each(|a| { + a.failure(); + }); } #[test] @@ -81,8 +107,11 @@ fn function_moved_only_explicit_features() { "test_crates/function_feature_changed/old/Cargo.toml", ) .add_arg("--only-explicit-features") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); CargoSemverChecks::new( "test_crates/function_feature_changed/new/", @@ -91,8 +120,11 @@ fn function_moved_only_explicit_features() { .add_arg("--only-explicit-features") .add_arg("--baseline-features") .add_arg("C") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); CargoSemverChecks::new( "test_crates/function_feature_changed/new/", @@ -103,8 +135,11 @@ fn function_moved_only_explicit_features() { .add_arg("A") .add_arg("--current-features") .add_arg("B") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); CargoSemverChecks::new( "test_crates/function_feature_changed/new/", @@ -113,8 +148,11 @@ fn function_moved_only_explicit_features() { .add_arg("--only-explicit-features") .add_arg("--features") .add_arg("B") - .run() - .failure(); + .run_all() + .into_iter() + .for_each(|a| { + a.failure(); + }); CargoSemverChecks::new( "test_crates/function_feature_changed/new/", @@ -127,8 +165,11 @@ fn function_moved_only_explicit_features() { .add_arg("B") .add_arg("--features") .add_arg("C") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); } #[test] @@ -138,8 +179,11 @@ fn function_moved_default_features() { "test_crates/function_feature_changed/old/Cargo.toml", ) .add_arg("--default-features") - .run() - .failure(); + .run_all() + .into_iter() + .for_each(|a| { + a.failure(); + }); CargoSemverChecks::new( "test_crates/function_feature_changed/new/", @@ -148,8 +192,11 @@ fn function_moved_default_features() { .add_arg("--default-features") .add_arg("--current-features") .add_arg("B") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); CargoSemverChecks::new( "test_crates/function_feature_changed/new/", @@ -158,8 +205,11 @@ fn function_moved_default_features() { .add_arg("--default-features") .add_arg("--features") .add_arg("B") - .run() - .failure(); + .run_all() + .into_iter() + .for_each(|a| { + a.failure(); + }); CargoSemverChecks::new( "test_crates/function_feature_changed/new/", @@ -170,8 +220,11 @@ fn function_moved_default_features() { .add_arg("B") .add_arg("--current-features") .add_arg("C") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); } #[test] @@ -180,8 +233,11 @@ fn function_moved_heuristic_features() { "test_crates/function_feature_changed/new/", "test_crates/function_feature_changed/old/Cargo.toml", ) - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); } #[test] @@ -191,8 +247,11 @@ fn function_moved_all_features() { "test_crates/function_feature_changed/old/Cargo.toml", ) .add_arg("--all-features") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); } #[test] @@ -202,8 +261,11 @@ fn default_features_when_default_undefined() { "test_crates/features_no_default/old/Cargo.toml", ) .add_arg("--default-features") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); CargoSemverChecks::new( "test_crates/features_no_default/new/", @@ -212,8 +274,11 @@ fn default_features_when_default_undefined() { .add_arg("--default-features") .add_arg("--features") .add_arg("A") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); CargoSemverChecks::new( "test_crates/features_no_default/new/", @@ -222,8 +287,11 @@ fn default_features_when_default_undefined() { .add_arg("--default-features") .add_arg("--baseline-features") .add_arg("A") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); CargoSemverChecks::new( "test_crates/features_no_default/new/", @@ -232,8 +300,11 @@ fn default_features_when_default_undefined() { .add_arg("--default-features") .add_arg("--current-features") .add_arg("B") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); CargoSemverChecks::new( "test_crates/features_no_default/new/", @@ -242,8 +313,11 @@ fn default_features_when_default_undefined() { .add_arg("--default-features") .add_arg("--features") .add_arg("B") - .run() - .failure(); + .run_all() + .into_iter() + .for_each(|a| { + a.failure(); + }); } #[test] @@ -254,8 +328,11 @@ fn feature_does_not_exist() { ) .add_arg("--features") .add_arg("new_feature") - .run() - .success(); + .run_all() + .into_iter() + .for_each(|a| { + a.success(); + }); CargoSemverChecks::new( "test_crates/function_feature_changed/new/", @@ -263,6 +340,9 @@ fn feature_does_not_exist() { ) .add_arg("--features") .add_arg("feature_to_be_removed") - .run() - .failure(); + .run_all() + .into_iter() + .for_each(|a| { + a.failure(); + }); }