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
This commit is contained in:
Pete Bacon Darwin 2020-02-10 09:12:53 +00:00 committed by Alex Rickabaugh
parent f95b8ce07e
commit 7e8ce24116
2 changed files with 38 additions and 0 deletions

View File

@ -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 {

View File

@ -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');