Skip to content

Rework cargo-test-support & testsuite to use CARGO_BIN_EXE_* for Cargo #15692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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-macos-keychain = { version = "0.4.15", path = "credential/cargo
cargo-credential-wincred = { version = "0.4.15", path = "credential/cargo-credential-wincred" }
cargo-platform = { path = "crates/cargo-platform", version = "0.3.0" }
cargo-test-macro = { version = "0.4.4", path = "crates/cargo-test-macro" }
cargo-test-support = { version = "0.7.5", path = "crates/cargo-test-support" }
cargo-test-support = { version = "0.8.0", path = "crates/cargo-test-support" }
cargo-util = { version = "0.2.22", path = "crates/cargo-util" }
cargo-util-schemas = { version = "0.9.0", path = "crates/cargo-util-schemas" }
cargo_metadata = "0.19.1"
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-test-support/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-test-support"
version = "0.7.5"
version = "0.8.0"
edition.workspace = true
rust-version = "1.87" # MSRV:1
license.workspace = true
Expand Down
206 changes: 1 addition & 205 deletions crates/cargo-test-support/src/cross_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,189 +9,7 @@
//!
//! These tests are all disabled on rust-lang/rust's CI, but run in Cargo's CI.

use crate::{basic_manifest, main_file, project};
use cargo_util::ProcessError;
use std::env;
use std::fmt::Write;
use std::process::{Command, Output};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Once;

/// Whether or not the resulting cross binaries can run on the host.
static CAN_RUN_ON_HOST: AtomicBool = AtomicBool::new(false);

