Skip to content

Commit

Permalink
Auto merge of #7486 - zachlute:suggest-alias2, r=alexcrichton
Browse files Browse the repository at this point in the history
Added aliases to subcommand typo suggestions.

Fixes #7278.

Also adds tests for alias suggestions.
  • Loading branch information
bors committed Nov 11, 2019
2 parents 2130016 + 0f157f5 commit 127d2b4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![warn(clippy::needless_borrow)]
#![warn(clippy::redundant_clone)]

use std::collections::BTreeSet;
use std::collections::{BTreeMap, BTreeSet};
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -111,6 +111,14 @@ fn list_commands(config: &Config) -> BTreeSet<CommandInfo> {
commands
}

/// List all runnable aliases
fn list_aliases(config: &Config) -> Vec<String> {
match config.get::<BTreeMap<String, String>>("alias") {
Ok(aliases) => aliases.keys().map(|a| a.to_string()).collect(),
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)
Expand All @@ -120,8 +128,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));
}
Expand Down
43 changes: 43 additions & 0 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 127d2b4

Please sign in to comment.