From 7e8ce24116354a94c3421cf3bb2a7904aed4270f Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Mon, 10 Feb 2020 09:12:53 +0000 Subject: [PATCH] refactor(compiler-cli): add `invalidateCaches` to `CachedFileSystem` (#35131) This is needed by ngcc when reading volatile files that may be changed by an external process (e.g. the lockfile). PR Close #35131 --- .../file_system/src/cached_file_system.ts | 5 +++ .../test/cached_file_system_spec.ts | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/packages/compiler-cli/src/ngtsc/file_system/src/cached_file_system.ts b/packages/compiler-cli/src/ngtsc/file_system/src/cached_file_system.ts index 92030c58c7..25e9ff81de 100644 --- a/packages/compiler-cli/src/ngtsc/file_system/src/cached_file_system.ts +++ b/packages/compiler-cli/src/ngtsc/file_system/src/cached_file_system.ts @@ -28,6 +28,11 @@ export class CachedFileSystem implements FileSystem { return this.existsCache.get(path) !; } + invalidateCaches(path: AbsoluteFsPath) { + this.readFileCache.delete(path); + this.existsCache.delete(path); + } + readFile(path: AbsoluteFsPath): string { if (!this.readFileCache.has(path)) { try { diff --git a/packages/compiler-cli/src/ngtsc/file_system/test/cached_file_system_spec.ts b/packages/compiler-cli/src/ngtsc/file_system/test/cached_file_system_spec.ts index 04610ddaa1..1f81b7c1e9 100644 --- a/packages/compiler-cli/src/ngtsc/file_system/test/cached_file_system_spec.ts +++ b/packages/compiler-cli/src/ngtsc/file_system/test/cached_file_system_spec.ts @@ -90,6 +90,39 @@ describe('CachedFileSystem', () => { }); }); + describe('invalidateCaches()', () => { + it('should use the delegate for `readFile()` if the path for the cached file has been invalidated', + () => { + spyOn(delegate, 'lstat').and.returnValue({isSymbolicLink: () => false}); + const spy = spyOn(delegate, 'readFile').and.returnValue('Some contents'); + fs.readFile(abcPath); // Call once to fill the cache + spy.calls.reset(); + + expect(fs.readFile(abcPath)).toBe('Some contents'); + expect(spy).not.toHaveBeenCalled(); + + fs.invalidateCaches(abcPath); + + expect(fs.readFile(abcPath)).toBe('Some contents'); + expect(spy).toHaveBeenCalledWith(abcPath); + }); + + it('should use the delegate `exists()` if the path for the cached file has been invalidated', + () => { + const spy = spyOn(delegate, 'exists').and.returnValue(true); + fs.exists(abcPath); // Call once to fill the cache + spy.calls.reset(); + + expect(fs.exists(abcPath)).toBe(true); + expect(spy).not.toHaveBeenCalled(); + + fs.invalidateCaches(abcPath); + + expect(fs.exists(abcPath)).toBe(true); + expect(spy).toHaveBeenCalledWith(abcPath); + }); + }); + describe('writeFile()', () => { it('should call delegate', () => { const spy = spyOn(delegate, 'writeFile');