From 4c66d183611d9104fecb79da14e2dce46869e286 Mon Sep 17 00:00:00 2001 From: Sean Stangl Date: Sun, 12 Dec 2021 13:29:31 -0700 Subject: [PATCH] fix panic if an alias is defined to "" --- crates/cargo-test-support/src/lib.rs | 9 +++++++++ src/bin/cargo/commands/help.rs | 6 +++++- tests/testsuite/help.rs | 9 ++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index a4c4153bad2..5280ea0efd2 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -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] diff --git a/src/bin/cargo/commands/help.rs b/src/bin/cargo/commands/help.rs index 1adc129db96..caaa0c5a496 100644 --- a/src/bin/cargo/commands/help.rs +++ b/src/bin/cargo/commands/help.rs @@ -55,7 +55,11 @@ fn try_help(config: &Config) -> CargoResult { 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(), }; diff --git a/tests/testsuite/help.rs b/tests/testsuite/help.rs index 7cad8ba25ae..fdb527e761a 100644 --- a/tests/testsuite/help.rs +++ b/tests/testsuite/help.rs @@ -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(""));