pub fn disabled() -> bool {
// First, disable if requested.
match env::var("CFG_DISABLE_CROSS_TESTS") {
Ok(ref s) if *s == "1" => return true,
_ => {}
}

// It requires setting `target.linker` for cross-compilation to work on aarch64,
// so not going to bother now.
if cfg!(all(target_arch = "aarch64", target_os = "linux")) {
return true;
}

// Cross tests are only tested to work on macos, linux, and MSVC windows.
if !(cfg!(target_os = "macos") || cfg!(target_os = "linux") || cfg!(target_env = "msvc")) {
return true;
}

// It's not particularly common to have a cross-compilation setup, so
// try to detect that before we fail a bunch of tests through no fault
// of the user.
static CAN_BUILD_CROSS_TESTS: AtomicBool = AtomicBool::new(false);
static CHECK: Once = Once::new();

let cross_target = alternate();

let run_cross_test = || -> anyhow::Result<Output> {
let p = project()
.at("cross_test")
.file("Cargo.toml", &basic_manifest("cross_test", "1.0.0"))
.file("src/main.rs", &main_file(r#""testing!""#, &[]))
.build();

let build_result = p
.cargo("build --target")
.arg(&cross_target)
.exec_with_output();

if build_result.is_ok() {
CAN_BUILD_CROSS_TESTS.store(true, Ordering::SeqCst);
}

let result = p
.cargo("run --target")
.arg(&cross_target)
.exec_with_output();

if result.is_ok() {
CAN_RUN_ON_HOST.store(true, Ordering::SeqCst);
}
build_result
};

CHECK.call_once(|| {
drop(run_cross_test());
});

if CAN_BUILD_CROSS_TESTS.load(Ordering::SeqCst) {
// We were able to compile a simple project, so the user has the
// necessary `std::` bits installed. Therefore, tests should not
// be disabled.
return false;
}

// We can't compile a simple cross project. We want to warn the user
// by failing a single test and having the remainder of the cross tests
// pass. We don't use `std::sync::Once` here because panicking inside its
// `call_once` method would poison the `Once` instance, which is not what
// we want.
static HAVE_WARNED: AtomicBool = AtomicBool::new(false);

if HAVE_WARNED.swap(true, Ordering::SeqCst) {
// We are some other test and somebody else is handling the warning.
// Just disable the current test.
return true;
}

// We are responsible for warning the user, which we do by panicking.
let mut message = format!(
"
Cannot cross compile to {}.

This failure can be safely ignored. If you would prefer to not see this
failure, you can set the environment variable CFG_DISABLE_CROSS_TESTS to \"1\".

Alternatively, you can install the necessary libraries to enable cross
compilation tests. Cross compilation tests depend on your host platform.
",
cross_target
);

if cfg!(target_os = "linux") {
message.push_str(
"
Linux cross tests target i686-unknown-linux-gnu, which requires the ability to
build and run 32-bit targets. This requires the 32-bit libraries to be
installed. For example, on Ubuntu, run `sudo apt install gcc-multilib` to
install the necessary libraries.
",
);
} else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
message.push_str(
"
macOS on aarch64 cross tests to target x86_64-apple-darwin.
This should be natively supported via Xcode, nothing additional besides the
rustup target should be needed.
",
);
} else if cfg!(target_os = "macos") {
message.push_str(
"
macOS on x86_64 cross tests to target x86_64-apple-ios, which requires the iOS
SDK to be installed. This should be included with Xcode automatically. If you
are using the Xcode command line tools, you'll need to install the full Xcode
app (from the Apple App Store), and switch to it with this command:

sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer

Some cross-tests want to *run* the executables on the host. These tests will
be ignored if this is not possible. On macOS, this means you need an iOS
simulator installed to run these tests. To install a simulator, open Xcode, go
to preferences > Components, and download the latest iOS simulator.
",
);
} else if cfg!(target_os = "windows") {
message.push_str(
"
Windows cross tests target i686-pc-windows-msvc, which requires the ability
to build and run 32-bit targets. This should work automatically if you have
properly installed Visual Studio build tools.
",
);
} else {
// The check at the top should prevent this.
panic!("platform should have been skipped");
}

let rustup_available = Command::new("rustup").output().is_ok();
if rustup_available {
write!(
message,
"
Make sure that the appropriate `rustc` target is installed with rustup:

rustup target add {}
",
cross_target
)
.unwrap();
} else {
write!(
message,
"
rustup does not appear to be installed. Make sure that the appropriate
`rustc` target is installed for the target `{}`.
",
cross_target
)
.unwrap();
}

// Show the actual error message.
match run_cross_test() {
Ok(_) => message.push_str("\nUh oh, second run succeeded?\n"),
Err(err) => match err.downcast_ref::<ProcessError>() {
Some(proc_err) => write!(message, "\nTest error: {}\n", proc_err).unwrap(),
None => write!(message, "\nUnexpected non-process error: {}\n", err).unwrap(),
},
}

panic!("{}", message);
}

/// The arch triple of the test-running host.
pub fn native() -> &'static str {
Expand Down Expand Up @@ -252,31 +70,9 @@ pub fn unused() -> &'static str {
"wasm32-unknown-unknown"
}

/// Whether or not the host can run cross-compiled executables.
pub fn can_run_on_host() -> bool {
if disabled() {
return false;
}
// macos is currently configured to cross compile to x86_64-apple-ios
// which requires a simulator to run. Azure's CI image appears to have the
// SDK installed, but are not configured to launch iOS images with a
// simulator.
if cfg!(target_os = "macos") {
if CAN_RUN_ON_HOST.load(Ordering::SeqCst) {
return true;
} else {
println!("Note: Cannot run on host, skipping.");
return false;
}
} else {
assert!(CAN_RUN_ON_HOST.load(Ordering::SeqCst));
return true;
}
}

/// Check if the given target has been installed.
///
/// Generally [`disabled`] should be used to check if cross-compilation is allowed.
/// Generally `testsuite::utils::cross_compile::disabled` should be used to check if cross-compilation is allowed.
/// And [`alternate`] to get the cross target.
///
/// You should only use this as a last resort to skip tests,
Expand Down
55 changes: 1 addition & 54 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,11 @@ pub mod install;
pub mod paths;
pub mod publish;
pub mod registry;
pub mod tools;

