Skip to content

Commit 4c66d18

Browse files
committed
fix panic if an alias is defined to ""
1 parent 6df4b1e commit 4c66d18

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,15 @@ impl Execs {
803803
}
804804
}
805805

806+
#[track_caller]
807+
pub fn run_expect_error(&mut self) {
808+
self.ran = true;
809+
let p = (&self.process_builder).clone().unwrap();
810+
if self.match_process(&p).is_ok() {
811+
panic!("test was expected to fail, but succeeded running {}", p);
812+
}
813+
}
814+
806815
/// Runs the process, checks the expected output, and returns the first
807816
/// JSON object on stdout.
808817
#[track_caller]

src/bin/cargo/commands/help.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ fn try_help(config: &Config) -> CargoResult<bool> {
5555
return Ok(true);
5656
}
5757
// Otherwise, resolve the alias into its subcommand.
58-
Some(argv) => argv[0].clone(),
58+
Some(argv) => {
59+
// An alias with an empty argv can be created via `"empty-alias" = ""`.
60+
let first = argv.get(0).map(String::as_str).unwrap_or(subcommand);
61+
first.to_string()
62+
}
5963
None => subcommand.to_string(),
6064
};
6165

tests/testsuite/help.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,19 @@ fn help_alias() {
146146
config,
147147
r#"
148148
[alias]
149-
simple-alias = ["build"]
149+
empty-alias = ""
150+
simple-alias = "build"
150151
complex-alias = ["build", "--release"]
151152
"#,
152153
)
153154
.unwrap();
154155

156+
// The `empty-alias` returns an error.
157+
cargo_process("help empty-alias")
158+
.env("PATH", Path::new(""))
159+
.with_stderr_contains("[..]The subcommand 'empty-alias' wasn't recognized[..]")
160+
.run_expect_error();
161+
155162
// Because `simple-alias` aliases a subcommand with no arguments, help shows the manpage.
156163
help_with_man_and_path("", "simple-alias", "build", Path::new(""));
157164

0 commit comments

Comments
 (0)