Skip to content

Commit 4a88f92

Browse files
committed
test: extract checking installed target to a function
1 parent b696870 commit 4a88f92

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
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
@@ -33,7 +33,7 @@ cargo-credential-macos-keychain = { version = "0.4.7", path = "credential/cargo-
3333
cargo-credential-wincred = { version = "0.4.7", path = "credential/cargo-credential-wincred" }
3434
cargo-platform = { path = "crates/cargo-platform", version = "0.2.0" }
3535
cargo-test-macro = { version = "0.4.0", path = "crates/cargo-test-macro" }
36-
cargo-test-support = { version = "0.7.0", path = "crates/cargo-test-support" }
36+
cargo-test-support = { version = "0.7.1", 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" }
3939
cargo_metadata = "0.19.0"

crates/cargo-test-support/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-support"
3-
version = "0.7.0"
3+
version = "0.7.1"
44
edition.workspace = true
55
rust-version = "1.83" # MSRV:1
66
license.workspace = true

crates/cargo-test-support/src/cross_compile.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,33 @@ pub fn can_run_on_host() -> bool {
267267
return true;
268268
}
269269
}
270+
271+
/// Check if the given target has been installed.
272+
///
273+
/// Generally [`disabled`] should be used to check if cross-compilation is allowed.
274+
/// And [`alternate`] to get the cross target.
275+
///
276+
/// You should only use this as a last resort to skip tests,
277+
/// because it doesn't report skipped tests as ignored.
278+
pub fn requires_target_installed(target: &str) -> bool {
279+
let has_target = std::process::Command::new("rustup")
280+
.args(["target", "list", "--installed"])
281+
.output()
282+
.ok()
283+
.map(|output| {
284+
String::from_utf8(output.stdout)
285+
.map(|stdout| stdout.contains(target))
286+
.unwrap_or_default()
287+
})
288+
.unwrap_or_default();
289+
if !has_target {
290+
let msg =
291+
format!("to run this test, run `rustup target add {target} --toolchain <toolchain>`",);
292+
if cargo_util::is_ci() {
293+
panic!("{msg}");
294+
} else {
295+
eprintln!("{msg}");
296+
}
297+
}
298+
has_target
299+
}

tests/testsuite/standard_lib.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
use std::path::{Path, PathBuf};
88

9+
use cargo_test_support::cross_compile;
910
use cargo_test_support::prelude::*;
1011
use cargo_test_support::registry::{Dependency, Package};
1112
use cargo_test_support::ProjectBuilder;
@@ -391,24 +392,8 @@ fn check_core() {
391392

392393
#[cargo_test(build_std_mock)]
393394
fn build_std_with_no_arg_for_core_only_target() {
394-
let has_rustup_aarch64_unknown_none = std::process::Command::new("rustup")
395-
.args(["target", "list", "--installed"])
396-
.output()
397-
.ok()
398-
.map(|output| {
399-
String::from_utf8(output.stdout)
400-
.map(|stdout| stdout.contains("aarch64-unknown-none"))
401-
.unwrap_or_default()
402-
})
403-
.unwrap_or_default();
404-
if !has_rustup_aarch64_unknown_none {
405-
let msg =
406-
"to run this test, run `rustup target add aarch64-unknown-none --toolchain nightly`";
407-
if cargo_util::is_ci() {
408-
panic!("{msg}");
409-
} else {
410-
eprintln!("{msg}");
411-
}
395+
let target = "aarch64-unknown-none";
396+
if !cross_compile::requires_target_installed(target) {
412397
return;
413398
}
414399

@@ -427,7 +412,8 @@ fn build_std_with_no_arg_for_core_only_target() {
427412
.build();
428413

429414
p.cargo("build -v")
430-
.arg("--target=aarch64-unknown-none")
415+
.arg("--target")
416+
.arg(target)
431417
.build_std(&setup)
432418
.with_stderr_data(
433419
str![[r#"
@@ -457,7 +443,8 @@ fn build_std_with_no_arg_for_core_only_target() {
457443
// Note that we don't download std dependencies for the second call
458444
// because `-Zbuild-std` downloads them all also when building for core only.
459445
p.cargo("build -v")
460-
.arg("--target=aarch64-unknown-none")
446+
.arg("--target")
447+
.arg(target)
461448
.target_host()
462449
.build_std(&setup)
463450
.with_stderr_data(

0 commit comments

Comments
 (0)