Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display builtin aliases with cargo --list #8542

Merged
merged 5 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<Vec<String>>> {
let alias_name = format!("alias.{}", command);
let user_alias = match config.get_string(&alias_name) {
Expand All @@ -60,12 +75,10 @@ fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<Str
Ok(None) => None,
Err(_) => config.get::<Option<Vec<String>>>(&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)
}
Expand Down Expand Up @@ -106,6 +119,15 @@ fn list_commands(config: &Config) -> BTreeSet<CommandInfo> {
});
}

// 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
}

Expand Down
11 changes: 11 additions & 0 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down