From a51759c5e8caa1c2796549a3d696b2f9d80d2de5 Mon Sep 17 00:00:00 2001 From: Damian Date: Thu, 3 Jan 2019 23:00:45 +0100 Subject: [PATCH] Moved checks into compile_options(), changed tests --- src/bin/cargo/commands/bench.rs | 4 +- src/bin/cargo/commands/build.rs | 4 +- src/bin/cargo/commands/check.rs | 4 +- src/bin/cargo/commands/doc.rs | 2 +- src/bin/cargo/commands/fix.rs | 4 +- src/bin/cargo/commands/install.rs | 8 +- src/bin/cargo/commands/run.rs | 4 +- src/bin/cargo/commands/rustc.rs | 2 +- src/bin/cargo/commands/rustdoc.rs | 2 +- src/bin/cargo/commands/test.rs | 4 +- src/cargo/util/command_prelude.rs | 29 ++-- tests/testsuite/list_targets.rs | 242 ++++++++++++------------------ 12 files changed, 119 insertions(+), 190 deletions(-) diff --git a/src/bin/cargo/commands/bench.rs b/src/bin/cargo/commands/bench.rs index c9c79314c..aa44eb0d8 100644 --- a/src/bin/cargo/commands/bench.rs +++ b/src/bin/cargo/commands/bench.rs @@ -72,9 +72,7 @@ Compilation can be customized with the `bench` profile in the manifest. pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let ws = args.workspace(config)?; - let mut compile_opts = args.compile_options(config, CompileMode::Bench)?; - - args.check_optional_opts_all(&ws, &compile_opts)?; + let mut compile_opts = args.compile_options(config, CompileMode::Bench, Some(&ws))?; compile_opts.build_config.release = true; diff --git a/src/bin/cargo/commands/build.rs b/src/bin/cargo/commands/build.rs index bd659fdf9..3938a8e56 100644 --- a/src/bin/cargo/commands/build.rs +++ b/src/bin/cargo/commands/build.rs @@ -48,9 +48,7 @@ the --release flag will use the `release` profile instead. pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let ws = args.workspace(config)?; - let mut compile_opts = args.compile_options(config, CompileMode::Build)?; - - args.check_optional_opts_all(&ws, &compile_opts)?; + let mut compile_opts = args.compile_options(config, CompileMode::Build, Some(&ws))?; compile_opts.export_dir = args.value_of_path("out-dir", config); if compile_opts.export_dir.is_some() && !config.cli_unstable().unstable_options { diff --git a/src/bin/cargo/commands/check.rs b/src/bin/cargo/commands/check.rs index 389b5a51f..b3dfeb15d 100644 --- a/src/bin/cargo/commands/check.rs +++ b/src/bin/cargo/commands/check.rs @@ -68,9 +68,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { } }; let mode = CompileMode::Check { test }; - let compile_opts = args.compile_options(config, mode)?; - - args.check_optional_opts_all(&ws, &compile_opts)?; + let compile_opts = args.compile_options(config, mode, Some(&ws))?; ops::compile(&ws, &compile_opts)?; Ok(()) diff --git a/src/bin/cargo/commands/doc.rs b/src/bin/cargo/commands/doc.rs index b32c793ff..abe575494 100644 --- a/src/bin/cargo/commands/doc.rs +++ b/src/bin/cargo/commands/doc.rs @@ -50,7 +50,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let mode = CompileMode::Doc { deps: !args.is_present("no-deps"), }; - let mut compile_opts = args.compile_options(config, mode)?; + let mut compile_opts = args.compile_options(config, mode, Some(&ws))?; compile_opts.local_rustdoc_args = if args.is_present("document-private-items") { Some(vec!["--document-private-items".to_string()]) } else { diff --git a/src/bin/cargo/commands/fix.rs b/src/bin/cargo/commands/fix.rs index 93e04c1c3..bc6542974 100644 --- a/src/bin/cargo/commands/fix.rs +++ b/src/bin/cargo/commands/fix.rs @@ -122,9 +122,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { // Unlike other commands default `cargo fix` to all targets to fix as much // code as we can. - let mut opts = args.compile_options(config, mode)?; - - args.check_optional_opts_all(&ws, &opts)?; + let mut opts = args.compile_options(config, mode, Some(&ws))?; if let CompileFilter::Default { .. } = opts.filter { opts.filter = CompileFilter::Only { diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 27859744a..8702fd99a 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -78,11 +78,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let registry = args.registry(config)?; config.reload_rooted_at_cargo_home()?; - let mut compile_opts = args.compile_options(config, CompileMode::Build)?; - - if let Ok(ws) = args.workspace(config) { - args.check_optional_opts_example_and_bin(&ws, &compile_opts)?; - } + + let workspace = args.workspace(config).ok(); + let mut compile_opts = args.compile_options(config, CompileMode::Build, workspace.as_ref())?; compile_opts.build_config.release = !args.is_present("debug"); diff --git a/src/bin/cargo/commands/run.rs b/src/bin/cargo/commands/run.rs index 96fc66ae4..fc8401d56 100644 --- a/src/bin/cargo/commands/run.rs +++ b/src/bin/cargo/commands/run.rs @@ -39,9 +39,7 @@ run. If you're passing arguments to both Cargo and the binary, the ones after pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let ws = args.workspace(config)?; - let mut compile_opts = args.compile_options(config, CompileMode::Build)?; - - args.check_optional_opts_example_and_bin(&ws, &compile_opts)?; + let mut compile_opts = args.compile_options(config, CompileMode::Build, Some(&ws))?; if !args.is_present("example") && !args.is_present("bin") { let default_runs: Vec<_> = compile_opts diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index cd959cc55..b0400b556 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -62,7 +62,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { return Err(CliError::new(err, 101)); } }; - let mut compile_opts = args.compile_options_for_single_package(config, mode)?; + let mut compile_opts = args.compile_options_for_single_package(config, mode, Some(&ws))?; let target_args = values(args, "args"); compile_opts.target_rustc_args = if target_args.is_empty() { None diff --git a/src/bin/cargo/commands/rustdoc.rs b/src/bin/cargo/commands/rustdoc.rs index 14bde6fe2..f016d2863 100644 --- a/src/bin/cargo/commands/rustdoc.rs +++ b/src/bin/cargo/commands/rustdoc.rs @@ -51,7 +51,7 @@ the `cargo help pkgid` command. pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let ws = args.workspace(config)?; let mut compile_opts = - args.compile_options_for_single_package(config, CompileMode::Doc { deps: false })?; + args.compile_options_for_single_package(config, CompileMode::Doc { deps: false }, Some(&ws))?; let target_args = values(args, "args"); compile_opts.target_rustdoc_args = if target_args.is_empty() { None diff --git a/src/bin/cargo/commands/test.rs b/src/bin/cargo/commands/test.rs index 187af63d1..63ac24c53 100644 --- a/src/bin/cargo/commands/test.rs +++ b/src/bin/cargo/commands/test.rs @@ -92,9 +92,7 @@ To get the list of all options available for the test binaries use this: pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let ws = args.workspace(config)?; - let mut compile_opts = args.compile_options(config, CompileMode::Test)?; - - args.check_optional_opts_all(&ws, &compile_opts)?; + let mut compile_opts = args.compile_options(config, CompileMode::Test, Some(&ws))?; let doc = args.is_present("doc"); if doc { diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 49690da05..1ad216142 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -288,6 +288,7 @@ pub trait ArgMatchesExt { &self, config: &'a Config, mode: CompileMode, + workspace: Option<&Workspace<'a>>, ) -> CargoResult> { let spec = Packages::from_flags( self._is_present("all"), @@ -344,6 +345,11 @@ pub trait ArgMatchesExt { local_rustdoc_args: None, export_dir: None, }; + + if let Some(ws) = workspace { + self.check_optional_opts(ws, &opts)?; + } + Ok(opts) } @@ -351,8 +357,9 @@ pub trait ArgMatchesExt { &self, config: &'a Config, mode: CompileMode, + workspace: Option<&Workspace<'a>>, ) -> CargoResult> { - let mut compile_opts = self.compile_options(config, mode)?; + let mut compile_opts = self.compile_options(config, mode, workspace)?; compile_opts.spec = Packages::Packages(self._values_of("package")); Ok(compile_opts) } @@ -429,27 +436,11 @@ about this warning."; Ok(index) } - fn check_optional_opts_example_and_bin( + fn check_optional_opts( &self, workspace: &Workspace<'_>, compile_opts: &CompileOptions<'_>, - ) -> CliResult { - if self.is_present_with_zero_values("example") { - print_available_examples(&workspace, &compile_opts)?; - } - - if self.is_present_with_zero_values("bin") { - print_available_binaries(&workspace, &compile_opts)?; - } - - Ok(()) - } - - fn check_optional_opts_all( - &self, - workspace: &Workspace<'_>, - compile_opts: &CompileOptions<'_>, - ) -> CliResult { + ) -> CargoResult<()> { if self.is_present_with_zero_values("example") { print_available_examples(&workspace, &compile_opts)?; } diff --git a/tests/testsuite/list_targets.rs b/tests/testsuite/list_targets.rs index badd464a4..0166bc130 100644 --- a/tests/testsuite/list_targets.rs +++ b/tests/testsuite/list_targets.rs @@ -1,69 +1,12 @@ use crate::support::project; -// cargo {run,install} only support --example and --bin -// cargo {build,check,fix,test} support --example, --bin, --bench and --test +const EXAMPLE: u8 = 0x1; +const BIN: u8 = 0x2; +const TEST: u8 = 0x4; +const BENCH: u8 = 0x8; -fn test_list_targets_example_and_bin_only(command: &str) { - let p = project() - .file("examples/a.rs", "fn main() { }") - .file("examples/b.rs", "fn main() { }") - .file("src/main.rs", "fn main() { }") - .build(); - - p.cargo(&format!("{} --example", command)) - .with_stderr( - "\ -error: \"--example\" takes one argument. -Available examples: - a - b - -", - ) - .with_status(101) - .run(); - - p.cargo(&format!("{} --bin", command)) - .with_stderr( - "\ -error: \"--bin\" takes one argument. -Available binaries: - foo - -", - ) - .with_status(101) - .run(); -} - -fn test_empty_list_targets_example_and_bin_only(command: &str) { - let p = project().file("src/lib.rs", "").build(); - - p.cargo(&format!("{} --example", command)) - .with_stderr( - "\ -error: \"--example\" takes one argument. -No examples available. - -", - ) - .with_status(101) - .run(); - - p.cargo(&format!("{} --bin", command)) - .with_stderr( - "\ -error: \"--bin\" takes one argument. -No binaries available. - -", - ) - .with_status(101) - .run(); -} - -fn test_list_targets_full(command: &str) { - let p = project() +fn list_targets_test(command: &str, targets: u8) { + let full_project = project() .file("examples/a.rs", "fn main() { }") .file("examples/b.rs", "fn main() { }") .file("benches/bench1.rs", "") @@ -73,165 +16,174 @@ fn test_list_targets_full(command: &str) { .file("src/main.rs", "fn main() { }") .build(); - p.cargo(&format!("{} --example", command)) - .with_stderr( - "\ + if targets & EXAMPLE != 0 { + full_project + .cargo(&format!("{} --example", command)) + .with_stderr( + "\ error: \"--example\" takes one argument. Available examples: a b ", - ) - .with_status(101) - .run(); + ) + .with_status(101) + .run(); + } - p.cargo(&format!("{} --bin", command)) - .with_stderr( - "\ + if targets & BIN != 0 { + full_project + .cargo(&format!("{} --bin", command)) + .with_stderr( + "\ error: \"--bin\" takes one argument. Available binaries: foo ", - ) - .with_status(101) - .run(); + ) + .with_status(101) + .run(); + } - p.cargo(&format!("{} --bench", command)) - .with_stderr( - "\ + if targets & BENCH != 0 { + full_project + .cargo(&format!("{} --bench", command)) + .with_stderr( + "\ error: \"--bench\" takes one argument. Available benches: bench1 bench2 ", - ) - .with_status(101) - .run(); + ) + .with_status(101) + .run(); + } - p.cargo(&format!("{} --test", command)) - .with_stderr( - "\ + if targets & TEST != 0 { + full_project + .cargo(&format!("{} --test", command)) + .with_stderr( + "\ error: \"--test\" takes one argument. Available tests: test1 test2 ", - ) - .with_status(101) - .run(); -} + ) + .with_status(101) + .run(); + } -fn test_empty_list_targets_full(command: &str) { - let p = project().file("src/lib.rs", "").build(); + let empty_project = project().file("src/lib.rs", "").build(); - p.cargo(&format!("{} --example", command)) - .with_stderr( - "\ + if targets & EXAMPLE != 0 { + empty_project + .cargo(&format!("{} --example", command)) + .with_stderr( + "\ error: \"--example\" takes one argument. No examples available. ", - ) - .with_status(101) - .run(); + ) + .with_status(101) + .run(); + } - p.cargo(&format!("{} --bin", command)) - .with_stderr( - "\ + if targets & BIN != 0 { + empty_project + .cargo(&format!("{} --bin", command)) + .with_stderr( + "\ error: \"--bin\" takes one argument. No binaries available. ", - ) - .with_status(101) - .run(); + ) + .with_status(101) + .run(); + } - p.cargo(&format!("{} --bench", command)) - .with_stderr( - "\ + if targets & BENCH != 0 { + empty_project + .cargo(&format!("{} --bench", command)) + .with_stderr( + "\ error: \"--bench\" takes one argument. No benches available. ", - ) - .with_status(101) - .run(); + ) + .with_status(101) + .run(); + } - p.cargo(&format!("{} --test", command)) - .with_stderr( - "\ + if targets & TEST != 0 { + empty_project + .cargo(&format!("{} --test", command)) + .with_stderr( + "\ error: \"--test\" takes one argument. No tests available. ", - ) - .with_status(101) - .run(); + ) + .with_status(101) + .run(); + } } #[test] fn build_list_targets() { - test_list_targets_full("build"); -} -#[test] -fn build_list_targets_empty() { - test_empty_list_targets_full("build"); + list_targets_test("build", EXAMPLE | BIN | TEST | BENCH); } #[test] fn check_list_targets() { - test_list_targets_full("check"); + list_targets_test("check", EXAMPLE | BIN | TEST | BENCH); } + #[test] -fn check_list_targets_empty() { - test_empty_list_targets_full("check"); +fn doc_list_targets() { + list_targets_test("doc", BIN); } #[test] fn fix_list_targets() { - test_list_targets_full("fix"); -} -#[test] -fn fix_list_targets_empty() { - test_empty_list_targets_full("fix"); + list_targets_test("fix", EXAMPLE | BIN | TEST | BENCH); } #[test] fn run_list_targets() { - test_list_targets_example_and_bin_only("run"); -} -#[test] -fn run_list_targets_empty() { - test_empty_list_targets_example_and_bin_only("run"); + list_targets_test("run", EXAMPLE | BIN); } #[test] fn test_list_targets() { - test_list_targets_full("test"); -} -#[test] -fn test_list_targets_empty() { - test_empty_list_targets_full("test"); + list_targets_test("test", EXAMPLE | BIN | TEST | BENCH); } #[test] fn bench_list_targets() { - test_list_targets_full("bench"); -} -#[test] -fn bench_list_targets_empty() { - test_empty_list_targets_full("bench"); + list_targets_test("bench", EXAMPLE | BIN | TEST | BENCH); } #[test] fn install_list_targets() { - test_list_targets_example_and_bin_only("install"); + list_targets_test("install", EXAMPLE | BIN); } + #[test] -fn install_list_targets_empty() { - test_empty_list_targets_example_and_bin_only("install"); +fn rustdoc_list_targets() { + list_targets_test("rustdoc", EXAMPLE | BIN | TEST | BENCH); +} + +#[test] +fn rustc_list_targets() { + list_targets_test("rustc", EXAMPLE | BIN | TEST | BENCH); }