refactor(ivy): add `removeFile` to ngtsc `FileSystem` (#34722)

PR Close #34722
This commit is contained in:
Pete Bacon Darwin 2020-01-09 22:34:28 +00:00 committed by Andrew Kushnir
parent 92c411f86d
commit ecbc25044c
7 changed files with 49 additions and 3 deletions

View File

@ -54,6 +54,12 @@ export class CachedFileSystem implements FileSystem {
this.existsCache.set(path, true); this.existsCache.set(path, true);
} }
removeFile(path: AbsoluteFsPath): void {
this.delegate.removeFile(path);
this.readFileCache.delete(path);
this.existsCache.set(path, false);
}
symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void { symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void {
this.delegate.symlink(target, path); this.delegate.symlink(target, path);
this.existsCache.set(path, true); this.existsCache.set(path, true);

View File

@ -19,6 +19,7 @@ export class InvalidFileSystem implements FileSystem {
exists(path: AbsoluteFsPath): boolean { throw makeError(); } exists(path: AbsoluteFsPath): boolean { throw makeError(); }
readFile(path: AbsoluteFsPath): string { throw makeError(); } readFile(path: AbsoluteFsPath): string { throw makeError(); }
writeFile(path: AbsoluteFsPath, data: string): void { throw makeError(); } writeFile(path: AbsoluteFsPath, data: string): void { throw makeError(); }
removeFile(path: AbsoluteFsPath): void { throw makeError(); }
symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void { throw makeError(); } symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void { throw makeError(); }
readdir(path: AbsoluteFsPath): PathSegment[] { throw makeError(); } readdir(path: AbsoluteFsPath): PathSegment[] { throw makeError(); }
lstat(path: AbsoluteFsPath): FileStats { throw makeError(); } lstat(path: AbsoluteFsPath): FileStats { throw makeError(); }

View File

@ -18,9 +18,8 @@ export class NodeJSFileSystem implements FileSystem {
private _caseSensitive: boolean|undefined = undefined; private _caseSensitive: boolean|undefined = undefined;
exists(path: AbsoluteFsPath): boolean { return fs.existsSync(path); } exists(path: AbsoluteFsPath): boolean { return fs.existsSync(path); }
readFile(path: AbsoluteFsPath): string { return fs.readFileSync(path, 'utf8'); } readFile(path: AbsoluteFsPath): string { return fs.readFileSync(path, 'utf8'); }
writeFile(path: AbsoluteFsPath, data: string): void { writeFile(path: AbsoluteFsPath, data: string): void { fs.writeFileSync(path, data, 'utf8'); }
return fs.writeFileSync(path, data, 'utf8'); removeFile(path: AbsoluteFsPath): void { fs.unlinkSync(path); }
}
symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void { fs.symlinkSync(target, path); } symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void { fs.symlinkSync(target, path); }
readdir(path: AbsoluteFsPath): PathSegment[] { return fs.readdirSync(path) as PathSegment[]; } readdir(path: AbsoluteFsPath): PathSegment[] { return fs.readdirSync(path) as PathSegment[]; }
lstat(path: AbsoluteFsPath): FileStats { return fs.lstatSync(path); } lstat(path: AbsoluteFsPath): FileStats { return fs.lstatSync(path); }

View File

@ -38,6 +38,7 @@ export interface FileSystem {
exists(path: AbsoluteFsPath): boolean; exists(path: AbsoluteFsPath): boolean;
readFile(path: AbsoluteFsPath): string; readFile(path: AbsoluteFsPath): string;
writeFile(path: AbsoluteFsPath, data: string): void; writeFile(path: AbsoluteFsPath, data: string): void;
removeFile(path: AbsoluteFsPath): void;
symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void; symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void;
readdir(path: AbsoluteFsPath): PathSegment[]; readdir(path: AbsoluteFsPath): PathSegment[];
lstat(path: AbsoluteFsPath): FileStats; lstat(path: AbsoluteFsPath): FileStats;

View File

@ -110,6 +110,23 @@ describe('CachedFileSystem', () => {
}); });
}); });
describe('removeFile()', () => {
it('should call delegate', () => {
const spy = spyOn(delegate, 'removeFile');
fs.removeFile(abcPath);
expect(spy).toHaveBeenCalledWith(abcPath);
});
it('should update the exists cache', () => {
spyOn(delegate, 'removeFile');
const existsSpy = spyOn(delegate, 'exists');
fs.removeFile(abcPath);
expect(fs.exists(abcPath)).toBe(false);
expect(existsSpy).not.toHaveBeenCalled();
});
});
describe('readdir()', () => { describe('readdir()', () => {
it('should call delegate', () => { it('should call delegate', () => {
const spy = spyOn(delegate, 'readdir'); const spy = spyOn(delegate, 'readdir');

View File

@ -51,6 +51,14 @@ describe('NodeJSFileSystem', () => {
}); });
}); });
describe('removeFile()', () => {
it('should delegate to fs.unlink()', () => {
const spy = spyOn(realFs, 'unlinkSync');
fs.removeFile(abcPath);
expect(spy).toHaveBeenCalledWith(abcPath);
});
});
describe('readdir()', () => { describe('readdir()', () => {
it('should delegate to fs.readdirSync()', () => { it('should delegate to fs.readdirSync()', () => {
const spy = spyOn(realFs, 'readdirSync').and.returnValue(['x', 'y/z']); const spy = spyOn(realFs, 'readdirSync').and.returnValue(['x', 'y/z']);

View File

@ -44,6 +44,20 @@ export abstract class MockFileSystem implements FileSystem {
entity[basename] = data; entity[basename] = data;
} }
removeFile(path: AbsoluteFsPath): void {
const [folderPath, basename] = this.splitIntoFolderAndFile(path);
const {entity} = this.findFromPath(folderPath);
if (entity === null || !isFolder(entity)) {
throw new MockFileSystemError(
'ENOENT', path, `Unable to remove file "${path}". The containing folder does not exist.`);
}
if (isFolder(entity[basename])) {
throw new MockFileSystemError(
'EISDIR', path, `Unable to remove file "${path}". The path to remove is a folder.`);
}
delete entity[basename];
}
symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void { symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void {
const [folderPath, basename] = this.splitIntoFolderAndFile(path); const [folderPath, basename] = this.splitIntoFolderAndFile(path);
const {entity} = this.findFromPath(folderPath); const {entity} = this.findFromPath(folderPath);