Fix MemoryFileSystem to treat an empty path as non-existent (#213)
* Fix MemoryFileSystem to treat an empty path as non-existent
`_MemoryFileSystem.findNode` returns `reference?.directory` if given
the root path or an empty path. However, an empty path should
be treated as non-existent, not as a directory. Instead explicitly
return `null` as a special case when given an empty path.
Partially addresses https://github.com/google/file.dart/issues/198.
* Make _MemoryFileSystem._current nullable
diff --git a/packages/file/CHANGELOG.md b/packages/file/CHANGELOG.md
index dd09a11..6e46da0 100644
--- a/packages/file/CHANGELOG.md
+++ b/packages/file/CHANGELOG.md
@@ -1,6 +1,7 @@
#### 7.0.0-wip
* Dart 3 fixes for class modifiers.
+* `MemoryFileSystem` now treats empty paths as non-existent.
* Fix `FileSystem.isLink`/`FileSystem.isLinkSync` to not follow symbolic links.
#### 6.1.4
diff --git a/packages/file/lib/src/backends/memory/memory_file_system.dart b/packages/file/lib/src/backends/memory/memory_file_system.dart
index 8d63ea5..f3cdaee 100644
--- a/packages/file/lib/src/backends/memory/memory_file_system.dart
+++ b/packages/file/lib/src/backends/memory/memory_file_system.dart
@@ -200,7 +200,7 @@
/// Gets the node backing for the current working directory. Note that this
/// can return null if the directory has been deleted or moved from under our
/// feet.
- DirectoryNode get _current => findNode(cwd) as DirectoryNode;
+ DirectoryNode? get _current => findNode(cwd) as DirectoryNode?;
@override
Node? findNode(
@@ -211,7 +211,9 @@
List<String>? pathWithSymlinks,
bool followTailLink = false,
}) {
- if (_context.isAbsolute(path)) {
+ if (path.isEmpty) {
+ return null;
+ } else if (_context.isAbsolute(path)) {
reference = _root;
path = path.substring(style.drive.length);
} else {
diff --git a/packages/file/test/common_tests.dart b/packages/file/test/common_tests.dart
index 679a90a..22fb36f 100644
--- a/packages/file/test/common_tests.dart
+++ b/packages/file/test/common_tests.dart
@@ -370,6 +370,11 @@
});
group('stat', () {
+ test('isNotFoundForEmptyPath', () {
+ FileStat stat = fs.statSync('');
+ expect(stat.type, FileSystemEntityType.notFound);
+ });
+
test('isNotFoundForPathToNonExistentEntityAtTail', () {
FileStat stat = fs.statSync(ns('/foo'));
expect(stat.type, FileSystemEntityType.notFound);