From da1749db0ae1fe83775dcbdbe60dea1e14340367 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 26 Oct 2020 02:40:28 +0800 Subject: [PATCH 01/12] feat(util): print_available_packages --- src/cargo/util/mod.rs | 2 +- src/cargo/util/workspace.rs | 40 ++++++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/cargo/util/mod.rs b/src/cargo/util/mod.rs index 8bed0b91633..11ac2bae679 100644 --- a/src/cargo/util/mod.rs +++ b/src/cargo/util/mod.rs @@ -28,7 +28,7 @@ pub use self::to_semver::ToSemver; pub use self::vcs::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo}; pub use self::workspace::{ print_available_benches, print_available_binaries, print_available_examples, - print_available_tests, + print_available_packages, print_available_tests, }; mod canonical_url; diff --git a/src/cargo/util/workspace.rs b/src/cargo/util/workspace.rs index fbfe11d8433..7da998938bd 100644 --- a/src/cargo/util/workspace.rs +++ b/src/cargo/util/workspace.rs @@ -8,7 +8,7 @@ fn get_available_targets<'a>( filter_fn: fn(&Target) -> bool, ws: &'a Workspace<'_>, options: &'a CompileOptions, -) -> CargoResult> { +) -> CargoResult> { let packages = options.spec.get_packages(ws)?; let mut targets: Vec<_> = packages @@ -19,6 +19,7 @@ fn get_available_targets<'a>( .iter() .filter(|target| filter_fn(target)) }) + .map(Target::name) .collect(); targets.sort(); @@ -26,7 +27,7 @@ fn get_available_targets<'a>( Ok(targets) } -fn print_available( +fn print_available_targets( filter_fn: fn(&Target) -> bool, ws: &Workspace<'_>, options: &CompileOptions, @@ -38,29 +39,50 @@ fn print_available( let mut output = String::new(); writeln!(output, "\"{}\" takes one argument.", option_name)?; - if targets.is_empty() { + print_availables(output, &targets, plural_name) +} + +fn print_availables(output: String, availables: &[&str], plural_name: &str) -> CargoResult<()> { + let mut output = output; + if availables.is_empty() { writeln!(output, "No {} available.", plural_name)?; } else { writeln!(output, "Available {}:", plural_name)?; - for target in targets { - writeln!(output, " {}", target.name())?; + for available in availables { + writeln!(output, " {}", available)?; } } bail!("{}", output) } +pub fn print_available_packages(ws: &Workspace<'_>) -> CargoResult<()> { + let packages = ws + .members() + .map(|pkg| pkg.name().as_str()) + .collect::>(); + + let mut output = String::new(); + writeln!( + output, + "\"--package \" requires a SPEC format value.\n\ + Run `cargo help pkgid` for more infomation about SPEC format." + )?; + + print_availables(output, &packages, "packages") +} + pub fn print_available_examples(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> { - print_available(Target::is_example, ws, options, "--example", "examples") + print_available_targets(Target::is_example, ws, options, "--example", "examples") } pub fn print_available_binaries(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> { - print_available(Target::is_bin, ws, options, "--bin", "binaries") + print_available_targets(Target::is_bin, ws, options, "--bin", "binaries") } pub fn print_available_benches(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> { - print_available(Target::is_bench, ws, options, "--bench", "benches") + print_available_targets(Target::is_bench, ws, options, "--bench", "benches") } pub fn print_available_tests(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> { - print_available(Target::is_test, ws, options, "--test", "tests") + print_available_targets(Target::is_test, ws, options, "--test", "tests") } From 60751e4840dc4f52ed369ecfe0b585ba4e5343b5 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 26 Oct 2020 02:41:28 +0800 Subject: [PATCH 02/12] fix: check `--package`'s value manually --- src/cargo/util/command_prelude.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 6800e00118b..fd6063e7d09 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -8,7 +8,7 @@ use crate::util::restricted_names::is_glob_pattern; use crate::util::{paths, toml::TomlProfile, validate_package_name}; use crate::util::{ print_available_benches, print_available_binaries, print_available_examples, - print_available_tests, + print_available_packages, print_available_tests, }; use crate::CargoResult; use anyhow::bail; @@ -52,7 +52,7 @@ pub trait AppExt: Sized { } fn arg_package_spec_simple(self, package: &'static str) -> Self { - self._arg(multi_opt("package", "SPEC", package).short("p")) + self._arg(optional_multi_opt("package", "SPEC", package).short("p")) } fn arg_package(self, package: &'static str) -> Self { @@ -498,6 +498,13 @@ pub trait ArgMatchesExt { if let Some(ws) = workspace { self.check_optional_opts(ws, &opts)?; + } else if self.is_present_with_zero_values("package") { + // As for cargo 0.50.0, this won't occur but if someone sneaks in + // we can still provide this informative message for them. + anyhow::bail!( + "\"--package \" requires a SPEC format value.\n\ + Run `cargo help pkgid` for more infomation about SPEC format." + ) } Ok(opts) @@ -588,6 +595,10 @@ about this warning."; workspace: &Workspace<'_>, compile_opts: &CompileOptions, ) -> CargoResult<()> { + if self.is_present_with_zero_values("package") { + print_available_packages(workspace)? + } + if self.is_present_with_zero_values("example") { print_available_examples(workspace, compile_opts)?; } From 4d5a56d7d07b7cfc4a3b24c6444473004b7c9792 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 26 Oct 2020 02:46:26 +0800 Subject: [PATCH 03/12] test: suggest available packages --- .../{list_targets.rs => list_availables.rs} | 70 ++++++++++++------- tests/testsuite/main.rs | 2 +- 2 files changed, 45 insertions(+), 27 deletions(-) rename tests/testsuite/{list_targets.rs => list_availables.rs} (64%) diff --git a/tests/testsuite/list_targets.rs b/tests/testsuite/list_availables.rs similarity index 64% rename from tests/testsuite/list_targets.rs rename to tests/testsuite/list_availables.rs index 09506f5dbce..7b7bac57ad4 100644 --- a/tests/testsuite/list_targets.rs +++ b/tests/testsuite/list_availables.rs @@ -1,13 +1,15 @@ -//! Tests for target filter flags giving suggestions on which targets are available. +//! Tests for packages/target filter flags giving suggestions on which +//! packages/targets are available. use cargo_test_support::project; -const EXAMPLE: u8 = 0x1; -const BIN: u8 = 0x2; -const TEST: u8 = 0x4; -const BENCH: u8 = 0x8; +const EXAMPLE: u8 = 1 << 0; +const BIN: u8 = 1 << 1; +const TEST: u8 = 1 << 2; +const BENCH: u8 = 1 << 3; +const PACKAGE: u8 = 1 << 4; -fn list_targets_test(command: &str, targets: u8) { +fn list_availables_test(command: &str, targets: u8) { let full_project = project() .file("examples/a.rs", "fn main() { }") .file("examples/b.rs", "fn main() { }") @@ -75,6 +77,22 @@ Available tests: test1 test2 +", + ) + .with_status(101) + .run(); + } + + if targets & PACKAGE != 0 { + full_project + .cargo(&format!("{} -p", command)) + .with_stderr( + "\ +[ERROR] \"--package \" requires a SPEC format value. +Run `cargo help pkgid` for more infomation about SPEC format. +Available packages: + foo + ", ) .with_status(101) @@ -141,51 +159,51 @@ No tests available. } #[cargo_test] -fn build_list_targets() { - list_targets_test("build", EXAMPLE | BIN | TEST | BENCH); +fn build_list_availables() { + list_availables_test("build", EXAMPLE | BIN | TEST | BENCH | PACKAGE); } #[cargo_test] -fn check_list_targets() { - list_targets_test("check", EXAMPLE | BIN | TEST | BENCH); +fn check_list_availables() { + list_availables_test("check", EXAMPLE | BIN | TEST | BENCH | PACKAGE); } #[cargo_test] -fn doc_list_targets() { - list_targets_test("doc", BIN); +fn doc_list_availables() { + list_availables_test("doc", BIN | PACKAGE); } #[cargo_test] -fn fix_list_targets() { - list_targets_test("fix", EXAMPLE | BIN | TEST | BENCH); +fn fix_list_availables() { + list_availables_test("fix", EXAMPLE | BIN | TEST | BENCH | PACKAGE); } #[cargo_test] -fn run_list_targets() { - list_targets_test("run", EXAMPLE | BIN); +fn run_list_availables() { + list_availables_test("run", EXAMPLE | BIN); } #[cargo_test] -fn test_list_targets() { - list_targets_test("test", EXAMPLE | BIN | TEST | BENCH); +fn test_list_availables() { + list_availables_test("test", EXAMPLE | BIN | TEST | BENCH | PACKAGE); } #[cargo_test] -fn bench_list_targets() { - list_targets_test("bench", EXAMPLE | BIN | TEST | BENCH); +fn bench_list_availables() { + list_availables_test("bench", EXAMPLE | BIN | TEST | BENCH | PACKAGE); } #[cargo_test] -fn install_list_targets() { - list_targets_test("install", EXAMPLE | BIN); +fn install_list_availables() { + list_availables_test("install", EXAMPLE | BIN); } #[cargo_test] -fn rustdoc_list_targets() { - list_targets_test("rustdoc", EXAMPLE | BIN | TEST | BENCH); +fn rustdoc_list_availables() { + list_availables_test("rustdoc", EXAMPLE | BIN | TEST | BENCH); } #[cargo_test] -fn rustc_list_targets() { - list_targets_test("rustc", EXAMPLE | BIN | TEST | BENCH); +fn rustc_list_availables() { + list_availables_test("rustc", EXAMPLE | BIN | TEST | BENCH); } diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index 2b1767d41e9..6a32d3b13d6 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -58,7 +58,7 @@ mod init; mod install; mod install_upgrade; mod jobserver; -mod list_targets; +mod list_availables; mod local_registry; mod locate_project; mod lockfile_compat; From fa522ccec1081ecde6c5b78960f3e35c2b439496 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Tue, 27 Oct 2020 22:03:54 +0800 Subject: [PATCH 04/12] fix: more information when -p value is absent --- src/cargo/util/command_prelude.rs | 5 +++-- src/cargo/util/workspace.rs | 34 +++++++++++++++++------------- tests/testsuite/list_availables.rs | 8 ++++--- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index fd6063e7d09..e4880f0a2a8 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -502,8 +502,9 @@ pub trait ArgMatchesExt { // As for cargo 0.50.0, this won't occur but if someone sneaks in // we can still provide this informative message for them. anyhow::bail!( - "\"--package \" requires a SPEC format value.\n\ - Run `cargo help pkgid` for more infomation about SPEC format." + "\"--package \" requires a SPEC format value, \n\ + which can be any package ID specifier in the dependency graph.\n\ + Run `cargo help pkgid` for more information about SPEC format." ) } diff --git a/src/cargo/util/workspace.rs b/src/cargo/util/workspace.rs index 7da998938bd..3f342994ecc 100644 --- a/src/cargo/util/workspace.rs +++ b/src/cargo/util/workspace.rs @@ -39,17 +39,12 @@ fn print_available_targets( let mut output = String::new(); writeln!(output, "\"{}\" takes one argument.", option_name)?; - print_availables(output, &targets, plural_name) -} - -fn print_availables(output: String, availables: &[&str], plural_name: &str) -> CargoResult<()> { - let mut output = output; - if availables.is_empty() { + if targets.is_empty() { writeln!(output, "No {} available.", plural_name)?; } else { writeln!(output, "Available {}:", plural_name)?; - for available in availables { - writeln!(output, " {}", available)?; + for target in targets { + writeln!(output, " {}", target)?; } } bail!("{}", output) @@ -61,16 +56,25 @@ pub fn print_available_packages(ws: &Workspace<'_>) -> CargoResult<()> { .map(|pkg| pkg.name().as_str()) .collect::>(); - let mut output = String::new(); - writeln!( - output, - "\"--package \" requires a SPEC format value.\n\ - Run `cargo help pkgid` for more infomation about SPEC format." - )?; + let mut output = "\"--package \" requires a SPEC format value, \ + which can be any package ID specifier in the dependency graph.\n\ + Run `cargo help pkgid` for more information about SPEC format.\n\n" + .to_string(); - print_availables(output, &packages, "packages") + if packages.is_empty() { + // This would never happen. + // Just in case something regresses we covers it here. + writeln!(output, "No packages available.")?; + } else { + writeln!(output, "Possible packages/workspace members:")?; + for package in packages { + writeln!(output, " {}", package)?; + } + } + bail!("{}", output) } + pub fn print_available_examples(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> { print_available_targets(Target::is_example, ws, options, "--example", "examples") } diff --git a/tests/testsuite/list_availables.rs b/tests/testsuite/list_availables.rs index 7b7bac57ad4..2297b9a3690 100644 --- a/tests/testsuite/list_availables.rs +++ b/tests/testsuite/list_availables.rs @@ -88,9 +88,11 @@ Available tests: .cargo(&format!("{} -p", command)) .with_stderr( "\ -[ERROR] \"--package \" requires a SPEC format value. -Run `cargo help pkgid` for more infomation about SPEC format. -Available packages: +[ERROR] \"--package \" requires a SPEC format value, \ +which can be any package ID specifier in the dependency graph. +Run `cargo help pkgid` for more information about SPEC format. + +Possible packages/workspace members: foo ", From 139247e4e63c454e3f14d7aecb86779458422f95 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Wed, 28 Oct 2020 00:30:26 +0800 Subject: [PATCH 05/12] feat: list availables for rustc,rustdoc,run,pkgid --- src/bin/cargo/commands/pkgid.rs | 8 +++++++- src/cargo/util/command_prelude.rs | 8 ++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/bin/cargo/commands/pkgid.rs b/src/bin/cargo/commands/pkgid.rs index 098031dcd29..19a17610f67 100644 --- a/src/bin/cargo/commands/pkgid.rs +++ b/src/bin/cargo/commands/pkgid.rs @@ -1,6 +1,7 @@ use crate::command_prelude::*; use cargo::ops; +use cargo::util::print_available_packages; pub fn cli() -> App { subcommand("pkgid") @@ -14,7 +15,12 @@ pub fn cli() -> App { pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let ws = args.workspace(config)?; - let spec = args.value_of("spec").or_else(|| args.value_of("package")); + if !args.is_present("spec") && args.is_present_with_zero_values("package") { + print_available_packages(&ws)? + } + let spec = args.value_of("spec").or_else(|| { + args.value_of("package") + }); let spec = ops::pkgid(&ws, spec)?; cargo::drop_println!(config, "{}", spec); Ok(()) diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index e4880f0a2a8..348aa94a0d2 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -56,7 +56,7 @@ pub trait AppExt: Sized { } fn arg_package(self, package: &'static str) -> Self { - self._arg(opt("package", package).short("p").value_name("SPEC")) + self._arg(optinal_opt("package", package).short("p").value_name("SPEC")) } fn arg_jobs(self) -> Self { @@ -220,6 +220,10 @@ pub fn opt(name: &'static str, help: &'static str) -> Arg<'static, 'static> { Arg::with_name(name).long(name).help(help) } +pub fn optinal_opt(name: &'static str, help: &'static str) -> Arg<'static, 'static> { + opt(name, help).min_values(0) +} + pub fn optional_multi_opt( name: &'static str, value_name: &'static str, @@ -502,7 +506,7 @@ pub trait ArgMatchesExt { // As for cargo 0.50.0, this won't occur but if someone sneaks in // we can still provide this informative message for them. anyhow::bail!( - "\"--package \" requires a SPEC format value, \n\ + "\"--package \" requires a SPEC format value, \ which can be any package ID specifier in the dependency graph.\n\ Run `cargo help pkgid` for more information about SPEC format." ) From d91be5a36fec992c9f64c85d2fe8c313444a5d05 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Wed, 28 Oct 2020 00:26:19 +0800 Subject: [PATCH 06/12] test: list availables for rustc,rustdoc,run,pkgid --- tests/testsuite/list_availables.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/testsuite/list_availables.rs b/tests/testsuite/list_availables.rs index 2297b9a3690..b1130c29d45 100644 --- a/tests/testsuite/list_availables.rs +++ b/tests/testsuite/list_availables.rs @@ -18,6 +18,7 @@ fn list_availables_test(command: &str, targets: u8) { .file("tests/test1.rs", "") .file("tests/test2.rs", "") .file("src/main.rs", "fn main() { }") + .file("Cargo.lock", "") // for `cargo pkgid` .build(); if targets & EXAMPLE != 0 { @@ -182,7 +183,7 @@ fn fix_list_availables() { #[cargo_test] fn run_list_availables() { - list_availables_test("run", EXAMPLE | BIN); + list_availables_test("run", EXAMPLE | BIN | PACKAGE); } #[cargo_test] @@ -202,10 +203,15 @@ fn install_list_availables() { #[cargo_test] fn rustdoc_list_availables() { - list_availables_test("rustdoc", EXAMPLE | BIN | TEST | BENCH); + list_availables_test("rustdoc", EXAMPLE | BIN | TEST | BENCH | PACKAGE); } #[cargo_test] fn rustc_list_availables() { - list_availables_test("rustc", EXAMPLE | BIN | TEST | BENCH); + list_availables_test("rustc", EXAMPLE | BIN | TEST | BENCH | PACKAGE); +} + +#[cargo_test] +fn pkgid_list_availables() { + list_availables_test("pkgid", PACKAGE); } From 73fb1a2db647142fce2dbcbfe27fef75a29a3949 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Wed, 28 Oct 2020 02:43:25 +0800 Subject: [PATCH 07/12] feat: list avaiables for clean,tree,update --- src/bin/cargo/commands/clean.rs | 6 ++++++ src/bin/cargo/commands/tree.rs | 6 ++++++ src/bin/cargo/commands/update.rs | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/src/bin/cargo/commands/clean.rs b/src/bin/cargo/commands/clean.rs index 80cd2a98f15..b02af1bb4a9 100644 --- a/src/bin/cargo/commands/clean.rs +++ b/src/bin/cargo/commands/clean.rs @@ -1,6 +1,7 @@ use crate::command_prelude::*; use cargo::ops::{self, CleanOptions}; +use cargo::util::print_available_packages; pub fn cli() -> App { subcommand("clean") @@ -18,6 +19,11 @@ pub fn cli() -> App { pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let ws = args.workspace(config)?; + + if args.is_present_with_zero_values("package") { + print_available_packages(&ws)?; + } + let opts = CleanOptions { config, spec: values(args, "package"), diff --git a/src/bin/cargo/commands/tree.rs b/src/bin/cargo/commands/tree.rs index b700be93952..10e89cd5ebd 100644 --- a/src/bin/cargo/commands/tree.rs +++ b/src/bin/cargo/commands/tree.rs @@ -4,6 +4,7 @@ use anyhow::{bail, format_err}; use cargo::core::dependency::DepKind; use cargo::ops::tree::{self, EdgeKind}; use cargo::ops::Packages; +use cargo::util::print_available_packages; use cargo::util::CargoResult; use std::collections::HashSet; use std::str::FromStr; @@ -176,6 +177,11 @@ subtree of the package given to -p.\n\ } let ws = args.workspace(config)?; + + if args.is_present_with_zero_values("package") { + print_available_packages(&ws)?; + } + let charset = tree::Charset::from_str(args.value_of("charset").unwrap()) .map_err(|e| anyhow::anyhow!("{}", e))?; let opts = tree::TreeOptions { diff --git a/src/bin/cargo/commands/update.rs b/src/bin/cargo/commands/update.rs index a887f908f7b..34bab9f757e 100644 --- a/src/bin/cargo/commands/update.rs +++ b/src/bin/cargo/commands/update.rs @@ -1,6 +1,7 @@ use crate::command_prelude::*; use cargo::ops::{self, UpdateOptions}; +use cargo::util::print_available_packages; pub fn cli() -> App { subcommand("update") @@ -20,6 +21,10 @@ pub fn cli() -> App { pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let ws = args.workspace(config)?; + if args.is_present_with_zero_values("package") { + print_available_packages(&ws)?; + } + let update_opts = UpdateOptions { aggressive: args.is_present("aggressive"), precise: args.value_of("precise"), From c34774561335559e1e5f0d82d898132e87c45b67 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Wed, 28 Oct 2020 01:33:34 +0800 Subject: [PATCH 08/12] test: list availables for clean,tree,update --- tests/testsuite/list_availables.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/testsuite/list_availables.rs b/tests/testsuite/list_availables.rs index b1130c29d45..6bbbeb160d5 100644 --- a/tests/testsuite/list_availables.rs +++ b/tests/testsuite/list_availables.rs @@ -215,3 +215,18 @@ fn rustc_list_availables() { fn pkgid_list_availables() { list_availables_test("pkgid", PACKAGE); } + +#[cargo_test] +fn tree_list_availables() { + list_availables_test("tree", PACKAGE); +} + +#[cargo_test] +fn clean_list_availables() { + list_availables_test("clean", PACKAGE); +} + +#[cargo_test] +fn update_list_availables() { + list_availables_test("update", PACKAGE); +} From cc6b43753be80c634775bcee4c6c5bb0ea1b10ac Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Wed, 28 Oct 2020 02:44:59 +0800 Subject: [PATCH 09/12] feat: check cargo uninstall package opt with empty value --- src/bin/cargo/commands/uninstall.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/bin/cargo/commands/uninstall.rs b/src/bin/cargo/commands/uninstall.rs index 5d989e1c997..45562c7fffd 100644 --- a/src/bin/cargo/commands/uninstall.rs +++ b/src/bin/cargo/commands/uninstall.rs @@ -15,6 +15,14 @@ pub fn cli() -> App { pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let root = args.value_of("root"); + + if args.is_present_with_zero_values("package") { + return Err(anyhow::anyhow!( + "\"--package \" requires a SPEC format value.\n\ + Run `cargo help pkgid` for more information about SPEC format." + ).into()) + } + let specs = args .values_of("spec") .unwrap_or_else(|| args.values_of("package").unwrap_or_default()) From 9d9c321c27f9c1e9c619aaa0a2121df236ec92d2 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Wed, 28 Oct 2020 02:44:23 +0800 Subject: [PATCH 10/12] test: check cargo uninstall package opt with empty value --- tests/testsuite/install.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index 014c7ed008a..30bfefec0a0 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -1181,6 +1181,18 @@ fn uninstall_multiple_and_specifying_bin() { .run(); } +#[cargo_test] +fn uninstall_with_empty_pakcage_option() { + cargo_process("uninstall -p") + .with_status(101) + .with_stderr("\ +[ERROR] \"--package \" requires a SPEC format value. +Run `cargo help pkgid` for more information about SPEC format. +", + ) + .run(); +} + #[cargo_test] fn uninstall_multiple_and_some_pkg_does_not_exist() { pkg("foo", "0.0.1"); From b9554f37abe4f802dd615a550773105a8b2eb386 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Wed, 28 Oct 2020 02:57:06 +0800 Subject: [PATCH 11/12] rustfmt --- src/bin/cargo/commands/pkgid.rs | 4 +--- src/bin/cargo/commands/uninstall.rs | 3 ++- src/cargo/util/command_prelude.rs | 6 +++++- src/cargo/util/workspace.rs | 3 +-- tests/testsuite/install.rs | 3 ++- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/bin/cargo/commands/pkgid.rs b/src/bin/cargo/commands/pkgid.rs index 19a17610f67..d81b7d9f619 100644 --- a/src/bin/cargo/commands/pkgid.rs +++ b/src/bin/cargo/commands/pkgid.rs @@ -18,9 +18,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { if !args.is_present("spec") && args.is_present_with_zero_values("package") { print_available_packages(&ws)? } - let spec = args.value_of("spec").or_else(|| { - args.value_of("package") - }); + let spec = args.value_of("spec").or_else(|| args.value_of("package")); let spec = ops::pkgid(&ws, spec)?; cargo::drop_println!(config, "{}", spec); Ok(()) diff --git a/src/bin/cargo/commands/uninstall.rs b/src/bin/cargo/commands/uninstall.rs index 45562c7fffd..9577becbbdc 100644 --- a/src/bin/cargo/commands/uninstall.rs +++ b/src/bin/cargo/commands/uninstall.rs @@ -20,7 +20,8 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { return Err(anyhow::anyhow!( "\"--package \" requires a SPEC format value.\n\ Run `cargo help pkgid` for more information about SPEC format." - ).into()) + ) + .into()); } let specs = args diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 348aa94a0d2..5f2a0ad50dc 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -56,7 +56,11 @@ pub trait AppExt: Sized { } fn arg_package(self, package: &'static str) -> Self { - self._arg(optinal_opt("package", package).short("p").value_name("SPEC")) + self._arg( + optinal_opt("package", package) + .short("p") + .value_name("SPEC"), + ) } fn arg_jobs(self) -> Self { diff --git a/src/cargo/util/workspace.rs b/src/cargo/util/workspace.rs index 3f342994ecc..d261717dc10 100644 --- a/src/cargo/util/workspace.rs +++ b/src/cargo/util/workspace.rs @@ -67,14 +67,13 @@ pub fn print_available_packages(ws: &Workspace<'_>) -> CargoResult<()> { writeln!(output, "No packages available.")?; } else { writeln!(output, "Possible packages/workspace members:")?; - for package in packages { + for package in packages { writeln!(output, " {}", package)?; } } bail!("{}", output) } - pub fn print_available_examples(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> { print_available_targets(Target::is_example, ws, options, "--example", "examples") } diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index 30bfefec0a0..070c39008ae 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -1185,7 +1185,8 @@ fn uninstall_multiple_and_specifying_bin() { fn uninstall_with_empty_pakcage_option() { cargo_process("uninstall -p") .with_status(101) - .with_stderr("\ + .with_stderr( + "\ [ERROR] \"--package \" requires a SPEC format value. Run `cargo help pkgid` for more information about SPEC format. ", From 4b9c503dfb2c3e452e2abb7f07a02a7284bf1001 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Wed, 28 Oct 2020 09:40:09 +0800 Subject: [PATCH 12/12] fix: remove unnecessary arg value existence check --- src/bin/cargo/commands/pkgid.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/cargo/commands/pkgid.rs b/src/bin/cargo/commands/pkgid.rs index d81b7d9f619..3b822355664 100644 --- a/src/bin/cargo/commands/pkgid.rs +++ b/src/bin/cargo/commands/pkgid.rs @@ -15,7 +15,7 @@ pub fn cli() -> App { pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let ws = args.workspace(config)?; - if !args.is_present("spec") && args.is_present_with_zero_values("package") { + if args.is_present_with_zero_values("package") { print_available_packages(&ws)? } let spec = args.value_of("spec").or_else(|| args.value_of("package"));