nu_test_support/
commands.rs

1use std::{
2    io::Read,
3    process::{Command, Stdio},
4    sync::{
5        Mutex,
6        atomic::{AtomicBool, Ordering::Relaxed},
7    },
8};
9
10static CARGO_BUILD_LOCK: Mutex<()> = Mutex::new(());
11static PLUGINS_BUILT: AtomicBool = AtomicBool::new(false);
12
13// This runs `cargo build --package nu_plugin_*` to ensure that all plugins
14// have been built before plugin tests run. We use a lock to avoid multiple
15// simultaneous `cargo build` invocations clobbering each other.
16pub fn ensure_plugins_built() {
17    let _guard = CARGO_BUILD_LOCK.lock().expect("could not get mutex lock");
18
19    if PLUGINS_BUILT.load(Relaxed) {
20        return;
21    }
22
23    let cargo_path = env!("CARGO");
24    let mut arguments = vec![
25        "build",
26        "--workspace",
27        "--bins",
28        // Don't build nu, so that we only build the plugins
29        "--exclude",
30        "nu",
31        // Exclude nu_plugin_polars, because it's not needed at this stage, and is a large build
32        "--exclude",
33        "nu_plugin_polars",
34        "--quiet",
35    ];
36
37    let profile = std::env::var("NUSHELL_CARGO_PROFILE");
38    if let Ok(profile) = &profile {
39        arguments.push("--profile");
40        arguments.push(profile);
41    }
42
43    let mut command = Command::new(cargo_path)
44        .args(arguments)
45        .stdout(Stdio::piped())
46        .stderr(Stdio::piped())
47        .spawn()
48        .expect("Failed to spawn cargo build command");
49
50    let stderr = command.stderr.take();
51
52    let success = command
53        .wait()
54        .expect("failed to wait cargo build command")
55        .success();
56
57    if let Some(mut stderr) = stderr {
58        let mut buffer = String::new();
59        stderr
60            .read_to_string(&mut buffer)
61            .expect("failed to read cargo build stderr");
62        if !buffer.is_empty() {
63            println!("=== cargo build stderr\n{buffer}");
64        }
65    }
66
67    if !success {
68        panic!("cargo build failed");
69    }
70
71    PLUGINS_BUILT.store(true, Relaxed);
72}