pub mod prelude {
pub use crate::cargo_test;
pub use crate::paths::CargoPathExt;
pub use crate::ArgLineCommandExt;
pub use crate::CargoCommandExt;
pub use crate::ChannelChangerCommandExt;
pub use crate::TestEnvCommandExt;
pub use snapbox::IntoData;
Expand Down Expand Up @@ -491,28 +489,6 @@ impl Project {
execs().with_process_builder(p)
}

/// Creates a `ProcessBuilder` to run cargo.
///
/// Arguments can be separated by spaces.
///
/// For `cargo run`, see [`Project::rename_run`].
///
/// # Example:
///
/// ```no_run
/// # let p = cargo_test_support::project().build();
/// p.cargo("build --bin foo").run();
/// ```
pub fn cargo(&self, cmd: &str) -> Execs {
let cargo = cargo_exe();
let mut execs = self.process(&cargo);
if let Some(ref mut p) = execs.process_builder {
p.env("CARGO", cargo);
p.arg_line(cmd);
}
execs
}

/// Safely run a process after `cargo build`.
///
/// Windows has a problem where a process cannot be reliably
Expand Down Expand Up @@ -621,11 +597,6 @@ pub fn main_file(println: &str, externed_deps: &[&str]) -> String {
buf
}

/// Path to the cargo binary
pub fn cargo_exe() -> PathBuf {
snapbox::cmd::cargo_bin("cargo")
}

/// This is the raw output from the process.
///
/// This is similar to `std::process::Output`, however the `status` is
Expand All @@ -643,8 +614,8 @@ pub struct RawOutput {
///
/// Construct with
/// - [`execs`]
/// - [`cargo_process`]
/// - [`Project`] methods
/// - `cargo_process` in testsuite
#[must_use]
#[derive(Clone)]
pub struct Execs {
Expand Down Expand Up @@ -1495,21 +1466,6 @@ impl TestEnvCommandExt for snapbox::cmd::Command {
}
}

/// Test the cargo command
pub trait CargoCommandExt {
fn cargo_ui() -> Self;
}

impl CargoCommandExt for snapbox::cmd::Command {
fn cargo_ui() -> Self {
Self::new(cargo_exe())
.with_assert(compare::assert_ui())
.env("CARGO_TERM_COLOR", "always")
.env("CARGO_TERM_HYPERLINKS", "true")
.test_env()
}
}

/// Add a list of arguments as a line
pub trait ArgLineCommandExt: Sized {
fn arg_line(mut self, s: &str) -> Self {
Expand Down Expand Up @@ -1547,15 +1503,6 @@ impl ArgLineCommandExt for snapbox::cmd::Command {
}
}

/// Run `cargo $arg_line`, see [`Execs`]
pub fn cargo_process(arg_line: &str) -> Execs {
let cargo = cargo_exe();
let mut p = process(&cargo);
p.env("CARGO", cargo);
p.arg_line(arg_line);
execs().with_process_builder(p)
}

/// Run `git $arg_line`, see [`ProcessBuilder`]
pub fn git_process(arg_line: &str) -> ProcessBuilder {
let mut p = process("git");
Expand Down
23 changes: 0 additions & 23 deletions crates/cargo-test-support/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,6 @@
//! # use cargo_test_support::registry::RegistryBuilder;
//! # use cargo_test_support::publish::validate_upload;
//! # use cargo_test_support::project;
//! // This replaces `registry::init()` and must be called before `Package::new().publish()`
//! let registry = RegistryBuilder::new().http_api().http_index().build();
//!
//! let p = project()
//! .file(
//! "Cargo.toml",
//! r#"
//! [package]
//! name = "foo"
//! version = "0.0.1"
//! edition = "2015"
//! authors = []
//! license = "MIT"
//! description = "foo"
//! "#,
//! )
//! .file("src/main.rs", "fn main() {}")
//! .build();
//!
//! p.cargo("publish --no-verify")
//! .replace_crates_io(registry.index_url())
//! .run();
//!
//! validate_upload(
//! r#"
//! {
Expand Down
3 changes: 2 additions & 1 deletion crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
//! "#)
//! .build();
//!
//! p.cargo("run").with_stdout_data(str!["24"]).run();
//! // Run a cargo command that interacts with the registry
//! // p.cargo("run").with_stdout_data(str!["24"]).run();
//! ```

use crate::git::repo;
Expand Down
Loading