Skip to content

Commit

Permalink
fix(alias): add test
Browse files Browse the repository at this point in the history
  • Loading branch information
heisen-li committed Apr 29, 2024
1 parent c6c7238 commit cbd5bbb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/bin/cargo/cli.rs
Expand Up @@ -279,9 +279,6 @@ fn expand_aliases(
args: ArgMatches,
mut already_expanded: Vec<String>,
) -> Result<(ArgMatches, GlobalArgs), CliError> {
println!("gctx == {:#?}", gctx);
println!("args == {:#?}", args);

if let Some((cmd, sub_args)) = args.subcommand() {
let exec = commands::builtin_exec(cmd);
let aliased_cmd = super::aliased_command(gctx, cmd);
Expand Down Expand Up @@ -359,9 +356,11 @@ For more information, see issue #12207 <https://github.com/rust-lang/cargo/issue
let global_args = GlobalArgs::new(sub_args);
let new_args = cli(gctx).no_binary_name(true).try_get_matches_from(alias)?;

let new_cmd = match new_args.subcommand_name() {
Some(new_cmd) => new_cmd,
None => return Err(anyhow!("subcommand is required, add a subcommand to the command alias {cmd} .").into()),
let Some(new_cmd) = new_args.subcommand_name() else {
return Err(anyhow!(
"subcommand is required, add a subcommand to the command alias `alias.{cmd}`"
)
.into());
};

already_expanded.push(cmd.to_string());
Expand Down
24 changes: 24 additions & 0 deletions tests/testsuite/cargo_alias_config.rs
Expand Up @@ -463,3 +463,27 @@ fn empty_alias() {
)
.run();
}

#[cargo_test]
fn alias_no_subcommand() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/main.rs", "fn main() {}")
.file(
".cargo/config.toml",
r#"
[alias]
a = "--locked"
"#,
)
.build();

p.cargo("a")
.with_status(101)
.with_stderr(
"\
[ERROR] subcommand is required, add a subcommand to the command alias `alias.a`
",
)
.run();
}

0 comments on commit cbd5bbb

Please sign in to comment.