Skip to content

Commit

Permalink
test: requires attribute accepts string literals for cmds
Browse files Browse the repository at this point in the history
The new syntax is key-value pair like `requires = "rustfmt"`,
comparing to the previous `requires_<cmd>`.
  • Loading branch information
weihanglo committed Nov 30, 2024
1 parent 759ee7c commit 4240e72
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ cargo-credential-libsecret = { version = "0.4.7", path = "credential/cargo-crede
cargo-credential-macos-keychain = { version = "0.4.7", path = "credential/cargo-credential-macos-keychain" }
cargo-credential-wincred = { version = "0.4.7", path = "credential/cargo-credential-wincred" }
cargo-platform = { path = "crates/cargo-platform", version = "0.2.0" }
cargo-test-macro = { version = "0.3.0", path = "crates/cargo-test-macro" }
cargo-test-macro = { version = "0.4.0", path = "crates/cargo-test-macro" }
cargo-test-support = { version = "0.6.0", path = "crates/cargo-test-support" }
cargo-util = { version = "0.2.14", path = "crates/cargo-util" }
cargo-util-schemas = { version = "0.7.0", path = "crates/cargo-util-schemas" }
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-test-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-test-macro"
version = "0.3.4"
version = "0.4.0"
edition.workspace = true
rust-version = "1.83" # MSRV:1
license.workspace = true
Expand Down
16 changes: 13 additions & 3 deletions crates/cargo-test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ use std::sync::Once;
/// This is useful for tests that use unstable options in `rustc` or `rustdoc`.
/// 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.
/// A `reason` field is required to explain why it is nightly-only.
/// * `requires_<cmd>` --- This indicates a command that is required to be installed to be run.
/// For example, `requires_rustfmt` means the test will only run if the executable `rustfmt` is installed.
/// * `requires = "<cmd>"` --- This indicates a command that is required to be installed to be run.
/// For example, `requires = "rustfmt"` means the test will only run if the executable `rustfmt` is installed.
/// These tests are *always* run on CI.
/// This is mainly used to avoid requiring contributors from having every dependency installed.
/// * `build_std_real` --- This is a "real" `-Zbuild-std` test (in the `build_std` integration test).
Expand Down Expand Up @@ -133,8 +133,18 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
"rustup or stable toolchain not installed"
);
}
s if s.starts_with("requires_") => {
s if s.starts_with("requires=") => {
let command = &s[9..];
let Ok(literal) = command.parse::<Literal>() else {
panic!("expect a string literal, found: {command}");
};
let literal = literal.to_string();
let Some(command) = literal
.strip_prefix('"')
.and_then(|lit| lit.strip_suffix('"'))
else {
panic!("expect a quoted string literal, found: {literal}");
};
set_ignore!(!has_command(command), "{command} not installed");
}
s if s.starts_with(">=1.") => {
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_init/simple_hg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::Project;

#[cargo_test(requires_hg)]
#[cargo_test(requires = "hg")]
fn case() {
let project = Project::from_template(current_dir!().join("in"));
let project_root = &project.root();
Expand Down
8 changes: 4 additions & 4 deletions tests/testsuite/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2917,7 +2917,7 @@ fn failed_submodule_checkout() {
t.join().unwrap();
}

#[cargo_test(requires_git)]
#[cargo_test(requires = "git")]
fn use_the_cli() {
let project = project();
let git_project = git::new("dep1", |project| {
Expand Down Expand Up @@ -3028,7 +3028,7 @@ fn templatedir_doesnt_cause_problems() {
p.cargo("check").run();
}

#[cargo_test(requires_git)]
#[cargo_test(requires = "git")]
fn git_with_cli_force() {
// Supports a force-pushed repo.
let git_project = git::new("dep1", |project| {
Expand Down Expand Up @@ -3095,7 +3095,7 @@ two
.run();
}

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

#[cargo_test(public_network_test, requires_git)]
#[cargo_test(public_network_test, requires = "git")]
fn github_fastpath_error_message() {
let p = project()
.file(
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/git_gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn run_test(path_env: Option<&OsStr>) {
);
}

#[cargo_test(requires_git)]
#[cargo_test(requires = "git")]
fn use_git_gc() {
run_test(None);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn simple_git() {
cargo_process("build").cwd(&paths::root().join("foo")).run();
}

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

Expand Down
26 changes: 19 additions & 7 deletions tests/testsuite/profile_trim_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,17 @@ mod object_works {
.stdout
}

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

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

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

#[cargo_test(requires_readelf, nightly, reason = "-Zremap-path-scope is unstable")]
#[cargo_test(
requires = "readelf",
nightly,
reason = "-Zremap-path-scope is unstable"
)]
fn with_split_debuginfo_off() {
object_works_helper("off", inspect_debuginfo);
}

#[cargo_test(requires_readelf, nightly, reason = "-Zremap-path-scope is unstable")]
#[cargo_test(
requires = "readelf",
nightly,
reason = "-Zremap-path-scope is unstable"
)]
fn with_split_debuginfo_packed() {
object_works_helper("packed", inspect_debuginfo);
}

#[cargo_test(requires_readelf, nightly, reason = "-Zremap-path-scope is unstable")]
#[cargo_test(
requires = "readelf",
nightly,
reason = "-Zremap-path-scope is unstable"
)]
fn with_split_debuginfo_unpacked() {
object_works_helper("unpacked", inspect_debuginfo);
}
Expand Down Expand Up @@ -676,7 +688,7 @@ fn custom_build_env_var_trim_paths() {
}

#[cfg(unix)]
#[cargo_test(requires_lldb, nightly, reason = "-Zremap-path-scope is unstable")]
#[cargo_test(requires = "lldb", nightly, reason = "-Zremap-path-scope is unstable")]
fn lldb_works_after_trimmed() {
use cargo_test_support::compare::assert_e2e;
use cargo_util::is_ci;
Expand Down

0 comments on commit 4240e72

Please sign in to comment.