Skip to content

Commit 9ac36a6

Browse files
authored
test: requires attribute accepts string literals for cmds (#14875)
### What does this PR try to resolve? The new syntax is key-value pair like `requires = "rustfmt"`, comparing to the previous `requires_<cmd>`. This enables `cargo_test` to require that the CLI contains non-alphabetic names, such as `llvm-profdata`. ### How should we test and review this PR? CI passes ### Additional information This is a request from <#14859 (comment)>, but I found it a bit tricky so split off this change first.
2 parents 759ee7c + 4240e72 commit 9ac36a6

File tree

9 files changed

+42
-20
lines changed

9 files changed

+42
-20
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cargo-credential-libsecret = { version = "0.4.7", path = "credential/cargo-crede
3232
cargo-credential-macos-keychain = { version = "0.4.7", path = "credential/cargo-credential-macos-keychain" }
3333
cargo-credential-wincred = { version = "0.4.7", path = "credential/cargo-credential-wincred" }
3434
cargo-platform = { path = "crates/cargo-platform", version = "0.2.0" }
35-
cargo-test-macro = { version = "0.3.0", path = "crates/cargo-test-macro" }
35+
cargo-test-macro = { version = "0.4.0", path = "crates/cargo-test-macro" }
3636
cargo-test-support = { version = "0.6.0", path = "crates/cargo-test-support" }
3737
cargo-util = { version = "0.2.14", path = "crates/cargo-util" }
3838
cargo-util-schemas = { version = "0.7.0", path = "crates/cargo-util-schemas" }

crates/cargo-test-macro/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cargo-test-macro"
3-
version = "0.3.4"
3+
version = "0.4.0"
44
edition.workspace = true
55
rust-version = "1.83" # MSRV:1
66
license.workspace = true

crates/cargo-test-macro/src/lib.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ use std::sync::Once;
3434
/// This is useful for tests that use unstable options in `rustc` or `rustdoc`.
3535
/// These tests are run in Cargo's CI, but are disabled in rust-lang/rust's CI due to the difficulty of updating both repos simultaneously.
3636
/// A `reason` field is required to explain why it is nightly-only.
37-
/// * `requires_<cmd>` --- This indicates a command that is required to be installed to be run.
38-
/// For example, `requires_rustfmt` means the test will only run if the executable `rustfmt` is installed.
37+
/// * `requires = "<cmd>"` --- This indicates a command that is required to be installed to be run.
38+
/// For example, `requires = "rustfmt"` means the test will only run if the executable `rustfmt` is installed.
3939
/// These tests are *always* run on CI.
4040
/// This is mainly used to avoid requiring contributors from having every dependency installed.
4141
/// * `build_std_real` --- This is a "real" `-Zbuild-std` test (in the `build_std` integration test).
@@ -133,8 +133,18 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
133133
"rustup or stable toolchain not installed"
134134
);
135135
}
136-
s if s.starts_with("requires_") => {
136+
s if s.starts_with("requires=") => {
137137
let command = &s[9..];
138+
let Ok(literal) = command.parse::<Literal>() else {
139+
panic!("expect a string literal, found: {command}");
140+
};
141+
let literal = literal.to_string();
142+
let Some(command) = literal
143+
.strip_prefix('"')
144+
.and_then(|lit| lit.strip_suffix('"'))
145+
else {
146+
panic!("expect a quoted string literal, found: {literal}");
147+
};
138148
set_ignore!(!has_command(command), "{command} not installed");
139149
}
140150
s if s.starts_with(">=1.") => {

tests/testsuite/cargo_init/simple_hg/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use cargo_test_support::prelude::*;
55
use cargo_test_support::str;
66
use cargo_test_support::Project;
77

8-
#[cargo_test(requires_hg)]
8+
#[cargo_test(requires = "hg")]
99
fn case() {
1010
let project = Project::from_template(current_dir!().join("in"));
1111
let project_root = &project.root();

tests/testsuite/git.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2917,7 +2917,7 @@ fn failed_submodule_checkout() {
29172917
t.join().unwrap();
29182918
}
29192919

2920-
#[cargo_test(requires_git)]
2920+
#[cargo_test(requires = "git")]
29212921
fn use_the_cli() {
29222922
let project = project();
29232923
let git_project = git::new("dep1", |project| {
@@ -3028,7 +3028,7 @@ fn templatedir_doesnt_cause_problems() {
30283028
p.cargo("check").run();
30293029
}
30303030

3031-
#[cargo_test(requires_git)]
3031+
#[cargo_test(requires = "git")]
30323032
fn git_with_cli_force() {
30333033
// Supports a force-pushed repo.
30343034
let git_project = git::new("dep1", |project| {
@@ -3095,7 +3095,7 @@ two
30953095
.run();
30963096
}
30973097

3098-
#[cargo_test(requires_git)]
3098+
#[cargo_test(requires = "git")]
30993099
fn git_fetch_cli_env_clean() {
31003100
// This tests that git-fetch-with-cli works when GIT_DIR environment
31013101
// variable is set (for whatever reason).
@@ -4069,7 +4069,7 @@ src/lib.rs
40694069
.run();
40704070
}
40714071

4072-
#[cargo_test(public_network_test, requires_git)]
4072+
#[cargo_test(public_network_test, requires = "git")]
40734073
fn github_fastpath_error_message() {
40744074
let p = project()
40754075
.file(

tests/testsuite/git_gc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn run_test(path_env: Option<&OsStr>) {
9090
);
9191
}
9292

93-
#[cargo_test(requires_git)]
93+
#[cargo_test(requires = "git")]
9494
fn use_git_gc() {
9595
run_test(None);
9696
}

tests/testsuite/new.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn simple_git() {
114114
cargo_process("build").cwd(&paths::root().join("foo")).run();
115115
}
116116

117-
#[cargo_test(requires_hg)]
117+
#[cargo_test(requires = "hg")]
118118
fn simple_hg() {
119119
cargo_process("new --lib foo --edition 2015 --vcs hg").run();
120120

tests/testsuite/profile_trim_paths.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -445,17 +445,17 @@ mod object_works {
445445
.stdout
446446
}
447447

448-
#[cargo_test(requires_nm, nightly, reason = "-Zremap-path-scope is unstable")]
448+
#[cargo_test(requires = "nm", nightly, reason = "-Zremap-path-scope is unstable")]
449449
fn with_split_debuginfo_off() {
450450
object_works_helper("off", inspect_debuginfo);
451451
}
452452

453-
#[cargo_test(requires_nm, nightly, reason = "-Zremap-path-scope is unstable")]
453+
#[cargo_test(requires = "nm", nightly, reason = "-Zremap-path-scope is unstable")]
454454
fn with_split_debuginfo_packed() {
455455
object_works_helper("packed", inspect_debuginfo);
456456
}
457457

458-
#[cargo_test(requires_nm, nightly, reason = "-Zremap-path-scope is unstable")]
458+
#[cargo_test(requires = "nm", nightly, reason = "-Zremap-path-scope is unstable")]
459459
fn with_split_debuginfo_unpacked() {
460460
object_works_helper("unpacked", inspect_debuginfo);
461461
}
@@ -475,17 +475,29 @@ mod object_works {
475475
.stdout
476476
}
477477

478-
#[cargo_test(requires_readelf, nightly, reason = "-Zremap-path-scope is unstable")]
478+
#[cargo_test(
479+
requires = "readelf",
480+
nightly,
481+
reason = "-Zremap-path-scope is unstable"
482+
)]
479483
fn with_split_debuginfo_off() {
480484
object_works_helper("off", inspect_debuginfo);
481485
}
482486

483-
#[cargo_test(requires_readelf, nightly, reason = "-Zremap-path-scope is unstable")]
487+
#[cargo_test(
488+
requires = "readelf",
489+
nightly,
490+
reason = "-Zremap-path-scope is unstable"
491+
)]
484492
fn with_split_debuginfo_packed() {
485493
object_works_helper("packed", inspect_debuginfo);
486494
}
487495

488-
#[cargo_test(requires_readelf, nightly, reason = "-Zremap-path-scope is unstable")]
496+
#[cargo_test(
497+
requires = "readelf",
498+
nightly,
499+
reason = "-Zremap-path-scope is unstable"
500+
)]
489501
fn with_split_debuginfo_unpacked() {
490502
object_works_helper("unpacked", inspect_debuginfo);
491503
}
@@ -676,7 +688,7 @@ fn custom_build_env_var_trim_paths() {
676688
}
677689

678690
#[cfg(unix)]
679-
#[cargo_test(requires_lldb, nightly, reason = "-Zremap-path-scope is unstable")]
691+
#[cargo_test(requires = "lldb", nightly, reason = "-Zremap-path-scope is unstable")]
680692
fn lldb_works_after_trimmed() {
681693
use cargo_test_support::compare::assert_e2e;
682694
use cargo_util::is_ci;

0 commit comments

Comments
 (0)