per instance temp directory naming (#152)
diff --git a/packages/file/CHANGELOG.md b/packages/file/CHANGELOG.md
index d44e91a..6b4f893 100644
--- a/packages/file/CHANGELOG.md
+++ b/packages/file/CHANGELOG.md
@@ -1,3 +1,8 @@
+#### 5.2.1
+
+* systemTemp directories created by `MemoryFileSystem` will allot names
+ based on the file system instance instead of globally.
+
#### 5.2.0
* Added a `MemoryRandomAccessFile` class and implemented
diff --git a/packages/file/lib/src/backends/memory/memory_directory.dart b/packages/file/lib/src/backends/memory/memory_directory.dart
index 0bb266e..4e2bbd3 100644
--- a/packages/file/lib/src/backends/memory/memory_directory.dart
+++ b/packages/file/lib/src/backends/memory/memory_directory.dart
@@ -17,6 +17,10 @@
import 'style.dart';
import 'utils.dart' as utils;
+// Tracks a unique name for system temp directories, per filesystem
+// instance.
+final Expando<int> _systemTempCounter = Expando<int>();
+
/// Internal implementation of [Directory].
class MemoryDirectory extends MemoryFileSystemEntity
with common.DirectoryAddOnsMixin
@@ -25,8 +29,6 @@
MemoryDirectory(NodeBasedFileSystem fileSystem, String path)
: super(fileSystem, path);
- static int _tempCounter = 0;
-
@override
io.FileSystemEntityType get expectedType => io.FileSystemEntityType.directory;
@@ -75,10 +77,12 @@
DirectoryNode node = fileSystem.findNode(dirname);
checkExists(node, () => dirname);
utils.checkIsDir(node, () => dirname);
+ int _tempCounter = _systemTempCounter[fileSystem] ?? 0;
String name() => '$basename$_tempCounter';
while (node.children.containsKey(name())) {
_tempCounter++;
}
+ _systemTempCounter[fileSystem] = _tempCounter;
DirectoryNode tempDir = DirectoryNode(node);
node.children[name()] = tempDir;
return MemoryDirectory(fileSystem, fileSystem.path.join(dirname, name()));
diff --git a/packages/file/pubspec.yaml b/packages/file/pubspec.yaml
index 77a45d7..d90f8eb 100644
--- a/packages/file/pubspec.yaml
+++ b/packages/file/pubspec.yaml
@@ -1,5 +1,5 @@
name: file
-version: 5.2.0
+version: 5.2.1
authors:
- Matan Lurey <matanl@google.com>
- Yegor Jbanov <yjbanov@google.com>
diff --git a/packages/file/test/memory_test.dart b/packages/file/test/memory_test.dart
index 3433ac7..42be7b1 100644
--- a/packages/file/test/memory_test.dart
+++ b/packages/file/test/memory_test.dart
@@ -110,4 +110,25 @@
raf.closeSync();
}
});
+
+ test('MemoryFileSystem.systemTempDirectory test', () {
+ final MemoryFileSystem fs = MemoryFileSystem.test();
+
+ final io.Directory fooA = fs.systemTempDirectory.createTempSync('foo');
+ final io.Directory fooB = fs.systemTempDirectory.createTempSync('foo');
+
+ expect(fooA.path, '/.tmp_rand0/foorand0');
+ expect(fooB.path, '/.tmp_rand0/foorand1');
+
+ final MemoryFileSystem secondFs = MemoryFileSystem.test();
+
+ final io.Directory fooAA =
+ secondFs.systemTempDirectory.createTempSync('foo');
+ final io.Directory fooBB =
+ secondFs.systemTempDirectory.createTempSync('foo');
+
+ // Names are recycled with a new instance
+ expect(fooAA.path, '/.tmp_rand0/foorand0');
+ expect(fooBB.path, '/.tmp_rand0/foorand1');
+ });
}