From c3422cfc40998755afb49bc0629eeb7e34d33d13 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 26 Jul 2018 11:29:47 +0100 Subject: [PATCH 1/5] Reformat main::list_commands to return CommandInfo over a tuple --- src/bin/cargo/cli.rs | 17 ++++++++++------- src/bin/cargo/command_prelude.rs | 15 +++++++++++++++ src/bin/cargo/main.rs | 19 ++++++++++++------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index cfa0a3a66da..85990c8f0ca 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -69,14 +69,17 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'" if args.is_present("list") { println!("Installed Commands:"); for command in list_commands(config) { - let (command, path) = command; - if is_verbose { - match path { - Some(p) => println!(" {:<20} {}", command, p), - None => println!(" {:<20}", command), + match command { + CommandInfo::BuiltIn { name } => { + println!(" {:<20} {}", name) + } + CommandInfo::External { name, path } => { + if is_verbose { + println!(" {:<20} {}", name, path.display()) + } else { + println!(" {:<20}", name) + } } - } else { - println!(" {}", command); } } return Ok(()); diff --git a/src/bin/cargo/command_prelude.rs b/src/bin/cargo/command_prelude.rs index fa49bea8639..59aef526da7 100644 --- a/src/bin/cargo/command_prelude.rs +++ b/src/bin/cargo/command_prelude.rs @@ -411,3 +411,18 @@ pub fn values(args: &ArgMatches, name: &str) -> Vec { .map(|s| s.to_string()) .collect() } + +#[derive(PartialEq, PartialOrd, Eq, Ord)] +pub enum CommandInfo { + BuiltIn { name: String }, + External { name: String, path: PathBuf }, +} + +impl CommandInfo { + pub fn name(&self) -> String { + match self { + CommandInfo::BuiltIn { name, .. } => name.to_string(), + CommandInfo::External { name, .. } => name.to_string(), + } + } +} diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index 76efc09094c..f2bf93bc95a 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -26,6 +26,8 @@ mod cli; mod command_prelude; mod commands; +use command_prelude::*; + fn main() { env_logger::init(); cargo::core::maybe_allow_nightly_features(); @@ -81,7 +83,7 @@ fn aliased_command(config: &Config, command: &str) -> CargoResult BTreeSet<(String, Option)> { +fn list_commands(config: &Config) -> BTreeSet { let prefix = "cargo-"; let suffix = env::consts::EXE_SUFFIX; let mut commands = BTreeSet::new(); @@ -101,16 +103,18 @@ fn list_commands(config: &Config) -> BTreeSet<(String, Option)> { } if is_executable(entry.path()) { let end = filename.len() - suffix.len(); - commands.insert(( - filename[prefix.len()..end].to_string(), - Some(path.display().to_string()), - )); + commands.insert(CommandInfo::External { + name: filename[prefix.len()..end].to_string(), + path: path.clone(), + }); } } } for cmd in commands::builtin() { - commands.insert((cmd.get_name().to_string(), None)); + commands.insert(CommandInfo::BuiltIn { + name: cmd.get_name().to_string(), + }); } commands @@ -121,7 +125,8 @@ fn find_closest(config: &Config, cmd: &str) -> Option { // Only consider candidates with a lev_distance of 3 or less so we don't // suggest out-of-the-blue options. cmds.into_iter() - .map(|(c, _)| (lev_distance(&c, cmd), c)) + .map(|c| c.name()) + .map(|c| (lev_distance(&c, cmd), c)) .filter(|&(d, _)| d < 4) .min_by_key(|a| a.0) .map(|slot| slot.1) From af2c355585887db60b8da28024aba3b2ab95edfe Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 26 Jul 2018 12:08:39 +0100 Subject: [PATCH 2/5] Show the command summary when running cargo --list --- src/bin/cargo/cli.rs | 5 +++-- src/bin/cargo/command_prelude.rs | 2 +- src/bin/cargo/main.rs | 1 + tests/testsuite/cargo_command.rs | 12 ++++++++++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 85990c8f0ca..3eb7f27be96 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -70,8 +70,9 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'" println!("Installed Commands:"); for command in list_commands(config) { match command { - CommandInfo::BuiltIn { name } => { - println!(" {:<20} {}", name) + CommandInfo::BuiltIn { name, about } => { + let summary = about.unwrap_or_default(); + println!(" {:<20} {}", name, summary) } CommandInfo::External { name, path } => { if is_verbose { diff --git a/src/bin/cargo/command_prelude.rs b/src/bin/cargo/command_prelude.rs index 59aef526da7..e40619b300e 100644 --- a/src/bin/cargo/command_prelude.rs +++ b/src/bin/cargo/command_prelude.rs @@ -414,7 +414,7 @@ pub fn values(args: &ArgMatches, name: &str) -> Vec { #[derive(PartialEq, PartialOrd, Eq, Ord)] pub enum CommandInfo { - BuiltIn { name: String }, + BuiltIn { name: String, about: Option, }, External { name: String, path: PathBuf }, } diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index f2bf93bc95a..18643a97e22 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -114,6 +114,7 @@ fn list_commands(config: &Config) -> BTreeSet { for cmd in commands::builtin() { commands.insert(CommandInfo::BuiltIn { name: cmd.get_name().to_string(), + about: cmd.p.meta.about.map(|s| s.to_string()), }); } diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index a754eeefe47..ffe020ad0ca 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -60,6 +60,18 @@ fn path() -> Vec { env::split_paths(&env::var_os("PATH").unwrap_or_default()).collect() } +#[test] +fn list_commands_with_descriptions() { + let p = project().build(); + let output = p.cargo("--list").exec_with_output().unwrap(); + let output = str::from_utf8(&output.stdout).unwrap(); + assert!( + output.contains("\n build Compile a local package and all of its dependencies"), + "missing build, with description: {}", + output + ); +} + #[test] fn list_command_looks_at_path() { let proj = project().build(); From a838f8fa9c4fed78a958e311d0a5934fb55b8026 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 26 Jul 2018 12:09:04 +0100 Subject: [PATCH 3/5] Fix read-manifest summary printing under cargo --list --- src/bin/cargo/cli.rs | 1 + src/bin/cargo/commands/read_manifest.rs | 7 +++++-- tests/testsuite/cargo_command.rs | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 3eb7f27be96..193def1e56d 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -72,6 +72,7 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'" match command { CommandInfo::BuiltIn { name, about } => { let summary = about.unwrap_or_default(); + let summary = summary.lines().next().unwrap_or(&summary); // display only the first line println!(" {:<20} {}", name, summary) } CommandInfo::External { name, path } => { diff --git a/src/bin/cargo/commands/read_manifest.rs b/src/bin/cargo/commands/read_manifest.rs index 1e54c79e87f..300bfe9c952 100644 --- a/src/bin/cargo/commands/read_manifest.rs +++ b/src/bin/cargo/commands/read_manifest.rs @@ -5,8 +5,11 @@ use cargo::print_json; pub fn cli() -> App { subcommand("read-manifest") .about( - "Deprecated, use `cargo metadata --no-deps` instead. -Print a JSON representation of a Cargo.toml manifest.", + "\ +Print a JSON representation of a Cargo.toml manifest. + +Deprecated, use `cargo metadata --no-deps` instead.\ +", ) .arg_manifest_path() } diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index ffe020ad0ca..b73d7508a62 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -70,6 +70,12 @@ fn list_commands_with_descriptions() { "missing build, with description: {}", output ); + // assert read-manifest prints the right one-line description followed by another command, indented. + assert!( + output.contains("\n read-manifest Print a JSON representation of a Cargo.toml manifest.\n "), + "missing build, with description: {}", + output + ); } #[test] From 09cd53832c7908b0f2e4a72ee6170b824496cdb7 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 26 Jul 2018 21:41:43 +0100 Subject: [PATCH 4/5] Adapt --list output in find_closest_biuld_to_build test --- tests/testsuite/cargo_command.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index b73d7508a62..646200a76c3 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -170,7 +170,7 @@ error: no such subcommand: `biuld` cargo_process().arg("--list"), execs() .with_status(0) - .with_stdout_contains(" build\n") + .with_stdout_contains(" build Compile a local package and all of its dependencies\n") .with_stdout_contains(" biuld\n"), ); } From cd55e0459cff37b2d2a20a13134bbac3fa7aca5a Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 26 Jul 2018 21:42:31 +0100 Subject: [PATCH 5/5] Avoid right-padding commands with spaces Primarily because it needlessly breaks tests. --- src/bin/cargo/cli.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 193def1e56d..d50c11673ea 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -79,7 +79,7 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'" if is_verbose { println!(" {:<20} {}", name, path.display()) } else { - println!(" {:<20}", name) + println!(" {}", name) } } }