use super::*;
use std::fs;
fn two_units(root: &std::path::Path, hash_lib: u64, hash_test: u64) -> WorkspaceIndex {
let crate_dir = root.join("shop");
fs::create_dir_all(crate_dir.join("src")).unwrap();
fs::write(crate_dir.join("src/lib.rs"), "pub fn a() {}\n").unwrap();
let var_dir = root.join("target/ridx/variants/v1");
fs::create_dir_all(var_dir.join(".deps")).unwrap();
for (i, (unit, hash)) in [("uk_lib", hash_lib), ("uk_test", hash_test)].into_iter().enumerate() {
let mut w = IndexWriter::new("shop", "0.1.0");
w.set_unit_key(unit);
w.get_or_insert_file("src/lib.rs");
w.add_file_hash("src/lib.rs", hash);
let object = var_dir.join(format!("0{i}_shop.ridx"));
w.write_to(&mut File::create(&object).unwrap()).unwrap();
let sidecar = serde_json::json!({ "name": "shop", "version": "0.1.0", "unit_key": unit, "object_path": object.display().to_string(),
"source_root": crate_dir.display().to_string(), "is_dependency": false, "file_hashes": { "src/lib.rs": hash } });
fs::write(var_dir.join(format!(".deps/0{i}_shop.json")), sidecar.to_string()).unwrap();
}
let manifest = serde_json::json!({ "version": "2.0", "format_version": crate::format::CURRENT_FORMAT_VERSION, "extractor_version": crate::format::EXTRACTOR_VERSION,
"active_variant_id": "v1", "variant_dir": "variants/v1", "workspace_root": root.display().to_string(), "dependency_shards": [] });
fs::write(root.join("target/ridx/ridx_manifest.json"), manifest.to_string()).unwrap();
WorkspaceIndex::open(root).unwrap()
}
#[test]
fn test_units_agree_on_source() {
let agree = tempfile::tempdir().unwrap();
let ws = two_units(agree.path(), 0xAAAA, 0xAAAA);
let file = agree.path().join("shop/src/lib.rs").display().to_string();
assert_eq!(ws.source_hash_for_file(&file), Some(0xAAAA));
assert!(ws.units_agree_on_source(&file));
let split = tempfile::tempdir().unwrap();
let ws = two_units(split.path(), 0xAAAA, 0xBBBB);
let file = split.path().join("shop/src/lib.rs").display().to_string();
assert!(ws.source_hash_for_file(&file).is_some());
assert!(!ws.units_agree_on_source(&file), "one unit holds the text before");
}
struct Unit<'a> {
key: &'a str,
krate: &'a str,
shard: &'a [(&'a str, u64)],
hashes: serde_json::Value,
own: Vec<String>,
}
fn name_of<'a>(u: &Unit<'a>) -> &'a str {
if u.krate == "." {
"w"
} else {
u.krate
}
}
fn dir_of(root: &std::path::Path, u: &Unit) -> std::path::PathBuf {
if u.krate == "." {
root.to_path_buf()
} else {
root.join(u.krate)
}
}
fn sidecar_of(root: &std::path::Path, i: usize, u: &Unit) -> (std::path::PathBuf, serde_json::Value) {
let var_dir = root.join("target/ridx/variants/v1");
let object = var_dir.join(format!("0{i}_{}.ridx", name_of(u)));
let sidecar = serde_json::json!({ "name": name_of(u), "version": "0.1.0", "unit_key": u.key, "object_path": object.display().to_string(),
"source_root": dir_of(root, u).display().to_string(), "is_dependency": false, "file_hashes": u.hashes, "own_sources": u.own });
(var_dir.join(format!(".deps/0{i}_{}.json", name_of(u))), sidecar)
}
fn units(root: &std::path::Path, units: &[Unit]) -> WorkspaceIndex {
let var_dir = root.join("target/ridx/variants/v1");
fs::create_dir_all(var_dir.join(".deps")).unwrap();
for (i, u) in units.iter().enumerate() {
fs::create_dir_all(dir_of(root, u).join("src")).unwrap();
let mut w = IndexWriter::new(name_of(u), "0.1.0");
w.set_unit_key(u.key);
for (f, h) in u.shard {
w.get_or_insert_file(f);
w.add_file_hash(f, *h);
}
w.write_to(&mut File::create(var_dir.join(format!("0{i}_{}.ridx", name_of(u)))).unwrap()).unwrap();
let (path, sidecar) = sidecar_of(root, i, u);
fs::write(path, sidecar.to_string()).unwrap();
}
let manifest = serde_json::json!({ "version": "2.0", "format_version": crate::format::CURRENT_FORMAT_VERSION, "extractor_version": crate::format::EXTRACTOR_VERSION,
"active_variant_id": "v1", "variant_dir": "variants/v1", "workspace_root": root.display().to_string(), "dependency_shards": [] });
fs::write(root.join("target/ridx/ridx_manifest.json"), manifest.to_string()).unwrap();
WorkspaceIndex::open(root).unwrap()
}
#[test]
fn test_ownership_comes_from_the_loaded_sidecar() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
fs::create_dir_all(root.join("shop/src")).unwrap();
fs::write(root.join("shop/src/gen.rs"), "pub fn g() {}\n").unwrap();
let gen = root.join("shop/src/gen.rs").display().to_string();
let app = Unit {
key: "uk_app",
krate: "app",
shard: &[("src/main.rs", 0x11)],
hashes: serde_json::json!({ (gen.clone()): 0xBBBB }),
own: vec![root.join("app/src/main.rs").display().to_string()],
};
let shop = Unit { key: "uk_shop", krate: "shop", shard: &[("src/gen.rs", 0xAAAA)], hashes: serde_json::json!({ "src/gen.rs": 0xAAAA }), own: vec![gen.clone()] };
let ws = units(root, &[app, shop]);
assert!(ws.units_agree_on_source(&gen), "a hash read through a dependency is no unit's text of the file");
let rewritten = Unit { key: "uk_app", krate: "app", shard: &[], hashes: serde_json::json!({ (gen.clone()): 0xBBBB }), own: vec![gen.clone()] };
let (path, sidecar) = sidecar_of(root, 0, &rewritten);
std::thread::sleep(std::time::Duration::from_millis(20));
fs::write(path, sidecar.to_string()).unwrap();
assert!(ws.units_agree_on_source(&gen), "the sidecar on disk now is not the one loaded");
}
#[test]
fn test_a_relative_sidecar_key_resolves_against_its_reader_root() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
fs::create_dir_all(root.join("shop/src")).unwrap();
fs::write(root.join("shop/src/gen.rs"), "pub fn g() {}\n").unwrap();
let gen = root.join("shop/src/gen.rs").display().to_string();
let lib = Unit { key: "uk_lib", krate: "shop", shard: &[("src/gen.rs", 0xAAAA)], hashes: serde_json::json!({ "src/gen.rs": 0xAAAA }), own: vec![gen.clone()] };
let test = Unit { key: "uk_test", krate: "shop", shard: &[("src/lib.rs", 0x22)], hashes: serde_json::json!({ "src/gen.rs": 0xBBBB }), own: vec![gen.clone()] };
let ws = units(root, &[lib, test]);
assert!(!ws.units_agree_on_source(&gen), "lib test holds the text before");
}
#[test]
fn test_one_alias_does_not_prove_ownership_of_another() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
fs::create_dir_all(root.join("src")).unwrap();
fs::create_dir_all(root.join("member/src")).unwrap();
fs::write(root.join("src/gen.rs"), "pub fn g() {}\n").unwrap();
fs::write(root.join("member/src/gen.rs"), "pub fn m() {}\n").unwrap();
let gen = root.join("src/gen.rs").display().to_string();
let own_member = root.join("member/src/gen.rs").display().to_string();
let a = Unit { key: "uk_w", krate: ".", shard: &[("src/gen.rs", 0x111)], hashes: serde_json::json!({ "src/gen.rs": 0x111 }), own: vec![gen.clone()] };
let b =
Unit { key: "uk_member", krate: "member", shard: &[("src/lib.rs", 0x3)], hashes: serde_json::json!({ (gen.clone()): 0x222, "src/gen.rs": 0x333 }), own: vec![own_member] };
let ws = units(root, &[a, b]);
assert!(ws.units_agree_on_source(&gen), "member only imported W/src/gen.rs");
assert!(ws.units_agree_on_source("src/gen.rs"), "the relative query names the same file");
}
#[cfg(unix)]
#[test]
fn test_a_symlinked_query_finds_a_relative_only_hash() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
fs::create_dir_all(root.join("shop/src")).unwrap();
fs::write(root.join("shop/src/gen.rs"), "pub fn g() {}\n").unwrap();
std::os::unix::fs::symlink(root.join("shop/src"), root.join("alias")).unwrap();
let gen = root.join("shop/src/gen.rs").display().to_string();
let a = Unit { key: "uk_a", krate: "shop", shard: &[("src/lib.rs", 0x1)], hashes: serde_json::json!({ (gen.clone()): 0x111 }), own: vec![gen.clone()] };
let b = Unit { key: "uk_b", krate: "shop", shard: &[("src/lib.rs", 0x2)], hashes: serde_json::json!({ "src/gen.rs": 0x222 }), own: vec![gen.clone()] };
let ws = units(root, &[a, b]);
assert!(!ws.units_agree_on_source(&gen), "asked directly");
assert!(!ws.units_agree_on_source(&root.join("alias/gen.rs").display().to_string()), "asked through the symlink");
}
#[test]
fn test_a_workspace_relative_routed_key_keeps_its_owner() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
fs::create_dir_all(root.join("shop/src")).unwrap();
fs::write(root.join("shop/src/gen.rs"), "pub fn g() {}\n").unwrap();
let gen = root.join("shop/src/gen.rs").display().to_string();
let a = Unit { key: "uk_a", krate: "shop", shard: &[("shop/src/gen.rs", 0x111)], hashes: serde_json::json!({}), own: vec![gen.clone()] };
let b = Unit { key: "uk_b", krate: "shop", shard: &[("shop/src/gen.rs", 0)], hashes: serde_json::json!({ "shop/src/gen.rs": 0x222 }), own: vec![gen.clone()] };
let ws = units(root, &[a, b]);
assert_eq!(ws.target_file_for_reader(1, &gen).as_deref(), Some("shop/src/gen.rs"));
assert!(!ws.units_agree_on_source(&gen), "B holds another text");
}
#[cfg(unix)]
#[test]
fn test_the_query_spelling_survives_canonical_routing() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
fs::create_dir_all(root.join("shop/src")).unwrap();
fs::write(root.join("shop/src/gen.rs"), "pub fn g() {}\n").unwrap();
std::os::unix::fs::symlink(root.join("shop/src"), root.join("alias")).unwrap();
let canonical = fs::canonicalize(root.join("shop/src/gen.rs")).unwrap().display().to_string();
let alias = root.join("alias/gen.rs").display().to_string();
let entry: &[(&str, u64)] = &[(canonical.as_str(), 0x111)];
let zero: &[(&str, u64)] = &[(canonical.as_str(), 0)];
let a = Unit { key: "uk_a", krate: "shop", shard: entry, hashes: serde_json::json!({}), own: vec![canonical.clone()] };
let b = Unit { key: "uk_b", krate: "shop", shard: zero, hashes: serde_json::json!({ (alias.clone()): 0x222 }), own: vec![canonical.clone()] };
let ws = units(root, &[a, b]);
assert!(!ws.units_agree_on_source(&alias), "B holds another text");
}
#[cfg(unix)]
#[test]
fn test_a_canonical_relative_route_keeps_the_reader_root() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
fs::create_dir_all(root.join("shop/src")).unwrap();
fs::write(root.join("shop/src/gen.rs"), "pub fn g() {}\n").unwrap();
std::os::unix::fs::symlink(root.join("shop/src"), root.join("shop/alias")).unwrap();
let canonical = fs::canonicalize(root.join("shop/src/gen.rs")).unwrap().display().to_string();
let a = Unit { key: "uk_a", krate: "shop", shard: &[("src/gen.rs", 0x111)], hashes: serde_json::json!({}), own: vec![canonical.clone()] };
let b = Unit { key: "uk_b", krate: "shop", shard: &[("src/gen.rs", 0)], hashes: serde_json::json!({ "src/gen.rs": 0x222 }), own: vec![canonical.clone()] };
let ws = units(root, &[a, b]);
assert!(!root.join("alias").exists());
assert_eq!(ws.target_file_for_reader(1, "alias/gen.rs").as_deref(), Some("src/gen.rs"));
assert!(!ws.units_agree_on_source("alias/gen.rs"), "B holds another text");
}
#[cfg(unix)]
#[test]
fn test_a_relative_recorded_root_lies_under_the_workspace() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
fs::create_dir_all(root.join("shop/src")).unwrap();
fs::write(root.join("shop/src/gen.rs"), "pub fn g() {}\n").unwrap();
std::os::unix::fs::symlink(root.join("shop/src"), root.join("shop/alias")).unwrap();
let canonical = fs::canonicalize(root.join("shop/src/gen.rs")).unwrap().display().to_string();
let a = Unit { key: "uk_a", krate: "shop", shard: &[("src/gen.rs", 0x111)], hashes: serde_json::json!({}), own: vec![canonical.clone()] };
let b = Unit { key: "uk_b", krate: "shop", shard: &[("src/gen.rs", 0)], hashes: serde_json::json!({ "src/gen.rs": 0x222 }), own: vec![canonical.clone()] };
drop(units(root, &[a, b]));
for (i, u) in [
Unit { key: "uk_a", krate: "shop", shard: &[], hashes: serde_json::json!({}), own: vec![canonical.clone()] },
Unit { key: "uk_b", krate: "shop", shard: &[], hashes: serde_json::json!({ "src/gen.rs": 0x222 }), own: vec![canonical.clone()] },
]
.iter()
.enumerate()
{
let (path, mut sidecar) = sidecar_of(root, i, u);
sidecar["source_root"] = "shop".into();
fs::write(path, sidecar.to_string()).unwrap();
}
let ws = WorkspaceIndex::open(root).unwrap();
assert!(!std::env::current_dir().unwrap().starts_with(root));
assert_eq!(ws.reader_root(1), Some(Path::new("shop")));
assert_eq!(ws.target_file_for_reader(1, "alias/gen.rs").as_deref(), Some("src/gen.rs"));
assert!(!ws.units_agree_on_source("alias/gen.rs"), "B holds another text");
}