diff --git a/pkg/front_end/lib/memory_file_system.dart b/pkg/front_end/lib/memory_file_system.dart index b5515b77251..01513496867 100644 --- a/pkg/front_end/lib/memory_file_system.dart +++ b/pkg/front_end/lib/memory_file_system.dart @@ -16,6 +16,7 @@ import 'file_system.dart'; /// Not intended to be implemented or extended by clients. class MemoryFileSystem implements FileSystem { final Map _files = {}; + final Set _directories = new Set(); /// The "current directory" in the in-memory virtual file system. /// @@ -60,8 +61,25 @@ class MemoryFileSystemEntity implements FileSystemEntity { other.uri == uri && identical(other._fileSystem, _fileSystem); + /// Create a directory for this file system entry. + /// + /// If the entry already exists, either as a file, or as a directory, + /// this is an error. + void createDirectory() { + if (_fileSystem._files[uri] != null) { + throw new FileSystemException(uri, 'Entry $uri is a file.'); + } + if (_fileSystem._directories.contains(uri)) { + throw new FileSystemException(uri, 'Directory $uri already exists.'); + } + _fileSystem._directories.add(uri); + } + @override - Future exists() async => _fileSystem._files[uri] != null; + Future exists() async { + return _fileSystem._files[uri] != null || + _fileSystem._directories.contains(uri); + } @override Future> readAsBytes() async { @@ -104,6 +122,9 @@ class MemoryFileSystemEntity implements FileSystemEntity { } void _update(Uri uri, Uint8List data) { + if (_fileSystem._directories.contains(uri)) { + throw new FileSystemException(uri, 'Entry $uri is a directory.'); + } _fileSystem._files[uri] = data; } } diff --git a/pkg/front_end/test/memory_file_system_test.dart b/pkg/front_end/test/memory_file_system_test.dart index f0b44533d98..986abb70fe1 100644 --- a/pkg/front_end/test/memory_file_system_test.dart +++ b/pkg/front_end/test/memory_file_system_test.dart @@ -37,6 +37,21 @@ class FileTest extends _BaseTestNative { file = entityForPath(path); } + test_createDirectory_doesNotExist() async { + file.createDirectory(); + expect(await file.exists(), true); + } + + test_createDirectory_exists_asDirectory() async { + file.createDirectory(); + expect(() => file.createDirectory(), _throwsFileSystemException); + } + + test_createDirectory_exists_asFile() async { + file.writeAsStringSync(''); + expect(() => file.createDirectory(), _throwsFileSystemException); + } + test_equals_differentPaths() { expect(file == entityForPath(join(tempPath, 'file2.txt')), isFalse); } @@ -45,11 +60,16 @@ class FileTest extends _BaseTestNative { expect(file == entityForPath(join(tempPath, 'file.txt')), isTrue); } + test_exists_directory_exists() async { + file.createDirectory(); + expect(await file.exists(), true); + } + test_exists_doesNotExist() async { expect(await file.exists(), false); } - test_exists_exists() async { + test_exists_file_exists() async { file.writeAsStringSync('x'); expect(await file.exists(), true); } @@ -99,6 +119,11 @@ class FileTest extends _BaseTestNative { expect(await file.readAsString(), '\u20ac'); } + test_writeAsBytesSync_directory() async { + file.createDirectory(); + expect(() => file.writeAsBytesSync([0]), _throwsFileSystemException); + } + test_writeAsBytesSync_modifyAfterRead() async { file.writeAsBytesSync([1]); (await file.readAsBytes())[0] = 2; @@ -118,6 +143,11 @@ class FileTest extends _BaseTestNative { expect(await file.readAsBytes(), [2]); } + test_writeAsStringSync_directory() async { + file.createDirectory(); + expect(() => file.writeAsStringSync(''), _throwsFileSystemException); + } + test_writeAsStringSync_overwrite() async { file.writeAsStringSync('first'); file.writeAsStringSync('second');