Skip to content

Commit

Permalink
Use clap's conflict support and keep some command tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heisen-li committed Jun 5, 2024
1 parent 09a8ece commit 5d8022c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 85 deletions.
11 changes: 7 additions & 4 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ pub fn cli() -> Command {
)
.arg_features()
.arg_parallel()
.arg(flag(
"debug",
"Build in debug mode (with the 'dev' profile) instead of release mode",
))
.arg(
flag(
"debug",
"Build in debug mode (with the 'dev' profile) instead of release mode",
)
.conflicts_with("profile"),
)
.arg_redundant_default_mode("release", "install", "debug")
.arg_profile("Install artifacts with the specified profile")
.arg_target_triple("Build for the target triple")
Expand Down
21 changes: 7 additions & 14 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ pub trait CommandExt: Sized {
) -> Self {
let msg = format!("`--{default_mode}` is the default for `cargo {command}`; instead `--{supported_mode}` is supported");
let value_parser = UnknownArgumentValueParser::suggest(msg);
self._arg(flag(default_mode, "").value_parser(value_parser).hide(true))
self._arg(
flag(default_mode, "")
.conflicts_with("profile")
.value_parser(value_parser)
.hide(true),
)
}

fn arg_targets_all(
Expand Down Expand Up @@ -226,6 +231,7 @@ pub trait CommandExt: Sized {
self._arg(
flag("release", release)
.short('r')
.conflicts_with("profile")
.help_heading(heading::COMPILATION_OPTIONS),
)
}
Expand Down Expand Up @@ -563,24 +569,13 @@ Run `{cmd}` to see possible targets."
) -> CargoResult<InternedString> {
let specified_profile = self._value_of("profile");

let err_message = |flag: &str| {
format!(
"\
the `--{flag}` flag can not be specified with the `--profile` flag
Please remove one of the flags."
)
};

// Check for allowed legacy names.
// This is an early exit, since it allows combination with `--release`.
match (specified_profile, profile_checking) {
// `cargo rustc` has legacy handling of these names
(Some(name @ ("dev" | "test" | "bench" | "check")), ProfileChecking::LegacyRustc)
// `cargo fix` and `cargo check` has legacy handling of this profile name
| (Some(name @ "test"), ProfileChecking::LegacyTestOnly) => {
if self.maybe_flag("release") {
bail!(err_message("release"));
}
return Ok(InternedString::new(name));
}
_ => {}
Expand All @@ -593,9 +588,7 @@ Please remove one of the flags."
) {
(false, false, None) => default,
(true, _, None) => "release",
(true, _, Some(_)) => bail!(err_message("release")),
(_, true, None) => "dev",
(_, true, Some(_)) => bail!(err_message("debug")),
// `doc` is separate from all the other reservations because
// [profile.doc] was historically allowed, but is deprecated and
// has no effect. To avoid potentially breaking projects, it is a
Expand Down
18 changes: 12 additions & 6 deletions tests/testsuite/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,20 +289,26 @@ fn rustc_check() {

// Verify compatible usage of --profile with --release, issue #7488
foo.cargo("rustc --profile check --release -- --emit=metadata")
.with_status(101)
.with_status(1)
.with_stderr(
"\
error: the `--release` flag can not be specified with the `--profile` flag
Please remove one of the flags.",
[ERROR] the argument '--profile <PROFILE-NAME>' cannot be used with '--release'
Usage: cargo[EXE] rustc --profile <PROFILE-NAME> [ARGS]...
For more information, try '--help'.",
)
.run();

foo.cargo("rustc --profile test --release -- --emit=metadata")
.with_status(101)
.with_status(1)
.with_stderr(
"\
error: the `--release` flag can not be specified with the `--profile` flag
Please remove one of the flags.",
[ERROR] the argument '--profile <PROFILE-NAME>' cannot be used with '--release'
Usage: cargo[EXE] rustc --profile <PROFILE-NAME> [ARGS]...
For more information, try '--help'.",
)
.run();
}
Expand Down
75 changes: 14 additions & 61 deletions tests/testsuite/profile_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,85 +380,38 @@ fn conflicting_usage() {
.build();

p.cargo("build --profile=dev --release")
.with_status(101)
.with_status(1)
.with_stderr(
"\
error: the `--release` flag can not be specified with the `--profile` flag
Please remove one of the flags.",
)
.run();
[ERROR] the argument '--profile <PROFILE-NAME>' cannot be used with '--release'
p.cargo("install --profile=release --debug")
.with_status(101)
.with_stderr(
"\
error: the `--debug` flag can not be specified with the `--profile` flag
Please remove one of the flags.",
)
.run();
Usage: cargo[EXE] build --profile <PROFILE-NAME>
p.cargo("rustc --profile=dev --release")
.with_status(101)
.with_stderr(
"\
error: the `--release` flag can not be specified with the `--profile` flag
Please remove one of the flags.
",
For more information, try '--help'.",
)
.run();

p.cargo("check --profile=dev --release")
.with_status(101)
p.cargo("install --profile=release --debug")
.with_status(1)
.with_stderr(
"\
error: the `--release` flag can not be specified with the `--profile` flag
Please remove one of the flags.",
)
.run();
[ERROR] the argument '--profile <PROFILE-NAME>' cannot be used with '--debug'
p.cargo("check --profile=test --release")
.with_status(101)
.with_stderr(
"\
error: the `--release` flag can not be specified with the `--profile` flag
Please remove one of the flags.",
)
.run();
Usage: cargo[EXE] install --profile <PROFILE-NAME> [CRATE[@<VER>]]...
// This is OK since the two are the same.
p.cargo("rustc --profile=release --release")
.with_status(101)
.with_stderr(
"\
error: the `--release` flag can not be specified with the `--profile` flag
Please remove one of the flags.",
For more information, try '--help'.",
)
.run();

p.cargo("build --profile=release --release")
.with_status(101)
p.cargo("check --profile=dev --release")
.with_status(1)
.with_stderr(
"\
error: the `--release` flag can not be specified with the `--profile` flag
Please remove one of the flags.",
)
.run();
[ERROR] the argument '--profile <PROFILE-NAME>' cannot be used with '--release'
p.cargo("install --path . --profile=dev --debug")
.with_status(101)
.with_stderr(
"\
error: the `--debug` flag can not be specified with the `--profile` flag
Please remove one of the flags.",
)
.run();
Usage: cargo[EXE] check --profile <PROFILE-NAME>
p.cargo("install --path . --profile=release --debug")
.with_status(101)
.with_stderr(
"\
error: the `--debug` flag can not be specified with the `--profile` flag
Please remove one of the flags.",
For more information, try '--help'.",
)
.run();
}
Expand Down

0 comments on commit 5d8022c

Please sign in to comment.