Skip to content

Commit 8bb6231

Browse files
committed
cargo_test CI macro: add requires_rustup_stable
This adds the `requires_rustup_stable` option to the cargo_test macro to require `rustup` to exist, and for the `stable` toolchain to be installed. It is required that stable be installed in Cargo's CI. On a developer machine, the tests will be ignored if rustup is not installed. It will also be ignored on rust-lang/rust's CI since it does not have rustup.
1 parent f9ba89b commit 8bb6231

File tree

1 file changed

+25
-9
lines changed
  • crates/cargo-test-macro/src

1 file changed

+25
-9
lines changed

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
7474
requires_reason = true;
7575
set_ignore!(is_not_nightly, "requires nightly");
7676
}
77+
"requires_rustup_stable" => {
78+
set_ignore!(
79+
!has_rustup_stable(),
80+
"rustup or stable toolchain not installed"
81+
);
82+
}
7783
s if s.starts_with("requires_") => {
7884
let command = &s[9..];
7985
set_ignore!(!has_command(command), "{command} not installed");
@@ -204,29 +210,27 @@ fn version() -> (u32, bool) {
204210
unsafe { VERSION }
205211
}
206212

207-
fn has_command(command: &str) -> bool {
208-
let output = match Command::new(command).arg("--version").output() {
213+
fn check_command(command_name: &str, args: &[&str]) -> bool {
214+
let mut command = Command::new(command_name);
215+
command.args(args);
216+
let output = match command.output() {
209217
Ok(output) => output,
210218
Err(e) => {
211219
// * hg is not installed on GitHub macOS or certain constrained
212220
// environments like Docker. Consider installing it if Cargo
213221
// gains more hg support, but otherwise it isn't critical.
214222
// * lldb is not pre-installed on Ubuntu and Windows, so skip.
215-
if is_ci() && !["hg", "lldb"].contains(&command) {
216-
panic!(
217-
"expected command `{}` to be somewhere in PATH: {}",
218-
command, e
219-
);
223+
if is_ci() && !matches!(command_name, "hg" | "lldb") {
224+
panic!("expected command `{command_name}` to be somewhere in PATH: {e}",);
220225
}
221226
return false;
222227
}
223228
};
224229
if !output.status.success() {
225230
panic!(
226-
"expected command `{}` to be runnable, got error {}:\n\
231+
"expected command `{command_name}` to be runnable, got error {}:\n\
227232
stderr:{}\n\
228233
stdout:{}\n",
229-
command,
230234
output.status,
231235
String::from_utf8_lossy(&output.stderr),
232236
String::from_utf8_lossy(&output.stdout)
@@ -235,6 +239,18 @@ fn has_command(command: &str) -> bool {
235239
true
236240
}
237241

242+
fn has_command(command: &str) -> bool {
243+
check_command(command, &["--version"])
244+
}
245+
246+
fn has_rustup_stable() -> bool {
247+
if option_env!("CARGO_TEST_DISABLE_NIGHTLY").is_some() {
248+
// This cannot run on rust-lang/rust CI due to the lack of rustup.
249+
return false;
250+
}
251+
check_command("cargo", &["+stable", "--version"])
252+
}
253+
238254
/// Whether or not this running in a Continuous Integration environment.
239255
fn is_ci() -> bool {
240256
// Consider using `tracked_env` instead of option_env! when it is stabilized.

0 commit comments

Comments
 (0)