From e0d6526a53db562fb26d3fcfba091412d3c93bf4 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 19 Nov 2023 12:31:42 +0100 Subject: Watching fixes (#2706) --- tests/src/tests.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'tests/src') diff --git a/tests/src/tests.rs b/tests/src/tests.rs index 3b33a729..42663e6c 100644 --- a/tests/src/tests.rs +++ b/tests/src/tests.rs @@ -218,12 +218,11 @@ struct TestWorld { library: Prehashed, book: Prehashed, fonts: Vec, - paths: RefCell>, + paths: RefCell>, } #[derive(Clone)] struct PathSlot { - system_path: PathBuf, source: OnceCell>, buffer: OnceCell>, } @@ -270,7 +269,7 @@ impl World for TestWorld { let slot = self.slot(id)?; slot.source .get_or_init(|| { - let buf = read(&slot.system_path)?; + let buf = read(&system_path(id)?)?; let text = String::from_utf8(buf)?; Ok(Source::new(id, text)) }) @@ -280,7 +279,7 @@ impl World for TestWorld { fn file(&self, id: FileId) -> FileResult { let slot = self.slot(id)?; slot.buffer - .get_or_init(|| read(&slot.system_path).map(Bytes::from)) + .get_or_init(|| read(&system_path(id)?).map(Bytes::from)) .clone() } @@ -303,16 +302,8 @@ impl TestWorld { } fn slot(&self, id: FileId) -> FileResult> { - let root: PathBuf = match id.package() { - Some(spec) => format!("packages/{}-{}", spec.name, spec.version).into(), - None => PathBuf::new(), - }; - - let system_path = id.vpath().resolve(&root).ok_or(FileError::AccessDenied)?; - Ok(RefMut::map(self.paths.borrow_mut(), |paths| { - paths.entry(system_path.clone()).or_insert_with(|| PathSlot { - system_path, + paths.entry(id).or_insert_with(|| PathSlot { source: OnceCell::new(), buffer: OnceCell::new(), }) @@ -320,7 +311,17 @@ impl TestWorld { } } -/// Read as file. +/// The file system path for a file ID. +fn system_path(id: FileId) -> FileResult { + let root: PathBuf = match id.package() { + Some(spec) => format!("packages/{}-{}", spec.name, spec.version).into(), + None => PathBuf::new(), + }; + + id.vpath().resolve(&root).ok_or(FileError::AccessDenied) +} + +/// Read a file. fn read(path: &Path) -> FileResult> { // Basically symlinks `assets/files` to `tests/files` so that the assets // are within the test project root. -- cgit v1.2.3