refactor(ivy): add exclusive mode to writeFile() (#34722)

This commit adds an `exclusive` parameter to the
`FileSystem.writeFile()` method. When this parameter is
true, the method will fail with an `EEXIST` error if the
file already exists on disk.

PR Close #34722
This commit is contained in:
Pete Bacon Darwin
2020-01-15 20:21:58 +00:00
committed by Andrew Kushnir
parent ecbc25044c
commit 3a6cb6a5d2
7 changed files with 20 additions and 8 deletions

View File

@ -34,13 +34,17 @@ export abstract class MockFileSystem implements FileSystem {
}
}
writeFile(path: AbsoluteFsPath, data: string): void {
writeFile(path: AbsoluteFsPath, data: string, exclusive: boolean = false): void {
const [folderPath, basename] = this.splitIntoFolderAndFile(path);
const {entity} = this.findFromPath(folderPath);
if (entity === null || !isFolder(entity)) {
throw new MockFileSystemError(
'ENOENT', path, `Unable to write file "${path}". The containing folder does not exist.`);
}
if (exclusive && entity[basename] !== undefined) {
throw new MockFileSystemError(
'EEXIST', path, `Unable to exclusively write file "${path}". The file already exists.`);
}
entity[basename] = data;
}