Skip to content

Commit 26d5d36

Browse files
committed
Deduplicate transient cleanup logic and add edge-case tests
cleanupFileInformationCache() now delegates to invalidateForFile() instead of duplicating the same transient-check logic. Add tests for invalidateForFile() with no-target entries and missing keys.
1 parent cbaca77 commit 26d5d36

2 files changed

Lines changed: 42 additions & 10 deletions

File tree

src/project/project-shared.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -707,16 +707,9 @@ export class FileInformationCacheMap extends Map<string, FileInformation>
707707
}
708708

709709
export function cleanupFileInformationCache(project: ProjectContext) {
710-
project.fileInformationCache.forEach((entry) => {
711-
if (entry?.target?.data) {
712-
const data = entry.target.data as {
713-
transient?: boolean;
714-
};
715-
if (data.transient && entry.target?.input) {
716-
safeRemoveSync(entry.target?.input);
717-
}
718-
}
719-
});
710+
for (const key of [...project.fileInformationCache.keys()]) {
711+
project.fileInformationCache.invalidateForFile(key);
712+
}
720713
}
721714

722715
export async function withProjectCleanup<T>(

tests/unit/project/file-information-cache.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,42 @@ unitTest(
207207
);
208208
},
209209
);
210+
211+
// deno-lint-ignore require-await
212+
unitTest(
213+
"fileInformationCache - invalidateForFile handles entry with no target",
214+
async () => {
215+
const project = createMockProjectContext();
216+
const sourcePath = join(project.dir, "doc.qmd");
217+
218+
// Populate cache entry with metadata only (no target)
219+
const entry = ensureFileInformationCache(project, sourcePath);
220+
entry.metadata = { title: "Test" };
221+
222+
// Should not throw
223+
project.fileInformationCache.invalidateForFile(sourcePath);
224+
225+
assert(
226+
!project.fileInformationCache.has(sourcePath),
227+
"Cache entry should be removed even without a target",
228+
);
229+
},
230+
);
231+
232+
// deno-lint-ignore require-await
233+
unitTest(
234+
"fileInformationCache - invalidateForFile is a no-op for missing keys",
235+
async () => {
236+
const project = createMockProjectContext();
237+
238+
// Should not throw on a key that doesn't exist
239+
project.fileInformationCache.invalidateForFile(
240+
join(project.dir, "nonexistent.qmd"),
241+
);
242+
243+
assert(
244+
project.fileInformationCache.size === 0,
245+
"Cache should remain empty",
246+
);
247+
},
248+
);

0 commit comments

Comments
 (0)