512 lines
18 KiB
Rust
512 lines
18 KiB
Rust
use std::{
|
|
collections::{BTreeMap, BTreeSet},
|
|
fs,
|
|
path::{Path, PathBuf},
|
|
sync::{
|
|
Arc,
|
|
atomic::{AtomicU64, Ordering},
|
|
},
|
|
};
|
|
use texrra_tex_core::resources::*;
|
|
fn directory() -> PathBuf {
|
|
static N: AtomicU64 = AtomicU64::new(0);
|
|
let p = std::env::temp_dir().join(format!(
|
|
"texrra-project-view-{}-{}",
|
|
std::process::id(),
|
|
N.fetch_add(1, Ordering::Relaxed)
|
|
));
|
|
fs::create_dir_all(&p).unwrap();
|
|
p
|
|
}
|
|
fn definition(p: &Path) -> ProjectDefinition {
|
|
ProjectDefinition {
|
|
configuration: WorldConfig::new(p),
|
|
revision: 1,
|
|
roots: BTreeSet::from([p.into()]),
|
|
files: BTreeMap::new(),
|
|
directories: BTreeSet::new(),
|
|
aliases: BTreeMap::new(),
|
|
generated_roots: BTreeSet::from([p.into()]),
|
|
generated: BTreeSet::new(),
|
|
}
|
|
}
|
|
fn file(d: &mut ProjectDefinition, path: PathBuf, data: &[u8]) {
|
|
d.files.insert(path, Arc::from(data));
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn casefolded_project_lookups_use_the_current_immutable_namespace() {
|
|
let root = directory().canonicalize().unwrap();
|
|
let requested = "chapter.tex";
|
|
let upper = root.join("CHAPTER.TEX");
|
|
let exact = root.join(requested);
|
|
let entry = root.join("main.tex");
|
|
let mut seed = definition(&root);
|
|
file(&mut seed, upper.clone(), b"initial buffer");
|
|
let snapshot = ProjectSnapshot::new(seed).unwrap();
|
|
let mut first = FileWorld::from_project(snapshot.clone()).unwrap();
|
|
assert!(!upper.exists());
|
|
assert_eq!(first.read_tex_input(requested, &entry).unwrap().version.path, upper);
|
|
|
|
let edited = snapshot.with_file_changes(
|
|
2, [(exact.clone(), Some(Arc::from(b"exact buffer".as_slice())))].into()
|
|
).unwrap();
|
|
let mut next = FileWorld::from_project(edited).unwrap();
|
|
assert_eq!(next.read_tex_input(requested, &entry).unwrap().version.path, exact);
|
|
assert_eq!(first.read_tex_input(requested, &entry).unwrap().version.path, upper);
|
|
fs::remove_dir_all(&root).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn immutable_buffer_edits_match_a_rebuilt_project_and_revalidate_imports() {
|
|
let p = directory();
|
|
let main = p.join("main.tex");
|
|
let font = p.join("font.tfm");
|
|
let output = p.join("job.aux");
|
|
let alias = p.join("entry");
|
|
let mut definition = definition(&p);
|
|
file(&mut definition, main.clone(), b"old");
|
|
file(&mut definition, font.clone(), b"untouched");
|
|
definition.generated.insert(output.clone());
|
|
definition.aliases.insert(alias.clone(), "main.tex".into());
|
|
let times = BTreeMap::from([(
|
|
main.clone(),
|
|
FileModificationTime {
|
|
unix_seconds: 1626300000,
|
|
nanoseconds: 7,
|
|
},
|
|
)]);
|
|
let original =
|
|
ProjectSnapshot::with_file_modifications(definition.clone(), times.clone()).unwrap();
|
|
let next = original
|
|
.with_file_edits(
|
|
2,
|
|
BTreeMap::from([(main.clone(), Arc::from(b"new".as_slice()))]),
|
|
)
|
|
.unwrap();
|
|
definition.revision = 2;
|
|
file(&mut definition, main.clone(), b"new");
|
|
let rebuilt = ProjectSnapshot::with_file_modifications(definition, times).unwrap();
|
|
assert_eq!(next.version(), rebuilt.version());
|
|
assert_eq!(
|
|
next.content_descriptor().unwrap(),
|
|
rebuilt.content_descriptor().unwrap()
|
|
);
|
|
assert_eq!(&*original.file(&main).unwrap().data, b"old");
|
|
assert!(Arc::ptr_eq(
|
|
&next.file(&font).unwrap().data,
|
|
&original.file(&font).unwrap().data
|
|
));
|
|
let mut world = FileWorld::from_project(next.clone()).unwrap();
|
|
assert_eq!(
|
|
world.read(ResourceKind::Tex, "entry", &main).unwrap().data,
|
|
b"new"
|
|
);
|
|
for path in [alias, output, p.join("unlisted.tex")] {
|
|
assert!(
|
|
original
|
|
.with_file_edits(3, BTreeMap::from([(path, Arc::from(b"bad".as_slice()))]))
|
|
.is_err()
|
|
);
|
|
}
|
|
let mut wire = serde_json::to_value(&*next).unwrap();
|
|
let recovered: ProjectSnapshot = serde_json::from_value(wire.clone()).unwrap();
|
|
recovered.validate().unwrap();
|
|
wire["files"][main.to_str().unwrap()]["data"] = serde_json::json!([111, 108, 100]);
|
|
wire["validated"] = serde_json::json!({"Ok": null});
|
|
let forged: ProjectSnapshot = serde_json::from_value(wire).unwrap();
|
|
assert!(
|
|
forged
|
|
.validate()
|
|
.unwrap_err()
|
|
.contains("invalid atomic project file")
|
|
);
|
|
assert!(forged.with_file_edits(3, BTreeMap::new()).is_err());
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn atomic_batch_unsaved_buffers_known_absence_and_project_identity_do_not_read_disk() {
|
|
let p = directory();
|
|
fs::write(p.join("main.tex"), b"PHYSICAL").unwrap();
|
|
fs::write(p.join("optional.tex"), b"EXISTS ONLY ON DISK").unwrap();
|
|
let mut d = definition(&p);
|
|
file(&mut d, p.join("main.tex"), b"UNSAVED");
|
|
d.aliases.insert(p.join("entry"), "main.tex".into());
|
|
d.generated.insert(p.join("owned.dat"));
|
|
fs::write(p.join("owned.dat"), b"LIVE").unwrap();
|
|
let first = ProjectSnapshot::new(d.clone()).unwrap();
|
|
let first_version = first.version().clone();
|
|
let mut old = FileWorld::from_project(first.clone()).unwrap();
|
|
assert_eq!(
|
|
old.read(ResourceKind::Tex, "main", &p.join("main.tex"))
|
|
.unwrap()
|
|
.data,
|
|
b"UNSAVED"
|
|
);
|
|
assert!(
|
|
old.read_optional(ResourceKind::Tex, "optional", &p.join("main.tex"))
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
fs::remove_file(p.join("main.tex")).unwrap();
|
|
d.revision = 2;
|
|
file(&mut d, p.join("main.tex"), b"SECOND");
|
|
file(&mut d, p.join("optional.tex"), b"VIRTUAL NEW FILE");
|
|
let second = ProjectSnapshot::new(d.clone()).unwrap();
|
|
d.revision = 3;
|
|
let third = ProjectSnapshot::new(d).unwrap();
|
|
assert_ne!(first_version.sha256, second.version().sha256);
|
|
assert_eq!(second.version().sha256, third.version().sha256);
|
|
assert_eq!(
|
|
old.read(ResourceKind::Tex, "main", &p.join("main.tex"))
|
|
.unwrap()
|
|
.data,
|
|
b"UNSAVED"
|
|
);
|
|
let mut current = FileWorld::from_project(second).unwrap();
|
|
assert_eq!(
|
|
current
|
|
.read(ResourceKind::Tex, "main", &p.join("main.tex"))
|
|
.unwrap()
|
|
.data,
|
|
b"SECOND"
|
|
);
|
|
assert_eq!(
|
|
current
|
|
.read(ResourceKind::Tex, "optional", &p.join("main.tex"))
|
|
.unwrap()
|
|
.data,
|
|
b"VIRTUAL NEW FILE"
|
|
);
|
|
assert_eq!(old.trace().project_version, Some(first_version));
|
|
assert!(old.trace().configuration_observations.is_empty());
|
|
let snapshot = old.input_snapshot();
|
|
assert_eq!(snapshot.files().count(), 1);
|
|
let encoded = serde_json::to_vec(&*snapshot).unwrap();
|
|
let saved: InputSnapshot = serde_json::from_slice(&encoded).unwrap();
|
|
let mut replay = FileWorld::from_snapshot(Arc::new(saved)).unwrap();
|
|
assert_eq!(
|
|
replay
|
|
.read(ResourceKind::Tex, "main", &p.join("main.tex"))
|
|
.unwrap()
|
|
.data,
|
|
b"UNSAVED"
|
|
);
|
|
assert!(
|
|
replay
|
|
.read_optional(ResourceKind::Tex, "optional", &p.join("main.tex"))
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
// A serde-loaded capture must not override a valid project identity with
|
|
// independently hashed bytes, an error, or a file outside its roles.
|
|
let original = serde_json::to_value(&*snapshot).unwrap();
|
|
let shadow = current
|
|
.read(ResourceKind::Tex, "main", &p.join("main.tex"))
|
|
.unwrap();
|
|
for value in [
|
|
serde_json::json!({"Ok":{"version":shadow.version,"data":shadow.data}}),
|
|
serde_json::json!({"Err":"injected external error"}),
|
|
] {
|
|
let mut bad = original.clone();
|
|
bad["files"][p.join("main.tex").to_str().unwrap()] = value;
|
|
let bad: InputSnapshot = serde_json::from_value(bad).unwrap();
|
|
assert!(FileWorld::from_snapshot(Arc::new(bad)).is_err());
|
|
}
|
|
let mut outside = original.clone();
|
|
let mut extra = serde_json::json!({"Ok":{"version":shadow.version,"data":shadow.data}});
|
|
let outside_path = p.with_extension("outside");
|
|
extra["Ok"]["version"]["path"] = serde_json::json!(outside_path);
|
|
outside["files"][outside_path.to_str().unwrap()] = extra;
|
|
let outside: InputSnapshot = serde_json::from_value(outside).unwrap();
|
|
assert!(FileWorld::from_snapshot(Arc::new(outside)).is_err());
|
|
// Recorded probe metadata is not authority to turn an immutable canonical
|
|
// input into a live host read, even when input is requested through an alias.
|
|
fs::write(p.join("main.tex"), b"PHYSICAL INJECTION").unwrap();
|
|
let mut forged = original;
|
|
forged["probes"][p.join("main.tex").to_str().unwrap()] =
|
|
serde_json::json!({"Ok":p.join("owned.dat")});
|
|
let forged: InputSnapshot = serde_json::from_value(forged).unwrap();
|
|
let mut forged = FileWorld::from_snapshot(Arc::new(forged)).unwrap();
|
|
assert_eq!(
|
|
forged
|
|
.read(ResourceKind::Tex, "entry", &p.join("main.tex"))
|
|
.unwrap()
|
|
.data,
|
|
b"UNSAVED"
|
|
);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
#[test]
|
|
fn directory_aliases_parent_components_and_unknown_universe_follow_declared_namespace() {
|
|
let p = directory();
|
|
let mut d = definition(&p);
|
|
file(&mut d, p.join("real/child/leaf.tex"), b"LEAF");
|
|
file(&mut d, p.join("real/parent.tex"), b"RIGHT");
|
|
file(&mut d, p.join("parent.tex"), b"WRONG");
|
|
d.aliases.insert(p.join("link"), "real/child".into());
|
|
d.aliases
|
|
.insert(p.join("file-dot"), "real/parent.tex/.".into());
|
|
d.aliases
|
|
.insert(p.join("file-slash"), "real/parent.tex/".into());
|
|
let mut w = FileWorld::from_project(ProjectSnapshot::new(d).unwrap()).unwrap();
|
|
assert_eq!(
|
|
w.read(
|
|
ResourceKind::Tex,
|
|
"./link/../parent.tex",
|
|
&p.join("main.tex")
|
|
)
|
|
.unwrap()
|
|
.data,
|
|
b"RIGHT"
|
|
);
|
|
assert!(
|
|
w.read(
|
|
ResourceKind::Tex,
|
|
"./real/parent.tex/../parent.tex",
|
|
&p.join("main.tex")
|
|
)
|
|
.err()
|
|
.unwrap()
|
|
.contains("traverses a file")
|
|
);
|
|
assert!(
|
|
w.read_optional(
|
|
ResourceKind::Tex,
|
|
"./missing/../parent.tex",
|
|
&p.join("main.tex")
|
|
)
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
assert!(
|
|
w.read(ResourceKind::Tex, "./real/parent.tex/", &p.join("main.tex"))
|
|
.is_err()
|
|
);
|
|
for name in ["./real/parent.tex/.", "file-dot", "file-slash"] {
|
|
assert!(
|
|
w.read(ResourceKind::Tex, name, &p.join("main.tex"))
|
|
.is_err()
|
|
);
|
|
}
|
|
let outside = p.with_extension("outside.tex");
|
|
fs::write(&outside, b"FORBIDDEN PHYSICAL FALLBACK").unwrap();
|
|
assert!(
|
|
w.read(
|
|
ResourceKind::Tex,
|
|
outside.to_str().unwrap(),
|
|
&p.join("main.tex")
|
|
)
|
|
.err()
|
|
.unwrap()
|
|
.contains("outside the resource universe")
|
|
);
|
|
fs::remove_file(outside).unwrap();
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
#[test]
|
|
fn recursive_priority_alias_cycles_and_new_higher_priority_files_use_one_version() {
|
|
let p = directory();
|
|
let mut d = definition(&p);
|
|
d.configuration.paths.insert(
|
|
ResourceKind::Tex,
|
|
vec![
|
|
SearchPath::directory(p.join("high"), true),
|
|
SearchPath::directory(p.join("low"), false),
|
|
],
|
|
);
|
|
file(&mut d, p.join("package/deep/unit.tex"), b"HIGH ALIAS");
|
|
file(&mut d, p.join("low/unit.tex"), b"LOW");
|
|
d.aliases.insert(p.join("high/link"), p.join("package"));
|
|
d.aliases.insert(p.join("high/loop"), p.join("high"));
|
|
let mut old = FileWorld::from_project(ProjectSnapshot::new(d.clone()).unwrap()).unwrap();
|
|
assert_eq!(
|
|
old.read(ResourceKind::Tex, "unit", &p.join("main.tex"))
|
|
.unwrap()
|
|
.data,
|
|
b"HIGH ALIAS"
|
|
);
|
|
assert!(old.trace().lookups[0].candidates.len() < 10);
|
|
d.revision = 2;
|
|
file(&mut d, p.join("high/unit.tex"), b"NEW DIRECT");
|
|
let mut new = FileWorld::from_project(ProjectSnapshot::new(d).unwrap()).unwrap();
|
|
assert_eq!(
|
|
new.read(ResourceKind::Tex, "unit", &p.join("main.tex"))
|
|
.unwrap()
|
|
.data,
|
|
b"NEW DIRECT"
|
|
);
|
|
assert_eq!(
|
|
old.read(ResourceKind::Tex, "unit", &p.join("main.tex"))
|
|
.unwrap()
|
|
.data,
|
|
b"HIGH ALIAS"
|
|
);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
#[test]
|
|
fn invalid_node_roles_alias_cycles_and_corrupted_content_are_rejected() {
|
|
let p = directory();
|
|
let mut base = definition(&p);
|
|
file(&mut base, p.join("main.tex"), b"DATA");
|
|
let mut cycle = base.clone();
|
|
cycle.aliases.insert(p.join("a"), "b".into());
|
|
cycle.aliases.insert(p.join("b"), "a".into());
|
|
assert!(
|
|
ProjectSnapshot::new(cycle)
|
|
.err()
|
|
.unwrap()
|
|
.contains("alias depth")
|
|
);
|
|
let mut roles = base.clone();
|
|
roles.directories.insert(p.join("main.tex"));
|
|
assert!(ProjectSnapshot::new(roles).is_err());
|
|
let mut generated = base.clone();
|
|
generated.generated.insert(p.join("main.tex"));
|
|
assert!(ProjectSnapshot::new(generated).is_err());
|
|
let mut outside = base.clone();
|
|
file(&mut outside, p.with_extension("outside"), b"BAD");
|
|
assert!(ProjectSnapshot::new(outside).is_err());
|
|
let mut dotted = base.clone();
|
|
file(&mut dotted, p.join("./dot.tex"), b"DOT");
|
|
assert!(ProjectSnapshot::new(dotted).is_err());
|
|
let mut capacity = definition(&p);
|
|
capacity.configuration.max_index_entries = p.ancestors().count() + 1;
|
|
capacity.generated = BTreeSet::from([p.join("first.dat"), p.join("second.dat")]);
|
|
assert!(
|
|
ProjectSnapshot::new(capacity)
|
|
.err()
|
|
.unwrap()
|
|
.contains("namespace capacity")
|
|
);
|
|
let project = ProjectSnapshot::new(base).unwrap();
|
|
let mut json = serde_json::to_value(&*project).unwrap();
|
|
json["files"][p.join("main.tex").to_str().unwrap()]["data"][0] = serde_json::json!(0);
|
|
let bad: ProjectSnapshot = serde_json::from_value(json).unwrap();
|
|
assert!(FileWorld::from_project(Arc::new(bad)).is_err());
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
#[test]
|
|
fn live_generated_overlay_preserves_writes_reopens_and_alias_lookup_after_cached_absence() {
|
|
let p = directory();
|
|
let mut d = definition(&p);
|
|
file(&mut d, p.join("main.tex"), b"IMMUTABLE");
|
|
d.aliases.insert(p.join("readback"), "owned.dat".into());
|
|
let mut w = FileWorld::from_project(ProjectSnapshot::new(d).unwrap()).unwrap();
|
|
assert!(
|
|
w.read_optional(ResourceKind::Tex, "readback", &p.join("main.tex"))
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
let path = p.join("owned.dat");
|
|
w.check_generated_output(&path, &path).unwrap();
|
|
fs::write(&path, b"FIRST").unwrap();
|
|
w.mark_live(&path);
|
|
assert_eq!(
|
|
w.read(ResourceKind::Tex, "readback", &p.join("main.tex"))
|
|
.unwrap()
|
|
.data,
|
|
b"FIRST"
|
|
);
|
|
fs::write(&path, b"SECOND").unwrap();
|
|
assert_eq!(
|
|
w.read(ResourceKind::Tex, "readback", &p.join("main.tex"))
|
|
.unwrap()
|
|
.data,
|
|
b"SECOND"
|
|
);
|
|
assert_eq!(
|
|
w.read(ResourceKind::Tex, "readback", &p.join("main.tex"))
|
|
.unwrap()
|
|
.data,
|
|
b"SECOND"
|
|
);
|
|
assert!(
|
|
w.check_generated_output(&p.join("main.tex"), &p.join("main.tex"))
|
|
.is_err()
|
|
);
|
|
assert!(
|
|
w.check_generated_output(&p.with_extension("outside"), &p.with_extension("outside"))
|
|
.is_err()
|
|
);
|
|
assert!(w.input_snapshot().project().is_some());
|
|
assert!(w.input_snapshot().file(&path).is_none());
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn protected_generated_resources_keep_alias_and_recursive_visibility_without_cache_role_grants() {
|
|
let p = directory();
|
|
fs::create_dir_all(p.join("gen/nested")).unwrap();
|
|
let mut d = definition(&p);
|
|
file(&mut d, p.join("main.tex"), b"SOURCE");
|
|
d.configuration.paths.insert(
|
|
ResourceKind::Tfm,
|
|
vec![SearchPath::directory(p.join("gen"), true)],
|
|
);
|
|
d.generated.insert(p.join("gen/nested/initial.tfm"));
|
|
d.aliases
|
|
.insert(p.join("again.tfm"), "gen/nested/initial.tfm".into());
|
|
d.aliases
|
|
.insert(p.join("later-view.tfm"), "gen/nested/later.tfm".into());
|
|
let project = ProjectSnapshot::new(d).unwrap();
|
|
let mut world = FileWorld::from_project(project.clone()).unwrap();
|
|
assert!(
|
|
world
|
|
.read_optional(ResourceKind::Tfm, "./later-view.tfm", &p)
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
for (name, alias, initial) in [("initial", "again", true), ("later", "later-view", false)] {
|
|
let path = p.join(format!("gen/nested/{name}.tfm"));
|
|
fs::write(&path, b"ORIGINAL RESOURCE").unwrap();
|
|
if !initial {
|
|
world.mark_live(&path);
|
|
}
|
|
let loaded = world.read(ResourceKind::Tfm, name, &p).unwrap();
|
|
world.protect_resource(&loaded);
|
|
fs::write(&path, b"PHYSICAL CHANGE").unwrap();
|
|
for lookup in [
|
|
format!("./{alias}.tfm"),
|
|
format!("./gen/nested/{name}.tfm"),
|
|
name.into(),
|
|
] {
|
|
assert_eq!(
|
|
world.read(ResourceKind::Tfm, &lookup, &p).unwrap().data,
|
|
loaded.data
|
|
);
|
|
}
|
|
fs::remove_file(&path).unwrap();
|
|
assert_eq!(
|
|
world.read(ResourceKind::Tfm, name, &p).unwrap().version,
|
|
loaded.version
|
|
);
|
|
assert!(world.check_generated_output(&path, &path).is_err());
|
|
assert!(world.check_artifact_output(&path).is_err());
|
|
}
|
|
let snapshot = world.input_snapshot();
|
|
assert!(!snapshot.replay_gaps().is_empty());
|
|
assert!(FileWorld::from_snapshot(snapshot).is_err());
|
|
|
|
// Arbitrary serialized cache bytes are not a real resource promotion.
|
|
let fresh = FileWorld::from_project(project).unwrap();
|
|
let mut forged = serde_json::to_value(&*fresh.input_snapshot()).unwrap();
|
|
let path = p.join("gen/fake.tfm");
|
|
let loaded = world.read(ResourceKind::Tfm, "initial", &p).unwrap();
|
|
let mut version = loaded.version;
|
|
version.path = path.clone();
|
|
forged["files"][path.to_str().unwrap()] =
|
|
serde_json::json!({"Ok":{"version":version,"data":loaded.data}});
|
|
let forged: InputSnapshot = serde_json::from_value(forged).unwrap();
|
|
let mut forged = FileWorld::from_snapshot(Arc::new(forged)).unwrap();
|
|
assert!(
|
|
forged
|
|
.read_optional(ResourceKind::Tfm, "fake", &p)
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|