Skip to content

Commit

Permalink
fix panic if an alias is defined to ""
Browse files Browse the repository at this point in the history
  • Loading branch information
sstangl committed Dec 12, 2021
1 parent 6df4b1e commit 4c66d18
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,15 @@ impl Execs {
}
}

#[track_caller]
pub fn run_expect_error(&mut self) {
self.ran = true;
let p = (&self.process_builder).clone().unwrap();
if self.match_process(&p).is_ok() {
panic!("test was expected to fail, but succeeded running {}", p);
}
}

/// Runs the process, checks the expected output, and returns the first
/// JSON object on stdout.
#[track_caller]
Expand Down
6 changes: 5 additions & 1 deletion src/bin/cargo/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ fn try_help(config: &Config) -> CargoResult<bool> {
return Ok(true);
}
// Otherwise, resolve the alias into its subcommand.
Some(argv) => argv[0].clone(),
Some(argv) => {
// An alias with an empty argv can be created via `"empty-alias" = ""`.
let first = argv.get(0).map(String::as_str).unwrap_or(subcommand);
first.to_string()
}
None => subcommand.to_string(),
};

Expand Down
9 changes: 8 additions & 1 deletion tests/testsuite/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,19 @@ fn help_alias() {
config,
r#"
[alias]
simple-alias = ["build"]
empty-alias = ""
simple-alias = "build"
complex-alias = ["build", "--release"]
"#,
)
.unwrap();

// The `empty-alias` returns an error.
cargo_process("help empty-alias")
.env("PATH", Path::new(""))
.with_stderr_contains("[..]The subcommand 'empty-alias' wasn't recognized[..]")
.run_expect_error();

// Because `simple-alias` aliases a subcommand with no arguments, help shows the manpage.
help_with_man_and_path("", "simple-alias", "build", Path::new(""));

Expand Down

0 comments on commit 4c66d18

Please sign in to comment.