diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index f9299367668..64a465108a1 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -47,6 +47,21 @@ fn main() { } } +/// Table for defining the aliases which come builtin in `Cargo`. +/// The contents are structured as: `(alias, aliased_command, description)`. +const BUILTIN_ALIASES: [(&str, &str, &str); 4] = [ + ("b", "build", "alias: build"), + ("c", "check", "alias: check"), + ("r", "run", "alias: run"), + ("t", "test", "alias: test"), +]; + +/// Function which contains the list of all of the builtin aliases and it's +/// corresponding execs represented as &str. +fn builtin_aliases_execs(cmd: &str) -> Option<&(&str, &str, &str)> { + BUILTIN_ALIASES.iter().find(|alias| alias.0 == cmd) +} + fn aliased_command(config: &Config, command: &str) -> CargoResult>> { let alias_name = format!("alias.{}", command); let user_alias = match config.get_string(&alias_name) { @@ -60,12 +75,10 @@ fn aliased_command(config: &Config, command: &str) -> CargoResult None, Err(_) => config.get::>>(&alias_name)?, }; - let result = user_alias.or_else(|| match command { - "b" => Some(vec!["build".to_string()]), - "c" => Some(vec!["check".to_string()]), - "r" => Some(vec!["run".to_string()]), - "t" => Some(vec!["test".to_string()]), - _ => None, + + let result = user_alias.or_else(|| match builtin_aliases_execs(command) { + Some(command_str) => Some(vec![command_str.1.to_string()]), + None => None, }); Ok(result) } @@ -106,6 +119,15 @@ fn list_commands(config: &Config) -> BTreeSet { }); } + // Add the builtin_aliases and them descriptions to the + // `commands` `BTreeSet`. + for command in &BUILTIN_ALIASES { + commands.insert(CommandInfo::BuiltIn { + name: command.0.to_string(), + about: Some(command.2.to_string()), + }); + } + commands } diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index fa4e05a74d2..667c2933886 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -78,6 +78,17 @@ fn list_commands_with_descriptions() { .run(); } +#[cargo_test] +fn list_aliases_with_descriptions() { + let p = project().build(); + p.cargo("--list") + .with_stdout_contains(" b alias: build") + .with_stdout_contains(" c alias: check") + .with_stdout_contains(" r alias: run") + .with_stdout_contains(" t alias: test") + .run(); +} + #[cargo_test] fn list_command_looks_at_path() { let proj = project().build();