Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,43 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'"
return Ok(());
}

let args = expand_aliases(config, args)?;

execute_subcommand(config, args)
}

fn expand_aliases(
config: &mut Config,
args: ArgMatches<'static>,
) -> Result<ArgMatches<'static>, CliError> {
if let (cmd, Some(args)) = args.subcommand() {
match (
commands::builtin_exec(cmd),
super::aliased_command(config, cmd)?,
) {
(None, Some(mut alias)) => {
alias.extend(
args.values_of("")
.unwrap_or_default()
.map(|s| s.to_string()),
);
let args = cli()
.setting(AppSettings::NoBinaryName)
.get_matches_from_safe(alias)?;
return expand_aliases(config, args);
}
(Some(_), Some(_)) => {
config.shell().warn(format!(
"alias `{}` is ignored, because it is shadowed by a built in command",
cmd
))?;
}
(_, None) => {}
}
};
Ok(args)
}

fn execute_subcommand(config: &mut Config, args: ArgMatches) -> CliResult {
let (cmd, subcommand_args) = match args.subcommand() {
(cmd, Some(args)) => (cmd, args),
Expand All @@ -79,7 +113,7 @@ fn execute_subcommand(config: &mut Config, args: ArgMatches) -> CliResult {
return Ok(());
}
};

let arg_target_dir = &subcommand_args.value_of_path("target-dir", config);

config.configure(
Expand All @@ -101,17 +135,6 @@ fn execute_subcommand(config: &mut Config, args: ArgMatches) -> CliResult {
return exec(config, subcommand_args);
}

if let Some(mut alias) = super::aliased_command(config, cmd)? {
alias.extend(
subcommand_args.values_of("")
.unwrap_or_default()
.map(|s| s.to_string()),
);
let subcommand_args = cli()
.setting(AppSettings::NoBinaryName)
.get_matches_from_safe(alias)?;
return execute_subcommand(config, subcommand_args);
}
let mut ext_args: Vec<&str> = vec![cmd];
ext_args.extend(subcommand_args.values_of("").unwrap_or_default());
super::execute_external_subcommand(config, cmd, &ext_args)
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ impl Config {
cfg.merge(value)
.chain_err(|| format!("failed to merge configuration at `{}`", path.display()))?;
Ok(())
}).chain_err(|| "Couldn't load Cargo configuration")?;
}).chain_err(|| "could not load Cargo configuration")?;

self.load_credentials(&mut cfg)?;
match cfg {
Expand Down
9 changes: 3 additions & 6 deletions tests/testsuite/bad_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn bad2() {
p.cargo("publish").arg("-v"),
execs().with_status(101).with_stderr(
"\
[ERROR] Couldn't load Cargo configuration
[ERROR] could not load Cargo configuration

Caused by:
failed to load TOML configuration from `[..]config`
Expand Down Expand Up @@ -164,10 +164,7 @@ fn bad5() {
.cwd(&p.root().join("foo")),
execs().with_status(101).with_stderr(
"\
[ERROR] Failed to create project `foo` at `[..]`

Caused by:
Couldn't load Cargo configuration
[ERROR] could not load Cargo configuration

Caused by:
failed to merge configuration at `[..]`
Expand Down Expand Up @@ -284,7 +281,7 @@ fn invalid_global_config() {
p.cargo("build").arg("-v"),
execs().with_status(101).with_stderr(
"\
[ERROR] Couldn't load Cargo configuration
[ERROR] could not load Cargo configuration

Caused by:
could not parse TOML configuration in `[..]`
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2913,7 +2913,7 @@ fn bad_cargo_config() {
foo.cargo("build").arg("-v"),
execs().with_status(101).with_stderr(
"\
[ERROR] Couldn't load Cargo configuration
[ERROR] could not load Cargo configuration

Caused by:
could not parse TOML configuration in `[..]`
Expand Down
42 changes: 38 additions & 4 deletions tests/testsuite/cargo_alias_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ fn alias_incorrect_config_type() {
assert_that(
p.cargo("b-cargo-test").arg("-v"),
execs().with_status(101).with_stderr_contains(
"[ERROR] invalid configuration \
for key `alias.b-cargo-test`
"\
[ERROR] invalid configuration for key `alias.b-cargo-test`
expected a list, but found a integer for [..]",
),
);
Expand Down Expand Up @@ -77,9 +77,42 @@ fn alias_config() {
.build();

assert_that(
p.cargo("b-cargo-test").arg("-v"),
p.cargo("b-cargo-test -v"),
execs()
.with_status(0)
.with_stderr_contains(
"\
[COMPILING] foo v0.5.0 [..]
[RUNNING] `rustc --crate-name foo [..]",
),
);
}

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

assert_that(
p.cargo("a-cargo-test"),
execs().with_status(0).with_stderr_contains(
"[COMPILING] foo v0.5.0 [..]
"\
[COMPILING] foo v0.5.0 [..]
[RUNNING] `rustc --crate-name foo [..]",
),
);
Expand Down Expand Up @@ -164,6 +197,7 @@ fn cant_shadow_builtin() {
p.cargo("build"),
execs().with_status(0).with_stderr(
"\
[WARNING] alias `build` is ignored, because it is shadowed by a built in command
[COMPILING] foo v0.5.0 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
Expand Down