//! Locate / build the maestro binaries needed by the harness. //! //! `cargo test -p maestro-e2e` does not, by default, rebuild bins from other //! workspace crates. So we re-invoke cargo from the first test that needs a //! binary and cache the resolved paths. The re-invocation acquires its own //! workspace lock — by the time tests start, the outer cargo has already //! released it. use std::path::{Path, PathBuf}; use std::sync::OnceLock; /// Absolute path of the workspace root (the dir containing the top-level /// `Cargo.toml` whose body has `[workspace]`). pub fn workspace_root() -> &'static Path { static ROOT: OnceLock = OnceLock::new(); ROOT.get_or_init(|| { let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR")); let mut cur: &Path = manifest.as_path(); loop { let candidate = cur.join("Cargo.toml"); if candidate.exists() { if let Ok(body) = std::fs::read_to_string(&candidate) { if body.contains("[workspace]") { return cur.to_path_buf(); } } } match cur.parent() { Some(p) => cur = p, None => panic!( "could not find workspace root starting from {}", PathBuf::from(env!("CARGO_MANIFEST_DIR")).display() ), } } }) } /// Resolve `target//` under the workspace root, after running /// `cargo build --bin -p ` to make sure it exists. pub fn workspace_target_bin(package: &str, bin: &str) -> PathBuf { static BUILDS: OnceLock>> = OnceLock::new(); let builds = BUILDS.get_or_init(|| std::sync::Mutex::new(Vec::new())); let already = { let lock = builds.lock().unwrap(); lock.iter().any(|b| b == bin) }; let release = !cfg!(debug_assertions); if !already { let mut cmd = std::process::Command::new(env!("CARGO")); cmd.args(["build", "--quiet", "-p", package, "--bin", bin]); if release { cmd.arg("--release"); } let status = cmd .current_dir(workspace_root()) .status() .unwrap_or_else(|e| panic!("failed to invoke cargo build for {bin}: {e}")); assert!(status.success(), "cargo build for `{bin}` failed"); builds.lock().unwrap().push(bin.to_string()); } let profile = if release { "release" } else { "debug" }; let path = workspace_root().join("target").join(profile).join(bin); assert!( path.exists(), "expected built binary at {} after `cargo build`", path.display() ); path }