mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-25 11:14:46 +00:00
Added aliases to subcommand typo suggestions.
Fixes #7278. Also adds tests for alias suggestions.
This commit is contained in:
parent
a429e8cc46
commit
ff3e880c17
@ -113,6 +113,17 @@ fn list_commands(config: &Config) -> BTreeSet<CommandInfo> {
|
||||
commands
|
||||
}
|
||||
|
||||
/// List all runnable aliases
|
||||
fn list_aliases(config: &Config) -> Vec<String> {
|
||||
match config.get_table("alias") {
|
||||
Ok(table) => match table {
|
||||
Some(aliases) => aliases.val.keys().map(|a| a.to_string()).collect(),
|
||||
None => Vec::new(),
|
||||
},
|
||||
Err(_) => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> CliResult {
|
||||
let command_exe = format!("cargo-{}{}", cmd, env::consts::EXE_SUFFIX);
|
||||
let path = search_directories(config)
|
||||
@ -122,8 +133,13 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> Cli
|
||||
let command = match path {
|
||||
Some(command) => command,
|
||||
None => {
|
||||
let cmds = list_commands(config);
|
||||
let did_you_mean = closest_msg(cmd, cmds.iter(), |c| c.name());
|
||||
let commands: Vec<String> = list_commands(config)
|
||||
.iter()
|
||||
.map(|c| c.name().to_string())
|
||||
.collect();
|
||||
let aliases = list_aliases(config);
|
||||
let suggestions = commands.iter().chain(aliases.iter());
|
||||
let did_you_mean = closest_msg(cmd, suggestions, |c| c);
|
||||
let err = failure::format_err!("no such subcommand: `{}`{}", cmd, did_you_mean);
|
||||
return Err(CliError::new(err, 101));
|
||||
}
|
||||
|
@ -168,6 +168,49 @@ error: no such subcommand: `biuld`
|
||||
.run();
|
||||
}
|
||||
|
||||
#[cargo_test]
|
||||
fn find_closest_alias() {
|
||||
let root = paths::root();
|
||||
let my_home = root.join("my_home");
|
||||
fs::create_dir(&my_home).unwrap();
|
||||
File::create(&my_home.join("config"))
|
||||
.unwrap()
|
||||
.write_all(
|
||||
br#"
|
||||
[alias]
|
||||
myalias = "build"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
cargo_process("myalais")
|
||||
.env("CARGO_HOME", &my_home)
|
||||
.with_status(101)
|
||||
.with_stderr_contains(
|
||||
"\
|
||||
error: no such subcommand: `myalais`
|
||||
|
||||
<tab>Did you mean `myalias`?
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
||||
// But, if no alias is defined, it must not suggest one!
|
||||
cargo_process("myalais")
|
||||
.with_status(101)
|
||||
.with_stderr_contains(
|
||||
"\
|
||||
error: no such subcommand: `myalais`
|
||||
",
|
||||
)
|
||||
.with_stderr_does_not_contain(
|
||||
"\
|
||||
<tab>Did you mean `myalias`?
|
||||
",
|
||||
)
|
||||
.run();
|
||||
}
|
||||
|
||||
// If a subcommand is more than an edit distance of 3 away, we don't make a suggestion.
|
||||
#[cargo_test]
|
||||
fn find_closest_dont_correct_nonsense() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user