1
1
@Tags (['wasm' ])
2
2
import 'dart:async' ;
3
+ import 'dart:developer' ;
3
4
import 'dart:math' ;
4
5
import 'dart:typed_data' ;
5
6
@@ -12,55 +13,15 @@ const _fsRoot = '/test';
12
13
13
14
Future <void > main () async {
14
15
group ('in memory' , () {
15
- _testWith (() => FileSystem . inMemory ());
16
+ _testWith (() => InMemoryFileSystem ());
16
17
});
17
18
18
19
group ('indexed db' , () {
19
20
_testWith (() => IndexedDbFileSystem .open (dbName: _randomName ()));
20
21
21
- test ('with proper persistence' , () async {
22
- final data = Uint8List .fromList (List .generate (255 , (i) => i));
23
- final dbName = _randomName ();
24
-
25
- await expectLater (IndexedDbFileSystem .databases (),
26
- completion (anyOf (isNull, isNot (contains (dbName)))),
27
- reason: 'Database $dbName should not exist' );
28
-
29
- final db1 = await IndexedDbFileSystem .open (dbName: dbName);
30
- expect (db1.files.length, 0 , reason: 'db1 is not empty' );
31
-
32
- db1.createFile ('test' );
33
- db1.write ('test' , data, 0 );
34
- db1.truncateFile ('test' , 128 );
35
- await db1.flush ();
36
- expect (db1.files, ['test' ], reason: 'File must exist' );
37
- await db1.close ();
38
-
39
- final db2 = await IndexedDbFileSystem .open (dbName: dbName);
40
- expect (db2.files, ['test' ], reason: 'Single file must be in db2 as well' );
41
-
42
- final read = Uint8List (128 );
43
- expect (db2.read ('test' , read, 0 ), 128 , reason: 'Should read 128 bytes' );
44
- expect (read, List .generate (128 , (i) => i),
45
- reason: 'The data written and read do not match' );
46
-
47
- await db2.clear ();
48
- expect (db2.files, isEmpty, reason: 'There must be no files in db2' );
49
-
50
- await db2.close ();
51
- await expectLater (
52
- Future .sync (db2.clear), throwsA (isA <FileSystemException >()));
53
-
54
- await IndexedDbFileSystem .deleteDatabase (dbName);
55
- await expectLater (IndexedDbFileSystem .databases (),
56
- completion (anyOf (isNull, isNot (contains (dbName)))),
57
- reason: 'Database $dbName should not exist in the end' );
58
- });
59
-
60
22
test ('example with frequent writes' , () async {
61
23
final fileSystem = await IndexedDbFileSystem .open (dbName: 'test' );
62
- final sqlite3 =
63
- await loadSqlite3 (SqliteEnvironment (fileSystem: fileSystem));
24
+ final sqlite3 = await loadSqlite3 (fileSystem);
64
25
final database = sqlite3.open ('test' );
65
26
expect (database.userVersion, 0 );
66
27
database.userVersion = 1 ;
@@ -95,53 +56,26 @@ Future<void> main() async {
95
56
await fileSystem.close ().timeout (const Duration (seconds: 1 ));
96
57
97
58
final fileSystem2 = await IndexedDbFileSystem .open (dbName: 'test' );
98
- final sqlite32 =
99
- await loadSqlite3 (SqliteEnvironment (fileSystem: fileSystem2));
59
+ final sqlite32 = await loadSqlite3 (fileSystem2);
100
60
final database2 = sqlite32.open ('test' );
101
61
expect (database2.userVersion, 1 );
102
62
expect (database2.select ('SELECT * FROM users' ), hasLength (200 ));
103
63
});
104
-
105
- test ('can delete and re-create files' , () async {
106
- final dbName = _randomName ();
107
- final fs = await IndexedDbFileSystem .open (dbName: dbName);
108
- addTearDown (() async {
109
- await fs.close ();
110
- return IndexedDbFileSystem .deleteDatabase (dbName);
111
- });
112
-
113
- fs.createFile ('foo' );
114
- fs.write ('foo' , Uint8List .fromList ([1 , 2 , 3 ]), 0 );
115
- await fs.flush ();
116
-
117
- fs.deleteFile ('foo' );
118
- await fs.flush ();
119
-
120
- fs.createFile ('foo' );
121
- fs.write ('foo' , Uint8List .fromList ([4 , 5 , 6 ]), 0 );
122
- await fs.flush ();
123
-
124
- final target = Uint8List (3 );
125
- fs.read ('foo' , target, 0 );
126
- expect (target, [4 , 5 , 6 ]);
127
- });
128
64
});
129
65
}
130
66
131
67
final _random = Random (DateTime .now ().millisecond);
132
68
String _randomName () => _random.nextInt (0x7fffffff ).toString ();
133
69
134
- Future <void > _disposeFileSystem (FileSystem fs, [String ? name]) async {
70
+ Future <void > _disposeFileSystem (VirtualFileSystem fs, [String ? name]) async {
135
71
if (fs is IndexedDbFileSystem ) {
136
72
await fs.close ();
137
73
if (name != null ) await IndexedDbFileSystem .deleteDatabase (name);
138
- } else {
139
- await Future .sync (fs.clear);
140
74
}
141
75
}
142
76
143
- Future <void > _testWith (FutureOr <FileSystem > Function () open) async {
144
- late FileSystem fs;
77
+ Future <void > _testWith (FutureOr <VirtualFileSystem > Function () open) async {
78
+ late VirtualFileSystem fs;
145
79
146
80
setUp (() async {
147
81
fs = await open ();
@@ -151,55 +85,68 @@ Future<void> _testWith(FutureOr<FileSystem> Function() open) async {
151
85
152
86
test ('can create files' , () {
153
87
expect (fs.exists ('$_fsRoot /foo.txt' ), isFalse);
154
- expect (fs.files, isEmpty);
155
88
fs.createFile ('$_fsRoot /foo.txt' );
156
89
expect (fs.exists ('$_fsRoot /foo.txt' ), isTrue);
157
- expect (fs.files, ['$_fsRoot /foo.txt' ]);
158
- fs.deleteFile ('$_fsRoot /foo.txt' );
159
- expect (fs.files, isEmpty);
90
+ fs.xDelete ('$_fsRoot /foo.txt' , 0 );
91
+ expect (fs.exists ('$_fsRoot /foo.txt' ), isFalse);
160
92
});
161
93
162
94
test ('can create and delete multiple files' , () {
95
+ final paths = < String > [];
96
+
163
97
for (var i = 1 ; i <= 10 ; i++ ) {
164
- fs.createFile ('$_fsRoot /foo$i .txt' );
98
+ final path = '$_fsRoot /foo$i .txt' ;
99
+ paths.add (path);
100
+
101
+ debugger ();
102
+ fs.createFile (path);
103
+ expect (fs.exists (path), isTrue);
165
104
}
166
- expect (fs.files, hasLength (10 ));
167
- for (final f in fs.files) {
168
- fs.deleteFile (f);
105
+
106
+ for (final path in paths) {
107
+ fs.xDelete (path, 0 );
108
+ expect (fs.exists (path), isFalse);
169
109
}
170
- expect (fs.files, isEmpty);
171
110
});
172
111
173
112
test ('reads and writes' , () {
174
113
expect (fs.exists ('$_fsRoot /foo.txt' ), isFalse);
114
+
115
+ final file = fs
116
+ .xOpen (Sqlite3Filename ('$_fsRoot /foo.txt' ), SqlFlag .SQLITE_OPEN_CREATE )
117
+ .file;
118
+
175
119
fs.createFile ('$_fsRoot /foo.txt' );
176
- addTearDown (() => fs.deleteFile ('$_fsRoot /foo.txt' ));
120
+ addTearDown (() {
121
+ file.xClose ();
122
+ fs.xDelete ('$_fsRoot /foo.txt' , 0 );
123
+ });
177
124
178
- expect (fs. sizeOfFile ( '$ _fsRoot /foo.txt' ), isZero);
125
+ expect (file. xFileSize ( ), isZero);
179
126
180
- fs. truncateFile ( '$ _fsRoot /foo.txt' , 1024 );
181
- expect (fs. sizeOfFile ( '$ _fsRoot /foo.txt' ), 1024 );
127
+ file. xTruncate ( 1024 );
128
+ expect (file. xFileSize ( ), 1024 );
182
129
183
- fs. truncateFile ( '$ _fsRoot /foo.txt' , 600 );
184
- expect (fs. sizeOfFile ( '$ _fsRoot /foo.txt' ), 600 );
130
+ file. xTruncate ( 600 );
131
+ expect (file. xFileSize ( ), 600 );
185
132
186
- fs. truncateFile ( '$ _fsRoot /foo.txt' , 0 );
187
- expect (fs. sizeOfFile ( '$ _fsRoot /foo.txt' ), 0 );
133
+ file. xTruncate ( 0 );
134
+ expect (file. xFileSize ( ), 0 );
188
135
189
- fs. write ( '$ _fsRoot /foo.txt' , Uint8List .fromList ([1 , 2 , 3 ]), 0 );
190
- expect (fs. sizeOfFile ( '$ _fsRoot /foo.txt' ), 3 );
136
+ file. xWrite ( Uint8List .fromList ([1 , 2 , 3 ]), 0 );
137
+ expect (file. xFileSize ( ), 3 );
191
138
192
139
final target = Uint8List (3 );
193
- expect (fs. read ( '$ _fsRoot /foo.txt' , target, 0 ), 3 );
140
+ file. xRead ( target, 0 );
194
141
expect (target, [1 , 2 , 3 ]);
195
142
});
143
+ }
196
144
197
- test ('can create files and clear fs' , () async {
198
- for (var i = 1 ; i <= 10 ; i++ ) {
199
- fs.createFile ('$_fsRoot /foo$i .txt' );
200
- }
201
- expect (fs.files, hasLength (10 ));
202
- await Future .sync (fs.clear);
203
- expect (fs.files, isEmpty);
204
- });
145
+ extension on VirtualFileSystem {
146
+ bool exists (String file) => xAccess (file, 0 ) != 0 ;
147
+
148
+ void createFile (String path) {
149
+ final open = xOpen (Sqlite3Filename (path), SqlFlag .SQLITE_OPEN_CREATE );
150
+ open.file.xClose ();
151
+ }
205
152
}
0 commit comments