Skip to content

Commit cbd5bbb

Browse files
committed
fix(alias): add test
1 parent c6c7238 commit cbd5bbb

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/bin/cargo/cli.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,6 @@ fn expand_aliases(
279279
args: ArgMatches,
280280
mut already_expanded: Vec<String>,
281281
) -> Result<(ArgMatches, GlobalArgs), CliError> {
282-
println!("gctx == {:#?}", gctx);
283-
println!("args == {:#?}", args);
284-
285282
if let Some((cmd, sub_args)) = args.subcommand() {
286283
let exec = commands::builtin_exec(cmd);
287284
let aliased_cmd = super::aliased_command(gctx, cmd);
@@ -359,9 +356,11 @@ For more information, see issue #12207 <https://github.com/rust-lang/cargo/issue
359356
let global_args = GlobalArgs::new(sub_args);
360357
let new_args = cli(gctx).no_binary_name(true).try_get_matches_from(alias)?;
361358

362-
let new_cmd = match new_args.subcommand_name() {
363-
Some(new_cmd) => new_cmd,
364-
None => return Err(anyhow!("subcommand is required, add a subcommand to the command alias {cmd} .").into()),
359+
let Some(new_cmd) = new_args.subcommand_name() else {
360+
return Err(anyhow!(
361+
"subcommand is required, add a subcommand to the command alias `alias.{cmd}`"
362+
)
363+
.into());
365364
};
366365

367366
already_expanded.push(cmd.to_string());

tests/testsuite/cargo_alias_config.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,3 +463,27 @@ fn empty_alias() {
463463
)
464464
.run();
465465
}
466+
467+
#[cargo_test]
468+
fn alias_no_subcommand() {
469+
let p = project()
470+
.file("Cargo.toml", &basic_bin_manifest("foo"))
471+
.file("src/main.rs", "fn main() {}")
472+
.file(
473+
".cargo/config.toml",
474+
r#"
475+
[alias]
476+
a = "--locked"
477+
"#,
478+
)
479+
.build();
480+
481+
p.cargo("a")
482+
.with_status(101)
483+
.with_stderr(
484+
"\
485+
[ERROR] subcommand is required, add a subcommand to the command alias `alias.a`
486+
",
487+
)
488+
.run();
489+
}

0 commit comments

Comments
 (0)