5
5
import 'dart:async' ;
6
6
import 'dart:convert' ;
7
7
8
+ import 'package:file/file.dart' ;
9
+ import 'package:file/memory.dart' ;
8
10
import 'package:flutter_tools/src/asset.dart' ;
9
11
import 'package:flutter_tools/src/base/io.dart' ;
10
12
import 'package:flutter_tools/src/build_info.dart' ;
11
13
import 'package:flutter_tools/src/devfs.dart' ;
12
- import 'package:flutter_tools/src/base/file_system.dart' ;
13
14
import 'package:flutter_tools/src/vmservice.dart' ;
14
15
import 'package:test/test.dart' ;
15
16
@@ -18,13 +19,20 @@ import 'src/context.dart';
18
19
import 'src/mocks.dart' ;
19
20
20
21
void main () {
21
- final String filePath = fs.path.join ('lib' , 'foo.txt' );
22
- final String filePath2 = fs.path.join ('foo' , 'bar.txt' );
22
+ FileSystem fs;
23
+ String filePath;
24
+ String filePath2;
23
25
Directory tempDir;
24
26
String basePath;
25
27
DevFS devFS;
26
28
final AssetBundle assetBundle = new AssetBundle ();
27
29
30
+ setUpAll (() {
31
+ fs = new MemoryFileSystem ();
32
+ filePath = fs.path.join ('lib' , 'foo.txt' );
33
+ filePath2 = fs.path.join ('foo' , 'bar.txt' );
34
+ });
35
+
28
36
group ('DevFSContent' , () {
29
37
test ('bytes' , () {
30
38
final DevFSByteContent content = new DevFSByteContent (< int > [4 , 5 , 6 ]);
@@ -59,7 +67,7 @@ void main() {
59
67
final MockDevFSOperations devFSOperations = new MockDevFSOperations ();
60
68
61
69
setUpAll (() {
62
- tempDir = _newTempDir ();
70
+ tempDir = _newTempDir (fs );
63
71
basePath = tempDir.path;
64
72
});
65
73
tearDownAll (_cleanupTempDirs);
@@ -72,7 +80,7 @@ void main() {
72
80
_packages['my_project' ] = fs.path.toUri ('lib' );
73
81
74
82
// simulate package
75
- await _createPackage ('somepkg' , 'somefile.txt' );
83
+ await _createPackage (fs, 'somepkg' , 'somefile.txt' );
76
84
77
85
devFS = new DevFS .operations (devFSOperations, 'test' , tempDir);
78
86
await devFS.create ();
@@ -95,7 +103,10 @@ void main() {
95
103
);
96
104
97
105
expect (bytes, 48 );
106
+ }, overrides: < Type , Generator > {
107
+ FileSystem : () => fs,
98
108
});
109
+
99
110
testUsingContext ('add new file to local file system' , () async {
100
111
final File file = fs.file (fs.path.join (basePath, filePath2));
101
112
await file.parent.create (recursive: true );
@@ -106,7 +117,10 @@ void main() {
106
117
]);
107
118
expect (devFS.assetPathsToEvict, isEmpty);
108
119
expect (bytes, 7 );
120
+ }, overrides: < Type , Generator > {
121
+ FileSystem : () => fs,
109
122
});
123
+
110
124
testUsingContext ('modify existing file on local file system' , () async {
111
125
await devFS.update ();
112
126
final File file = fs.file (fs.path.join (basePath, filePath));
@@ -124,7 +138,10 @@ void main() {
124
138
]);
125
139
expect (devFS.assetPathsToEvict, isEmpty);
126
140
expect (bytes, 6 );
141
+ }, overrides: < Type , Generator > {
142
+ FileSystem : () => fs,
127
143
});
144
+
128
145
testUsingContext ('delete a file from the local file system' , () async {
129
146
final File file = fs.file (fs.path.join (basePath, filePath));
130
147
await file.delete ();
@@ -134,20 +151,26 @@ void main() {
134
151
]);
135
152
expect (devFS.assetPathsToEvict, isEmpty);
136
153
expect (bytes, 0 );
154
+ }, overrides: < Type , Generator > {
155
+ FileSystem : () => fs,
137
156
});
157
+
138
158
testUsingContext ('add new package' , () async {
139
- await _createPackage ('newpkg' , 'anotherfile.txt' );
159
+ await _createPackage (fs, 'newpkg' , 'anotherfile.txt' );
140
160
final int bytes = await devFS.update ();
141
161
devFSOperations.expectMessages (< String > [
142
162
'writeFile test .packages' ,
143
163
'writeFile test packages/newpkg/anotherfile.txt' ,
144
164
]);
145
165
expect (devFS.assetPathsToEvict, isEmpty);
146
166
expect (bytes, 69 );
167
+ }, overrides: < Type , Generator > {
168
+ FileSystem : () => fs,
147
169
});
170
+
148
171
testUsingContext ('add new package with double slashes in URI' , () async {
149
172
final String packageName = 'doubleslashpkg' ;
150
- await _createPackage (packageName, 'somefile.txt' , doubleSlash: true );
173
+ await _createPackage (fs, packageName, 'somefile.txt' , doubleSlash: true );
151
174
152
175
final Set <String > fileFilter = new Set <String >();
153
176
final List <Uri > pkgUris = < Uri > [fs.path.toUri (basePath)]..addAll (_packages.values);
@@ -169,76 +192,96 @@ void main() {
169
192
]);
170
193
expect (devFS.assetPathsToEvict, isEmpty);
171
194
expect (bytes, 109 );
195
+ }, overrides: < Type , Generator > {
196
+ FileSystem : () => fs,
172
197
});
198
+
173
199
testUsingContext ('add an asset bundle' , () async {
174
200
assetBundle.entries['a.txt' ] = new DevFSStringContent ('abc' );
175
201
final int bytes = await devFS.update (bundle: assetBundle, bundleDirty: true );
176
202
devFSOperations.expectMessages (< String > [
177
- 'writeFile test ${_inAssetBuildDirectory ('a.txt' )}' ,
203
+ 'writeFile test ${_inAssetBuildDirectory (fs , 'a.txt' )}' ,
178
204
]);
179
205
expect (devFS.assetPathsToEvict, unorderedMatches (< String > ['a.txt' ]));
180
206
devFS.assetPathsToEvict.clear ();
181
207
expect (bytes, 3 );
208
+ }, overrides: < Type , Generator > {
209
+ FileSystem : () => fs,
182
210
});
211
+
183
212
testUsingContext ('add a file to the asset bundle - bundleDirty' , () async {
184
213
assetBundle.entries['b.txt' ] = new DevFSStringContent ('abcd' );
185
214
final int bytes = await devFS.update (bundle: assetBundle, bundleDirty: true );
186
215
// Expect entire asset bundle written because bundleDirty is true
187
216
devFSOperations.expectMessages (< String > [
188
- 'writeFile test ${_inAssetBuildDirectory ('a.txt' )}' ,
189
- 'writeFile test ${_inAssetBuildDirectory ('b.txt' )}' ,
217
+ 'writeFile test ${_inAssetBuildDirectory (fs , 'a.txt' )}' ,
218
+ 'writeFile test ${_inAssetBuildDirectory (fs , 'b.txt' )}' ,
190
219
]);
191
220
expect (devFS.assetPathsToEvict, unorderedMatches (< String > [
192
221
'a.txt' , 'b.txt' ]));
193
222
devFS.assetPathsToEvict.clear ();
194
223
expect (bytes, 7 );
224
+ }, overrides: < Type , Generator > {
225
+ FileSystem : () => fs,
195
226
});
227
+
196
228
testUsingContext ('add a file to the asset bundle' , () async {
197
229
assetBundle.entries['c.txt' ] = new DevFSStringContent ('12' );
198
230
final int bytes = await devFS.update (bundle: assetBundle);
199
231
devFSOperations.expectMessages (< String > [
200
- 'writeFile test ${_inAssetBuildDirectory ('c.txt' )}' ,
232
+ 'writeFile test ${_inAssetBuildDirectory (fs , 'c.txt' )}' ,
201
233
]);
202
234
expect (devFS.assetPathsToEvict, unorderedMatches (< String > [
203
235
'c.txt' ]));
204
236
devFS.assetPathsToEvict.clear ();
205
237
expect (bytes, 2 );
238
+ }, overrides: < Type , Generator > {
239
+ FileSystem : () => fs,
206
240
});
241
+
207
242
testUsingContext ('delete a file from the asset bundle' , () async {
208
243
assetBundle.entries.remove ('c.txt' );
209
244
final int bytes = await devFS.update (bundle: assetBundle);
210
245
devFSOperations.expectMessages (< String > [
211
- 'deleteFile test ${_inAssetBuildDirectory ('c.txt' )}' ,
246
+ 'deleteFile test ${_inAssetBuildDirectory (fs , 'c.txt' )}' ,
212
247
]);
213
248
expect (devFS.assetPathsToEvict, unorderedMatches (< String > ['c.txt' ]));
214
249
devFS.assetPathsToEvict.clear ();
215
250
expect (bytes, 0 );
251
+ }, overrides: < Type , Generator > {
252
+ FileSystem : () => fs,
216
253
});
254
+
217
255
testUsingContext ('delete all files from the asset bundle' , () async {
218
256
assetBundle.entries.clear ();
219
257
final int bytes = await devFS.update (bundle: assetBundle, bundleDirty: true );
220
258
devFSOperations.expectMessages (< String > [
221
- 'deleteFile test ${_inAssetBuildDirectory ('a.txt' )}' ,
222
- 'deleteFile test ${_inAssetBuildDirectory ('b.txt' )}' ,
259
+ 'deleteFile test ${_inAssetBuildDirectory (fs , 'a.txt' )}' ,
260
+ 'deleteFile test ${_inAssetBuildDirectory (fs , 'b.txt' )}' ,
223
261
]);
224
262
expect (devFS.assetPathsToEvict, unorderedMatches (< String > [
225
263
'a.txt' , 'b.txt'
226
264
]));
227
265
devFS.assetPathsToEvict.clear ();
228
266
expect (bytes, 0 );
267
+ }, overrides: < Type , Generator > {
268
+ FileSystem : () => fs,
229
269
});
270
+
230
271
testUsingContext ('delete dev file system' , () async {
231
272
await devFS.destroy ();
232
273
devFSOperations.expectMessages (< String > ['destroy test' ]);
233
274
expect (devFS.assetPathsToEvict, isEmpty);
275
+ }, overrides: < Type , Generator > {
276
+ FileSystem : () => fs,
234
277
});
235
278
});
236
279
237
280
group ('devfs remote' , () {
238
281
MockVMService vmService;
239
282
240
283
setUpAll (() async {
241
- tempDir = _newTempDir ();
284
+ tempDir = _newTempDir (fs );
242
285
basePath = tempDir.path;
243
286
vmService = new MockVMService ();
244
287
await vmService.setUp ();
@@ -255,7 +298,7 @@ void main() {
255
298
file.writeAsBytesSync (< int > [1 , 2 , 3 ]);
256
299
257
300
// simulate package
258
- await _createPackage ('somepkg' , 'somefile.txt' );
301
+ await _createPackage (fs, 'somepkg' , 'somefile.txt' );
259
302
260
303
devFS = new DevFS (vmService, 'test' , tempDir);
261
304
await devFS.create ();
@@ -270,13 +313,17 @@ void main() {
270
313
]);
271
314
expect (devFS.assetPathsToEvict, isEmpty);
272
315
expect (bytes, 48 );
316
+ }, overrides: < Type , Generator > {
317
+ FileSystem : () => fs,
273
318
});
274
319
275
320
testUsingContext ('delete dev file system' , () async {
276
321
expect (vmService.messages, isEmpty, reason: 'prior test timeout' );
277
322
await devFS.destroy ();
278
323
vmService.expectMessages (< String > ['_deleteDevFS {fsName: test}' ]);
279
324
expect (devFS.assetPathsToEvict, isEmpty);
325
+ }, overrides: < Type , Generator > {
326
+ FileSystem : () => fs,
280
327
});
281
328
});
282
329
}
@@ -349,7 +396,7 @@ class MockVM implements VM {
349
396
final List <Directory > _tempDirs = < Directory > [];
350
397
final Map < String , Uri > _packages = < String , Uri > {};
351
398
352
- Directory _newTempDir () {
399
+ Directory _newTempDir (FileSystem fs ) {
353
400
final Directory tempDir = fs.systemTempDirectory.createTempSync ('devfs${_tempDirs .length }' );
354
401
_tempDirs.add (tempDir);
355
402
return tempDir;
@@ -361,8 +408,8 @@ void _cleanupTempDirs() {
361
408
}
362
409
}
363
410
364
- Future <Null > _createPackage (String pkgName, String pkgFileName, { bool doubleSlash: false }) async {
365
- final Directory pkgTempDir = _newTempDir ();
411
+ Future <Null > _createPackage (FileSystem fs, String pkgName, String pkgFileName, { bool doubleSlash: false }) async {
412
+ final Directory pkgTempDir = _newTempDir (fs );
366
413
String pkgFilePath = fs.path.join (pkgTempDir.path, pkgName, 'lib' , pkgFileName);
367
414
if (doubleSlash) {
368
415
// Force two separators into the path.
@@ -380,6 +427,6 @@ Future<Null> _createPackage(String pkgName, String pkgFileName, { bool doubleSla
380
427
fs.file (fs.path.join (_tempDirs[0 ].path, '.packages' )).writeAsStringSync (sb.toString ());
381
428
}
382
429
383
- String _inAssetBuildDirectory (String filename) {
430
+ String _inAssetBuildDirectory (FileSystem fs, String filename) {
384
431
return '${fs .path .toUri (getAssetBuildDirectory ()).path }/$filename ' ;
385
432
}
0 commit comments