IOOverrides using package:file for testing (#4774)
diff --git a/pubspec.lock b/pubspec.lock index c7bc53a..4d9ed0f 100644 --- a/pubspec.lock +++ b/pubspec.lock
@@ -106,7 +106,7 @@ source: hosted version: "3.5.2" file: - dependency: transitive + dependency: "direct dev" description: name: file sha256: a3b4f84adafef897088c160faf7dfffb7696046cb13ae90b508c2cbc95d3b8d4
diff --git a/pubspec.yaml b/pubspec.yaml index ab0f64d..54973d2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml
@@ -33,6 +33,7 @@ dev_dependencies: checks: ^0.3.1 dart_flutter_team_lints: ^3.5.2 + file: ^7.0.1 shelf_test_handler: ^2.0.2 test: ^1.26.3 test_descriptor: ^2.0.2
diff --git a/test/wasm/iooverrides.dart b/test/wasm/iooverrides.dart new file mode 100644 index 0000000..4532703 --- /dev/null +++ b/test/wasm/iooverrides.dart
@@ -0,0 +1,56 @@ +import 'dart:io'; + +import 'package:file/file.dart' as f; + +/// Creates an [IOOverrides] that uses [fs] for all operations. +IOOverrides createFileSystemIOOverrides(f.FileSystem fs) => + _FileSystemIOOverrides(fs); + +/// An [IOOverrides] that uses a [f.FileSystem] for all operations. +final class _FileSystemIOOverrides extends IOOverrides { + final f.FileSystem _fs; + + _FileSystemIOOverrides(this._fs); + + @override + File createFile(String path) => _fs.file(path); + + @override + Directory createDirectory(String path) => _fs.directory(path); + + @override + Link createLink(String path) => _fs.link(path); + + @override + Future<FileStat> stat(String path) => _fs.stat(path); + + @override + FileStat statSync(String path) => _fs.statSync(path); + + @override + Future<bool> fseIdentical(String path1, String path2) => + _fs.identical(path1, path2); + + @override + bool fseIdenticalSync(String path1, String path2) => + _fs.identicalSync(path1, path2); + + @override + Future<FileSystemEntityType> fseGetType(String path, bool followLinks) => + _fs.type(path, followLinks: followLinks); + + @override + FileSystemEntityType fseGetTypeSync(String path, bool followLinks) => + _fs.typeSync(path, followLinks: followLinks); + + @override + Directory getCurrentDirectory() => _fs.currentDirectory; + + @override + void setCurrentDirectory(String path) { + _fs.currentDirectory = path; + } + + @override + Directory getSystemTempDirectory() => _fs.systemTempDirectory; +}
diff --git a/test/wasm/iooverrides_test.dart b/test/wasm/iooverrides_test.dart new file mode 100644 index 0000000..0986462 --- /dev/null +++ b/test/wasm/iooverrides_test.dart
@@ -0,0 +1,44 @@ +import 'dart:io'; + +import 'package:file/memory.dart'; +import 'package:test/test.dart'; + +import 'iooverrides.dart'; + +void main() { + test('FileSystemIOOverrides redirects IO operations', () async { + final fs = MemoryFileSystem(); + await IOOverrides.runWithIOOverrides(() async { + final file = File('/test.txt'); + await file.writeAsString('hello'); + + expect(fs.file('/test.txt').readAsStringSync(), 'hello'); + expect(await file.readAsString(), 'hello'); + + final dir = Directory('/subdir'); + await dir.create(); + expect(fs.directory('/subdir').existsSync(), isTrue); + + final list = await Directory('/').list().toList(); + expect(list.map((e) => e.path), containsAll(['test.txt', 'subdir'])); + }, createFileSystemIOOverrides(fs)); + }); + + test('getCurrentDirectory and setCurrentDirectory', () async { + final fs = MemoryFileSystem(); + fs.directory('/a/b').createSync(recursive: true); + await IOOverrides.runWithIOOverrides(() async { + expect(Directory.current.path, '/'); + Directory.current = '/a/b'; + expect(Directory.current.path, '/a/b'); + expect(fs.currentDirectory.path, '/a/b'); + }, createFileSystemIOOverrides(fs)); + }); + + test('systemTempDirectory', () async { + final fs = MemoryFileSystem(); + await IOOverrides.runWithIOOverrides(() async { + expect(Directory.systemTemp.path, fs.systemTempDirectory.path); + }, createFileSystemIOOverrides(fs)); + }); +}