From 4240e722d475b53067ae93f121d8e41109c2ac51 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 28 Nov 2024 17:31:33 -0500 Subject: [PATCH] test: `requires` attribute accepts string literals for cmds The new syntax is key-value pair like `requires = "rustfmt"`, comparing to the previous `requires_`. --- Cargo.lock | 2 +- Cargo.toml | 2 +- crates/cargo-test-macro/Cargo.toml | 2 +- crates/cargo-test-macro/src/lib.rs | 16 ++++++++++--- tests/testsuite/cargo_init/simple_hg/mod.rs | 2 +- tests/testsuite/git.rs | 8 +++---- tests/testsuite/git_gc.rs | 2 +- tests/testsuite/new.rs | 2 +- tests/testsuite/profile_trim_paths.rs | 26 +++++++++++++++------ 9 files changed, 42 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c981033f846..45441a66152 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -451,7 +451,7 @@ dependencies = [ [[package]] name = "cargo-test-macro" -version = "0.3.4" +version = "0.4.0" [[package]] name = "cargo-test-support" diff --git a/Cargo.toml b/Cargo.toml index a4c0749bc1f..988b2f9ba7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/crates/cargo-test-macro/Cargo.toml b/crates/cargo-test-macro/Cargo.toml index 78b13e2da49..379bf21efef 100644 --- a/crates/cargo-test-macro/Cargo.toml +++ b/crates/cargo-test-macro/Cargo.toml @@ -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 diff --git a/crates/cargo-test-macro/src/lib.rs b/crates/cargo-test-macro/src/lib.rs index 93bf0ec08c6..f9740873184 100644 --- a/crates/cargo-test-macro/src/lib.rs +++ b/crates/cargo-test-macro/src/lib.rs @@ -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_` --- 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 = ""` --- 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). @@ -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::() 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.") => { diff --git a/tests/testsuite/cargo_init/simple_hg/mod.rs b/tests/testsuite/cargo_init/simple_hg/mod.rs index c32455afa21..aa95f8065ce 100644 --- a/tests/testsuite/cargo_init/simple_hg/mod.rs +++ b/tests/testsuite/cargo_init/simple_hg/mod.rs @@ -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(); diff --git a/tests/testsuite/git.rs b/tests/testsuite/git.rs index 9637596c4d7..40e9db15c86 100644 --- a/tests/testsuite/git.rs +++ b/tests/testsuite/git.rs @@ -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| { @@ -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| { @@ -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). @@ -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( diff --git a/tests/testsuite/git_gc.rs b/tests/testsuite/git_gc.rs index fa1791fee8c..5b2cce78f3c 100644 --- a/tests/testsuite/git_gc.rs +++ b/tests/testsuite/git_gc.rs @@ -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); } diff --git a/tests/testsuite/new.rs b/tests/testsuite/new.rs index 4c007bec168..8a57283fc0c 100644 --- a/tests/testsuite/new.rs +++ b/tests/testsuite/new.rs @@ -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(); diff --git a/tests/testsuite/profile_trim_paths.rs b/tests/testsuite/profile_trim_paths.rs index 2adc1897b73..9340a523d0d 100644 --- a/tests/testsuite/profile_trim_paths.rs +++ b/tests/testsuite/profile_trim_paths.rs @@ -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); } @@ -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); } @@ -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;