use super::*;
fn test_config(cache_dir: &Path) -> Config {
Config {
cache_dir: cache_dir.to_path_buf(),
stats_report: None,
verify: false,
incremental: false,
share_out_dir: false,
events: false,
cc: false,
remote: Default::default(),
http: Default::default(),
gc: Default::default(),
scheduler: Default::default(),
target: crate::config::TargetSettings {
views: false,
root: cache_dir.join("targets"),
},
}
}
#[tokio::test]
async fn session_environment_directs_cargo_at_the_shim() {
let cache = tempfile::tempdir().unwrap();
let session_dir = tempfile::tempdir().unwrap();
let session = CacheSession::start(session_dir.path(), &test_config(cache.path()))
.await
.unwrap();
let workspace = tempfile::tempdir().unwrap();
let mut values = BTreeMap::from([("RUSTC_WRAPPER".into(), "existing".into())]);
let run = session
.begin(
workspace.path(),
&workspace.path().join("target"),
&["build".to_string()],
&mut values,
)
.await;
assert!(run.is_some());
assert!(values.contains_key(SOCKET_ENV));
assert!(values.contains_key(STAGING_ENV));
assert_eq!(values.get(BUILD_ENV).unwrap().len(), 64);
let wrapper = Path::new(values.get("RUSTC_WRAPPER").unwrap());
assert_eq!(wrapper.file_stem().unwrap(), RUSTC_SHIM_STEM);
assert_eq!(values.get(PREVIOUS_RUSTC_WRAPPER_ENV).unwrap(), "existing");
assert_eq!(values.get("CARGO_INCREMENTAL").unwrap(), "0");
assert_eq!(values.get(VERIFY_ENV).unwrap(), "0");
session.finish().await.unwrap();
}
#[cfg(unix)]
#[tokio::test]
async fn cc_shim_path_survives_and_is_reused_across_sessions() {
if resolve_on_path(CcLanguage::C.default_driver()).is_none() {
return;
}
let cache = tempfile::tempdir().unwrap();
let mut config = test_config(cache.path());
config.cc = true;
let first_path = {
let session_dir = tempfile::tempdir().unwrap();
let session = CacheSession::start(session_dir.path(), &config)
.await
.unwrap();
let path = session
.cc_shims
.as_ref()
.and_then(|shims| shims.cc.as_ref())
.map(|(shim, _)| shim.clone())
.unwrap();
session.finish().await.unwrap();
path
};
assert!(
first_path.is_file(),
"the compiler path cached by a build system must survive its mbx session"
);
let second_dir = tempfile::tempdir().unwrap();
let second = CacheSession::start(second_dir.path(), &config)
.await
.unwrap();
let second_path = second
.cc_shims
.as_ref()
.and_then(|shims| shims.cc.as_ref())
.map(|(shim, _)| shim.as_path())
.unwrap();
assert_eq!(
second_path, first_path,
"later sessions must reuse the compiler path a build system cached"
);
second.finish().await.unwrap();
}
#[cfg(unix)]
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn concurrent_sessions_can_install_shared_cc_shims() {
if resolve_on_path(CcLanguage::C.default_driver()).is_none() {
return;
}
let cache = tempfile::tempdir().unwrap();
let mut config = test_config(cache.path());
config.cc = true;
let barrier = Arc::new(tokio::sync::Barrier::new(8));
let mut starts = tokio::task::JoinSet::new();
for _ in 0..8 {
let config = config.clone();
let barrier = Arc::clone(&barrier);
starts.spawn(async move {
let session_dir = tempfile::tempdir().unwrap();
barrier.wait().await;
let session = CacheSession::start(session_dir.path(), &config).await?;
let shim = session
.cc_shims
.as_ref()
.and_then(|shims| shims.cc.as_ref())
.map(|(shim, _)| shim.clone())
.unwrap();
session.finish().await?;
Ok::<_, eyre::Report>(shim)
});
}
let mut installed = Vec::new();
while let Some(result) = starts.join_next().await {
installed.push(result.unwrap().unwrap());
}
assert_eq!(installed.len(), 8);
assert!(installed.iter().all(|shim| shim == &installed[0]));
assert!(installed[0].is_file());
}
#[tokio::test]
async fn incremental_builds_leave_cargo_incremental_alone() {
let cache = tempfile::tempdir().unwrap();
let session_dir = tempfile::tempdir().unwrap();
let mut config = test_config(cache.path());
config.incremental = true;
let session = CacheSession::start(session_dir.path(), &config)
.await
.unwrap();
let workspace = tempfile::tempdir().unwrap();
let mut values = BTreeMap::new();
session
.begin(
workspace.path(),
&workspace.path().join("target"),
&["build".to_string()],
&mut values,
)
.await;
assert!(!values.contains_key("CARGO_INCREMENTAL"));
session.finish().await.unwrap();
}
#[tokio::test]
async fn verify_mode_is_passed_to_the_shim() {
let cache = tempfile::tempdir().unwrap();
let session_dir = tempfile::tempdir().unwrap();
let mut config = test_config(cache.path());
config.verify = true;
let session = CacheSession::start(session_dir.path(), &config)
.await
.unwrap();
let workspace = tempfile::tempdir().unwrap();
let mut values = BTreeMap::new();
session
.begin(
workspace.path(),
&workspace.path().join("target"),
&["build".to_string()],
&mut values,
)
.await;
assert_eq!(values.get(VERIFY_ENV).unwrap(), "1");
session.finish().await.unwrap();
}
#[test]
fn identity_follows_the_dependency_graph_not_the_path() {
let first = tempfile::tempdir().unwrap();
let second = tempfile::tempdir().unwrap();
std::fs::write(first.path().join("Cargo.lock"), "version = 4\n").unwrap();
std::fs::write(second.path().join("Cargo.lock"), "version = 4\n").unwrap();
let command = ["build".to_string()];
assert_eq!(
build_identity(first.path(), &command),
build_identity(second.path(), &command),
"separate worktrees of one project must share a manifest"
);
std::fs::write(second.path().join("Cargo.lock"), "version = 3\n").unwrap();
assert_ne!(
build_identity(first.path(), &command),
build_identity(second.path(), &command),
);
assert_ne!(
build_identity(first.path(), &command),
build_identity(first.path(), &["test".to_string()]),
);
}
#[test]
fn identity_falls_back_to_the_directory_name() {
let directory = tempfile::tempdir().unwrap();
let command = ["build".to_string()];
assert_eq!(
build_identity(directory.path(), &command).len(),
64,
"a project without a lockfile still gets an identity"
);
}
#[test]
fn path_shim_names_select_their_language() {
assert_eq!(path_shim_language("cc"), Some(CcLanguage::C));
assert_eq!(path_shim_language("gcc"), Some(CcLanguage::C));
assert_eq!(path_shim_language("clang"), Some(CcLanguage::C));
assert_eq!(path_shim_language("c++"), Some(CcLanguage::Cxx));
assert_eq!(path_shim_language("g++"), Some(CcLanguage::Cxx));
assert_eq!(path_shim_language("clang++"), Some(CcLanguage::Cxx));
assert_eq!(path_shim_language("gcc-13"), None);
assert_eq!(path_shim_language("mbx"), None);
}
#[test]
fn exec_identity_is_shared_across_checkouts_with_one_lockfile() {
let first = tempfile::tempdir().unwrap();
let second = tempfile::tempdir().unwrap();
std::fs::write(first.path().join("Cargo.lock"), "version = 4\n").unwrap();
std::fs::write(second.path().join("Cargo.lock"), "version = 4\n").unwrap();
let command = ["make".to_string()];
assert_eq!(
exec_identity(first.path(), &command),
exec_identity(second.path(), &command),
"worktrees of one project must share a manifest for predictions to travel"
);
assert_ne!(
exec_identity(first.path(), &command),
exec_identity(first.path(), &["make".to_string(), "-j8".to_string()]),
);
}
#[test]
fn exec_identity_falls_back_to_the_directory_name() {
let directory = tempfile::tempdir().unwrap();
let command = ["make".to_string()];
assert_eq!(
exec_identity(directory.path(), &command).len(),
64,
"a project with no lockfile and no git origin still gets an identity"
);
}
#[cfg(unix)]
#[test]
fn a_shim_directory_never_supplies_the_real_compiler() {
let directory = tempfile::tempdir().unwrap();
let shims = directory.path().join("shims");
let real_dir = directory.path().join("bin");
std::fs::create_dir(&shims).unwrap();
std::fs::create_dir(&real_dir).unwrap();
let other_mbx = directory.path().join("other-mbx");
std::fs::write(&other_mbx, b"#!/bin/sh\n").unwrap();
std::os::unix::fs::symlink(&other_mbx, shims.join("cc")).unwrap();
let real_cc = real_dir.join("cc");
std::fs::write(&real_cc, b"#!/bin/sh\n").unwrap();
let running = directory.path().join("mbx");
std::fs::write(&running, b"#!/bin/sh\n").unwrap();
let path = std::env::join_paths([shims.as_path(), real_dir.as_path()]).unwrap();
let resolved = resolve_in_path(&path, "cc", &running, &shims);
assert_eq!(
resolved.map(|path| std::fs::canonicalize(path).unwrap()),
Some(std::fs::canonicalize(&real_cc).unwrap()),
"a shim must never be chosen as the compiler it stands in for"
);
}
#[cfg(unix)]
#[test]
fn a_hard_linked_shim_is_recognized_as_the_same_binary() {
let directory = tempfile::tempdir().unwrap();
let binary = directory.path().join("mbx");
std::fs::write(&binary, b"#!/bin/sh\n").unwrap();
let shim_dir = directory.path().join("cc-path");
std::fs::create_dir(&shim_dir).unwrap();
let linked = shim_dir.join("cc");
std::fs::hard_link(&binary, &linked).unwrap();
assert!(is_same_binary(&linked, Some(&binary)));
let copied = shim_dir.join("c++");
std::fs::copy(&binary, &copied).unwrap();
assert!(!is_same_binary(&copied, Some(&binary)));
}
#[test]
fn handshake_rejects_version_skew() {
let response = serde_json::to_string(&AgentResponse::Hello {
protocol: AGENT_PROTOCOL_VERSION,
agent_version: "another-version".into(),
})
.unwrap();
assert!(validate_handshake_response(&response).is_err());
}
fn agent_stats(fill: impl FnOnce(&mut AgentStats)) -> AgentStats {
let mut stats = AgentStats::default();
fill(&mut stats);
stats
}
#[test]
fn qualification_results_are_not_reported_as_misses() {
let stats = agent_stats(|stats| {
stats.lookups = 5;
stats.hits = 2;
stats.verifications = 2;
});
assert_eq!(cache_misses(&stats), 1);
}
#[test]
fn a_loaded_manifest_that_matched_nothing_is_called_out() {
let unmatched = agent_stats(|stats| {
stats.unconsulted = 255;
stats.predictions_loaded = 257;
});
assert!(stale_manifest_note(&unmatched).unwrap().contains("257"));
let cold = agent_stats(|stats| stats.unconsulted = 255);
assert_eq!(stale_manifest_note(&cold), None);
let live = agent_stats(|stats| {
stats.unconsulted = 2;
stats.lookups = 253;
stats.predictions_loaded = 257;
});
assert_eq!(stale_manifest_note(&live), None);
}
#[test]
fn compiler_only_sessions_are_reportable() {
let stats = agent_stats(|stats| {
stats.compiler =
BTreeMap::from([("bypass".into(), mbx_cache_core::CompilerStats::new(1, 42))]);
});
assert!(should_display_stats(&stats));
}
#[test]
fn a_session_that_only_failed_its_remote_is_reportable() {
let stats = agent_stats(|stats| stats.remote_failures = 3);
assert!(should_display_stats(&stats));
}
#[test]
fn writes_versioned_stats_report() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("nested").join("stats.json");
let stats = agent_stats(|stats| {
stats.session_duration_ns = 42;
stats.lookups = 5;
stats.hits = 2;
stats.verifications = 1;
stats.prefetched_actions = 3;
stats.downloaded_bytes = 1024;
stats.restored_output_files = 7;
stats.restored_output_bytes = 2048;
stats.reflinked_output_files = 5;
stats.reflinked_output_bytes = 1536;
stats.copied_output_files = 2;
stats.copied_output_bytes = 512;
stats.avoided_compiler_duration_ns = 2_000;
stats.compiler =
BTreeMap::from([("miss".into(), mbx_cache_core::CompilerStats::new(3, 4_000))]);
stats.slow_compilations = BTreeMap::from([("slow_crate".into(), 3_000)]);
stats.remote_blob_requests = 4;
stats.remote_blob_pack_requests = 2;
stats.remote_blob_pack_blobs = 100;
stats.materialization_duration_ns = 9;
stats.predictions_loaded = 11;
});
write_stats_report(&path, &stats).unwrap();
let report: serde_json::Value = serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
assert_eq!(report["version"], 4);
assert_eq!(report["predictions_loaded"], 11);
assert_eq!(report["session_duration_ns"], 42);
assert_eq!(report["hits"], 2);
assert_eq!(report["misses"], 2);
assert_eq!(report["compiler_invocations_avoided"], 2);
assert_eq!(report["estimated_compiler_duration_avoided_ns"], 2_000);
assert_eq!(report["compiler"]["miss"]["invocations"], 3);
assert_eq!(report["compiler"]["miss"]["duration_ns"], 4_000);
assert_eq!(report["slow_compilations"][0]["crate_name"], "slow_crate");
assert_eq!(report["slow_compilations"][0]["duration_ns"], 3_000);
assert_eq!(report["prefetched_actions"], 3);
assert_eq!(report["downloaded_bytes"], 1024);
assert_eq!(report["restored_output_files"], 7);
assert_eq!(report["restored_output_bytes"], 2048);
assert_eq!(report["reflinked_output_files"], 5);
assert_eq!(report["reflinked_output_bytes"], 1536);
assert_eq!(report["copied_output_files"], 2);
assert_eq!(report["copied_output_bytes"], 512);
assert_eq!(report["remote_blob_requests"], 4);
assert_eq!(report["remote_blob_pack_requests"], 2);
assert_eq!(report["remote_blob_pack_blobs"], 100);
assert_eq!(report["materialization_duration_ns"], 9);
}
#[test]
fn finds_crate_names_in_transparent_invocations() {
assert_eq!(
crate_name_argument(&["--crate-name".into(), "fixture".into()]),
Some("fixture".into())
);
assert_eq!(
crate_name_argument(&["--crate-name=attached".into()]),
Some("attached".into())
);
assert_eq!(crate_name_argument(&["--version".into()]), None);
}
#[test]
fn recognizes_the_bypassed_invocations_that_run_a_linker() {
for arguments in [
vec!["--crate-type", "bin"],
vec!["--crate-type=cdylib"],
vec!["--crate-type", "lib,dylib"],
vec!["--crate-type=proc-macro"],
vec!["--crate-type=staticlib"],
vec!["--test", "--crate-type=lib"],
vec!["--crate-type=bin", "--emit=dep-info,link"],
vec!["--test", "--emit", "link=/tmp/out"],
] {
let arguments: Vec<OsString> = arguments.iter().map(OsString::from).collect();
assert!(links_natively(&arguments), "{arguments:?} links");
}
for arguments in [
vec!["--crate-type", "lib"],
vec!["--crate-type=rlib"],
vec!["--crate-type", "lib,rlib"],
vec!["--emit=metadata"],
vec!["--crate-name", "bin"],
vec!["--crate-type=bin", "--emit=metadata"],
vec!["--test", "--emit", "dep-info,metadata"],
vec!["--crate-type=cdylib", "--emit=dep-info,metadata"],
] {
let arguments: Vec<OsString> = arguments.iter().map(OsString::from).collect();
assert!(!links_natively(&arguments), "{arguments:?} does not link");
}
}
#[cfg(unix)]
#[test]
fn the_session_shim_tracks_the_binary_it_was_installed_from() {
let directory = tempfile::tempdir().unwrap();
let executable = std::env::current_exe().unwrap();
let shim = install_shim(&executable, directory.path(), ShimLink::Tracking).unwrap();
let metadata = std::fs::symlink_metadata(&shim).unwrap();
assert!(
metadata.file_type().is_symlink(),
"the session shim should be a symlink, found {metadata:?}"
);
assert_eq!(std::fs::read_link(&shim).unwrap(), executable);
}
#[cfg(unix)]
#[test]
fn the_installed_wrapper_pins_the_bytes_it_was_made_from() {
let directory = tempfile::tempdir().unwrap();
let executable = std::env::current_exe().unwrap();
let shim = install_shim(&executable, directory.path(), ShimLink::Pinned).unwrap();
assert!(
!std::fs::symlink_metadata(&shim)
.unwrap()
.file_type()
.is_symlink()
);
assert_eq!(
std::fs::metadata(&shim).unwrap().len(),
std::fs::metadata(&executable).unwrap().len()
);
}
#[cfg(unix)]
#[test]
fn a_shim_is_never_installed_as_a_link_to_nothing() {
let directory = tempfile::tempdir().unwrap();
for missing in [directory.path().join("gone"), PathBuf::from("relative/mbx")] {
assert!(
install_shim(&missing, directory.path(), ShimLink::Tracking).is_err(),
"{} should not install a shim",
missing.display()
);
}
}
#[cfg(unix)]
#[test]
fn a_relative_target_is_resolved_before_it_is_linked() {
let directory = tempfile::tempdir().unwrap();
let shim = directory.path().join(RUSTC_SHIM_STEM);
assert!(symlink_shim(Path::new("Cargo.toml"), &shim));
let target = std::fs::read_link(&shim).unwrap();
assert!(
target.is_absolute(),
"{} should be absolute",
target.display()
);
assert_eq!(
std::fs::canonicalize(&shim).unwrap(),
std::fs::canonicalize("Cargo.toml").unwrap()
);
}
#[test]
fn a_missing_cpp_compiler_still_leaves_c_compilations_cached() {
let shims = CcShims {
cc: Some((
PathBuf::from("/session/mbx-cc"),
PathBuf::from("/usr/bin/cc"),
)),
cxx: None,
targeted: Vec::new(),
};
let mut environment = BTreeMap::new();
shims.apply_host(&mut environment);
assert_eq!(
environment.get("HOST_CC").map(String::as_str),
Some("/session/mbx-cc")
);
assert_eq!(
environment.get("MBX_REAL_CC").map(String::as_str),
Some("/usr/bin/cc")
);
assert!(!environment.contains_key("HOST_CXX"));
assert!(!environment.contains_key("MBX_REAL_CXX"));
}
#[test]
fn both_compilers_present_are_both_redirected() {
let shims = CcShims {
cc: Some((
PathBuf::from("/session/mbx-cc"),
PathBuf::from("/usr/bin/cc"),
)),
cxx: Some((
PathBuf::from("/session/mbx-cxx"),
PathBuf::from("/usr/bin/c++"),
)),
targeted: Vec::new(),
};
let mut environment = BTreeMap::new();
shims.apply_host(&mut environment);
for name in ["HOST_CC", "HOST_CXX", "MBX_REAL_CC", "MBX_REAL_CXX"] {
assert!(environment.contains_key(name), "{name} should be set");
}
}
#[test]
fn only_the_cc_crates_target_variables_name_a_cross_compiler() {
use CcLanguage::{C, Cxx};
for (variable, expected) in [
("TARGET_CC", Some(C)),
("TARGET_CXX", Some(Cxx)),
("CC_aarch64-unknown-linux-musl", Some(C)),
("CC_aarch64_unknown_linux_musl", Some(C)),
("CXX_aarch64-unknown-linux-musl", Some(Cxx)),
("CC", None),
("CXX", None),
("HOST_CC", None),
("CC_", None),
("CCACHE_DIR", None),
("CXXFLAGS", None),
("CC_FORCE_DISABLE", None),
("CC_KNOWN_WRAPPER_CUSTOM", None),
("CC_ENABLE_DEBUG_OUTPUT", None),
("CC_FOR_BUILD", None),
("CXX_FOR_BUILD", None),
("CC_gcc", None),
] {
assert_eq!(
targeted_compiler_language(variable).map(|l| format!("{l:?}")),
expected.map(|l| format!("{l:?}")),
"{variable}"
);
}
}
#[test]
fn a_compiler_that_is_already_a_shim_is_not_wrapped_again() {
let executable = std::env::current_exe().expect("current exe");
let shims = tempfile::tempdir().expect("tempdir");
let planted = shims.path().join("aarch64-linux-musl-gcc");
std::fs::write(&planted, "#!/bin/sh\nexit 0\n").expect("write shim");
assert_eq!(
resolve_named_compiler(&planted.display().to_string(), &executable, shims.path()),
None,
"a compiler inside the shim directory is a shim, not a compiler"
);
}
#[test]
fn a_cross_only_image_still_gets_its_named_compiler_wrapped() {
let shims = CcShims {
cc: None,
cxx: None,
targeted: vec![TargetedCompiler {
variable: "CC_aarch64-unknown-linux-musl".into(),
shim_name: "mbx-cc-cc_aarch64-unknown-linux-musl".into(),
shim: PathBuf::from("/session/mbx-cc-cc_aarch64-unknown-linux-musl"),
real: PathBuf::from("/usr/bin/aarch64-linux-musl-gcc"),
}],
};
let mut environment = BTreeMap::new();
shims.apply_host(&mut environment);
shims.apply_targeted(&mut environment);
assert!(!environment.contains_key("HOST_CC"));
assert_eq!(
environment
.get("CC_aarch64-unknown-linux-musl")
.map(String::as_str),
Some("/session/mbx-cc-cc_aarch64-unknown-linux-musl")
);
assert!(!shims.pins().is_empty());
}
#[test]
fn a_compiler_named_as_a_command_is_not_wrapped() {
let executable = std::env::current_exe().expect("current exe");
let shims = tempfile::tempdir().expect("tempdir");
for value in ["ccache gcc", "", " ", "cc -m32"] {
assert_eq!(
resolve_named_compiler(value, &executable, shims.path()),
None,
"{value:?} is not a single executable"
);
}
}
#[test]
fn naming_a_host_compiler_does_not_cost_the_cross_one_its_shim() {
let shims = CcShims {
cc: Some((
PathBuf::from("/session/mbx-cc"),
PathBuf::from("/usr/bin/cc"),
)),
cxx: None,
targeted: vec![TargetedCompiler {
variable: "CC_aarch64-unknown-linux-musl".into(),
shim_name: "mbx-cc-cc_aarch64-unknown-linux-musl".into(),
shim: PathBuf::from("/session/mbx-cc-cc_aarch64-unknown-linux-musl"),
real: PathBuf::from("/usr/bin/aarch64-linux-musl-gcc"),
}],
};
let mut environment = BTreeMap::new();
shims.apply_targeted(&mut environment);
assert_eq!(
environment
.get("CC_aarch64-unknown-linux-musl")
.map(String::as_str),
Some("/session/mbx-cc-cc_aarch64-unknown-linux-musl")
);
assert!(!environment.contains_key("HOST_CC"));
assert_eq!(
shims.pins().get("mbx-cc-cc_aarch64-unknown-linux-musl"),
Some(&PathBuf::from("/usr/bin/aarch64-linux-musl-gcc"))
);
}
#[test]
fn targeted_shim_names_dispatch_to_their_language() {
for (stem, expected) in [
("mbx-cc-cc_aarch64-unknown-linux-musl", "C"),
("mbx-cxx-cxx_aarch64-unknown-linux-musl", "Cxx"),
("mbx-cc-target_cc", "C"),
("mbx-cxx-target_cxx", "Cxx"),
] {
let language = if stem.starts_with("mbx-cxx-") {
CcLanguage::Cxx
} else {
CcLanguage::C
};
assert_eq!(format!("{language:?}"), expected, "{stem}");
}
}