nushell/crates/nu-test-support/src/commands.rs
Skyler Hawthorne 9a4dad6ca1
Fix unit tests on Android (#10224)
# Description

* The path to the binaries for tests is slightly incorrect. It is
missing the build target when it is set with the `CARGO_BUILD_TARGET`
environment variable. For example, when `CARGO_BUILD_TARGET` is set to
`aarch64-linux-android`, the path to the `nu` binary is:

  `./target/aarch64-linux-android/debug/nu`

  rather than

  `./target/debug/nu`

This is common on Termux since the default target that rustc detects can
cause problems on some projects, such as [python's `cryptography`
package](https://github.com/pyca/cryptography/issues/7248).
  
This technically isn't a problem specific to Android, but is more likely
to happen on Android due to the latter.
* Additionally, the existing variable named `NUSHELL_CARGO_TARGET` is in
fact the profile, not the build target, so this was renamed to
`NUSHELL_CARGO_PROFILE`. This change is included because without the
rename, the build system would be using `CARGO_BUILD_TARGET` for the
build target and `NUSHELL_CARGO_TARGET` for the build profile, which is
confusing.
* `std path add` tests were missing `android` test

# User-Facing Changes

For those who would like to build nushell on Termux, the unit tests will
pass now.
2023-09-05 20:17:34 +12:00

61 lines
1.7 KiB
Rust

use std::{
io::Read,
process::{Command, Stdio},
sync::{
atomic::{AtomicBool, Ordering::Relaxed},
Mutex,
},
};
static CARGO_BUILD_LOCK: Mutex<()> = Mutex::new(());
static PLUGINS_BUILT: AtomicBool = AtomicBool::new(false);
// This runs `cargo build --package nu_plugin_*` to ensure that all plugins
// have been built before plugin tests run. We use a lock to avoid multiple
// simultaneous `cargo build` invocations clobbering each other.
pub fn ensure_plugins_built() {
let _guard = CARGO_BUILD_LOCK.lock().expect("could not get mutex lock");
if PLUGINS_BUILT.load(Relaxed) {
return;
}
let cargo_path = env!("CARGO");
let mut arguments = vec!["build", "--package", "nu_plugin_*", "--quiet"];
let profile = std::env::var("NUSHELL_CARGO_PROFILE");
if let Ok(profile) = &profile {
arguments.push("--profile");
arguments.push(profile);
}
let mut command = Command::new(cargo_path)
.args(arguments)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("Failed to spawn cargo build command");
let stderr = command.stderr.take();
let success = command
.wait()
.expect("failed to wait cargo build command")
.success();
if let Some(mut stderr) = stderr {
let mut buffer = String::new();
stderr
.read_to_string(&mut buffer)
.expect("failed to read cargo build stderr");
if !buffer.is_empty() {
println!("=== cargo build stderr\n{buffer}");
}
}
if !success {
panic!("cargo build failed");
}
PLUGINS_BUILT.store(true, Relaxed);
}