use super::apply_cache_dirs;
use crate::snippets::session::ValidationSession;
use crate::snippets::types::Language;
use std::path::PathBuf;
#[test]
fn without_a_session_both_caches_are_scoped_to_the_snippet_directory() {
let root = tempfile::tempdir().expect("temporary root");
let mut command = std::process::Command::new("zig");
apply_cache_dirs(&mut command, root.path(), None);
let configured: Vec<_> = command
.get_envs()
.filter_map(|(key, value)| value.map(|value| (key.to_string_lossy().into_owned(), PathBuf::from(value))))
.collect();
assert_eq!(
configured,
vec![
("ZIG_GLOBAL_CACHE_DIR".to_string(), root.path().join("zig-global-cache")),
("ZIG_LOCAL_CACHE_DIR".to_string(), root.path().join("zig-local-cache")),
]
);
}
#[test]
fn with_a_session_apply_cache_dirs_leaves_the_global_cache_unset() {
let root = tempfile::tempdir().expect("temporary root");
let session = ValidationSession {
language: Language::Zig,
working_directory: root.path().to_path_buf(),
manifest: None,
fingerprint: "session-shared-global-cache-fixture".into(),
env: std::collections::BTreeMap::new(),
include_paths: Vec::new(),
rust_features: Vec::new(),
rust_dependencies: std::collections::BTreeMap::new(),
};
let mut command = std::process::Command::new("zig");
apply_cache_dirs(&mut command, root.path(), Some(&session));
let configured: std::collections::BTreeMap<String, PathBuf> = command
.get_envs()
.filter_map(|(key, value)| value.map(|value| (key.to_string_lossy().into_owned(), PathBuf::from(value))))
.collect();
assert!(
!configured.contains_key("ZIG_GLOBAL_CACHE_DIR"),
"apply_cache_dirs must not set ZIG_GLOBAL_CACHE_DIR when a session is present: {configured:?}"
);
assert_eq!(
configured.get("ZIG_LOCAL_CACHE_DIR"),
Some(&root.path().join("zig-local-cache"))
);
session.apply(&mut command);
let after_session_apply: std::collections::BTreeMap<String, PathBuf> = command
.get_envs()
.filter_map(|(key, value)| value.map(|value| (key.to_string_lossy().into_owned(), PathBuf::from(value))))
.collect();
let shared_global_cache = after_session_apply
.get("ZIG_GLOBAL_CACHE_DIR")
.expect("session.apply must set ZIG_GLOBAL_CACHE_DIR");
assert!(
shared_global_cache.starts_with(
root.path()
.join(".alef/snippets/cache")
.join(session.toolchain_cache_key())
),
"the global cache must be the session's toolchain-key-scoped, session-shared directory, not a \
scratch directory: {}",
shared_global_cache.display()
);
}