mirror of
https://github.com/dart-lang/sdk
synced 2024-11-02 12:24:24 +00:00
Add createDirectory() to MemoryFileSystemEntity.
We need this API to create a mock SDK in a way that makes ProcessedOptions.validateOptions() to accept it - the SDK root must exist. Alternatively (or in addition to) we could make writeXYZ create parent directories implicitly. So, to "create" a directory, you would need to create a file in this directory. R=ahe@google.com, paulberry@google.com, sigmund@google.com BUG= Review-Url: https://codereview.chromium.org/2994643002 .
This commit is contained in:
parent
9a0c11c26b
commit
41bcfaa4a4
2 changed files with 53 additions and 2 deletions
|
@ -16,6 +16,7 @@ import 'file_system.dart';
|
|||
/// Not intended to be implemented or extended by clients.
|
||||
class MemoryFileSystem implements FileSystem {
|
||||
final Map<Uri, Uint8List> _files = {};
|
||||
final Set<Uri> _directories = new Set<Uri>();
|
||||
|
||||
/// 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<bool> exists() async => _fileSystem._files[uri] != null;
|
||||
Future<bool> exists() async {
|
||||
return _fileSystem._files[uri] != null ||
|
||||
_fileSystem._directories.contains(uri);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<int>> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
|
|
Loading…
Reference in a new issue