Skip to content

Commit

Permalink
refactor(compiler-cli): add invalidateCaches to CachedFileSystem (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
petebacondarwin authored and alxhub committed Feb 19, 2020
1 parent 03a8b16 commit e67c69a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit e67c69a

Please sign in to comment.