Commit ff619621 authored by Todd Volkert's avatar Todd Volkert Committed by GitHub

Update devfs_test to use MemoryFileSystem (#8811)

parent e271fa3e
...@@ -8,7 +8,7 @@ environment: ...@@ -8,7 +8,7 @@ environment:
sdk: '>=1.19.0 <2.0.0' sdk: '>=1.19.0 <2.0.0'
dependencies: dependencies:
file: 2.3.1 file: 2.3.2
json_rpc_2: '^2.0.0' json_rpc_2: '^2.0.0'
matcher: '>=0.12.0 <1.0.0' matcher: '>=0.12.0 <1.0.0'
meta: ^1.0.4 meta: ^1.0.4
......
...@@ -551,7 +551,7 @@ class DevFS { ...@@ -551,7 +551,7 @@ class DevFS {
for (String packageName in packageMap.map.keys) { for (String packageName in packageMap.map.keys) {
final Uri packageUri = packageMap.map[packageName]; final Uri packageUri = packageMap.map[packageName];
final String packagePath = packageUri.toFilePath(); final String packagePath = fs.path.fromUri(packageUri);
final Directory packageDirectory = fs.directory(packageUri); final Directory packageDirectory = fs.directory(packageUri);
Uri directoryUriOnDevice = fs.path.toUri(fs.path.join('packages', packageName) + fs.path.separator); Uri directoryUriOnDevice = fs.path.toUri(fs.path.join('packages', packageName) + fs.path.separator);
bool packageExists; bool packageExists;
......
...@@ -12,7 +12,7 @@ dependencies: ...@@ -12,7 +12,7 @@ dependencies:
args: ^0.13.4 args: ^0.13.4
coverage: ^0.8.0 coverage: ^0.8.0
crypto: '>=1.1.1 <3.0.0' crypto: '>=1.1.1 <3.0.0'
file: 2.3.1 file: 2.3.2
http: ^0.11.3+12 http: ^0.11.3+12
intl: '>=0.14.0 <0.15.0' intl: '>=0.14.0 <0.15.0'
json_rpc_2: ^2.0.0 json_rpc_2: ^2.0.0
......
...@@ -5,11 +5,12 @@ ...@@ -5,11 +5,12 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/asset.dart'; import 'package:flutter_tools/src/asset.dart';
import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/devfs.dart'; import 'package:flutter_tools/src/devfs.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/vmservice.dart'; import 'package:flutter_tools/src/vmservice.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
...@@ -18,13 +19,20 @@ import 'src/context.dart'; ...@@ -18,13 +19,20 @@ import 'src/context.dart';
import 'src/mocks.dart'; import 'src/mocks.dart';
void main() { void main() {
final String filePath = fs.path.join('lib', 'foo.txt'); FileSystem fs;
final String filePath2 = fs.path.join('foo', 'bar.txt'); String filePath;
String filePath2;
Directory tempDir; Directory tempDir;
String basePath; String basePath;
DevFS devFS; DevFS devFS;
final AssetBundle assetBundle = new AssetBundle(); final AssetBundle assetBundle = new AssetBundle();
setUpAll(() {
fs = new MemoryFileSystem();
filePath = fs.path.join('lib', 'foo.txt');
filePath2 = fs.path.join('foo', 'bar.txt');
});
group('DevFSContent', () { group('DevFSContent', () {
test('bytes', () { test('bytes', () {
final DevFSByteContent content = new DevFSByteContent(<int>[4, 5, 6]); final DevFSByteContent content = new DevFSByteContent(<int>[4, 5, 6]);
...@@ -59,7 +67,7 @@ void main() { ...@@ -59,7 +67,7 @@ void main() {
final MockDevFSOperations devFSOperations = new MockDevFSOperations(); final MockDevFSOperations devFSOperations = new MockDevFSOperations();
setUpAll(() { setUpAll(() {
tempDir = _newTempDir(); tempDir = _newTempDir(fs);
basePath = tempDir.path; basePath = tempDir.path;
}); });
tearDownAll(_cleanupTempDirs); tearDownAll(_cleanupTempDirs);
...@@ -72,7 +80,7 @@ void main() { ...@@ -72,7 +80,7 @@ void main() {
_packages['my_project'] = fs.path.toUri('lib'); _packages['my_project'] = fs.path.toUri('lib');
// simulate package // simulate package
await _createPackage('somepkg', 'somefile.txt'); await _createPackage(fs, 'somepkg', 'somefile.txt');
devFS = new DevFS.operations(devFSOperations, 'test', tempDir); devFS = new DevFS.operations(devFSOperations, 'test', tempDir);
await devFS.create(); await devFS.create();
...@@ -95,7 +103,10 @@ void main() { ...@@ -95,7 +103,10 @@ void main() {
); );
expect(bytes, 48); expect(bytes, 48);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
testUsingContext('add new file to local file system', () async { testUsingContext('add new file to local file system', () async {
final File file = fs.file(fs.path.join(basePath, filePath2)); final File file = fs.file(fs.path.join(basePath, filePath2));
await file.parent.create(recursive: true); await file.parent.create(recursive: true);
...@@ -106,7 +117,10 @@ void main() { ...@@ -106,7 +117,10 @@ void main() {
]); ]);
expect(devFS.assetPathsToEvict, isEmpty); expect(devFS.assetPathsToEvict, isEmpty);
expect(bytes, 7); expect(bytes, 7);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
testUsingContext('modify existing file on local file system', () async { testUsingContext('modify existing file on local file system', () async {
await devFS.update(); await devFS.update();
final File file = fs.file(fs.path.join(basePath, filePath)); final File file = fs.file(fs.path.join(basePath, filePath));
...@@ -124,7 +138,10 @@ void main() { ...@@ -124,7 +138,10 @@ void main() {
]); ]);
expect(devFS.assetPathsToEvict, isEmpty); expect(devFS.assetPathsToEvict, isEmpty);
expect(bytes, 6); expect(bytes, 6);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
testUsingContext('delete a file from the local file system', () async { testUsingContext('delete a file from the local file system', () async {
final File file = fs.file(fs.path.join(basePath, filePath)); final File file = fs.file(fs.path.join(basePath, filePath));
await file.delete(); await file.delete();
...@@ -134,9 +151,12 @@ void main() { ...@@ -134,9 +151,12 @@ void main() {
]); ]);
expect(devFS.assetPathsToEvict, isEmpty); expect(devFS.assetPathsToEvict, isEmpty);
expect(bytes, 0); expect(bytes, 0);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
testUsingContext('add new package', () async { testUsingContext('add new package', () async {
await _createPackage('newpkg', 'anotherfile.txt'); await _createPackage(fs, 'newpkg', 'anotherfile.txt');
final int bytes = await devFS.update(); final int bytes = await devFS.update();
devFSOperations.expectMessages(<String>[ devFSOperations.expectMessages(<String>[
'writeFile test .packages', 'writeFile test .packages',
...@@ -144,10 +164,13 @@ void main() { ...@@ -144,10 +164,13 @@ void main() {
]); ]);
expect(devFS.assetPathsToEvict, isEmpty); expect(devFS.assetPathsToEvict, isEmpty);
expect(bytes, 69); expect(bytes, 69);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
testUsingContext('add new package with double slashes in URI', () async { testUsingContext('add new package with double slashes in URI', () async {
final String packageName = 'doubleslashpkg'; final String packageName = 'doubleslashpkg';
await _createPackage(packageName, 'somefile.txt', doubleSlash: true); await _createPackage(fs, packageName, 'somefile.txt', doubleSlash: true);
final Set<String> fileFilter = new Set<String>(); final Set<String> fileFilter = new Set<String>();
final List<Uri> pkgUris = <Uri>[fs.path.toUri(basePath)]..addAll(_packages.values); final List<Uri> pkgUris = <Uri>[fs.path.toUri(basePath)]..addAll(_packages.values);
...@@ -169,68 +192,88 @@ void main() { ...@@ -169,68 +192,88 @@ void main() {
]); ]);
expect(devFS.assetPathsToEvict, isEmpty); expect(devFS.assetPathsToEvict, isEmpty);
expect(bytes, 109); expect(bytes, 109);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
testUsingContext('add an asset bundle', () async { testUsingContext('add an asset bundle', () async {
assetBundle.entries['a.txt'] = new DevFSStringContent('abc'); assetBundle.entries['a.txt'] = new DevFSStringContent('abc');
final int bytes = await devFS.update(bundle: assetBundle, bundleDirty: true); final int bytes = await devFS.update(bundle: assetBundle, bundleDirty: true);
devFSOperations.expectMessages(<String>[ devFSOperations.expectMessages(<String>[
'writeFile test ${_inAssetBuildDirectory('a.txt')}', 'writeFile test ${_inAssetBuildDirectory(fs, 'a.txt')}',
]); ]);
expect(devFS.assetPathsToEvict, unorderedMatches(<String>['a.txt'])); expect(devFS.assetPathsToEvict, unorderedMatches(<String>['a.txt']));
devFS.assetPathsToEvict.clear(); devFS.assetPathsToEvict.clear();
expect(bytes, 3); expect(bytes, 3);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
testUsingContext('add a file to the asset bundle - bundleDirty', () async { testUsingContext('add a file to the asset bundle - bundleDirty', () async {
assetBundle.entries['b.txt'] = new DevFSStringContent('abcd'); assetBundle.entries['b.txt'] = new DevFSStringContent('abcd');
final int bytes = await devFS.update(bundle: assetBundle, bundleDirty: true); final int bytes = await devFS.update(bundle: assetBundle, bundleDirty: true);
// Expect entire asset bundle written because bundleDirty is true // Expect entire asset bundle written because bundleDirty is true
devFSOperations.expectMessages(<String>[ devFSOperations.expectMessages(<String>[
'writeFile test ${_inAssetBuildDirectory('a.txt')}', 'writeFile test ${_inAssetBuildDirectory(fs, 'a.txt')}',
'writeFile test ${_inAssetBuildDirectory('b.txt')}', 'writeFile test ${_inAssetBuildDirectory(fs, 'b.txt')}',
]); ]);
expect(devFS.assetPathsToEvict, unorderedMatches(<String>[ expect(devFS.assetPathsToEvict, unorderedMatches(<String>[
'a.txt', 'b.txt'])); 'a.txt', 'b.txt']));
devFS.assetPathsToEvict.clear(); devFS.assetPathsToEvict.clear();
expect(bytes, 7); expect(bytes, 7);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
testUsingContext('add a file to the asset bundle', () async { testUsingContext('add a file to the asset bundle', () async {
assetBundle.entries['c.txt'] = new DevFSStringContent('12'); assetBundle.entries['c.txt'] = new DevFSStringContent('12');
final int bytes = await devFS.update(bundle: assetBundle); final int bytes = await devFS.update(bundle: assetBundle);
devFSOperations.expectMessages(<String>[ devFSOperations.expectMessages(<String>[
'writeFile test ${_inAssetBuildDirectory('c.txt')}', 'writeFile test ${_inAssetBuildDirectory(fs, 'c.txt')}',
]); ]);
expect(devFS.assetPathsToEvict, unorderedMatches(<String>[ expect(devFS.assetPathsToEvict, unorderedMatches(<String>[
'c.txt'])); 'c.txt']));
devFS.assetPathsToEvict.clear(); devFS.assetPathsToEvict.clear();
expect(bytes, 2); expect(bytes, 2);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
testUsingContext('delete a file from the asset bundle', () async { testUsingContext('delete a file from the asset bundle', () async {
assetBundle.entries.remove('c.txt'); assetBundle.entries.remove('c.txt');
final int bytes = await devFS.update(bundle: assetBundle); final int bytes = await devFS.update(bundle: assetBundle);
devFSOperations.expectMessages(<String>[ devFSOperations.expectMessages(<String>[
'deleteFile test ${_inAssetBuildDirectory('c.txt')}', 'deleteFile test ${_inAssetBuildDirectory(fs, 'c.txt')}',
]); ]);
expect(devFS.assetPathsToEvict, unorderedMatches(<String>['c.txt'])); expect(devFS.assetPathsToEvict, unorderedMatches(<String>['c.txt']));
devFS.assetPathsToEvict.clear(); devFS.assetPathsToEvict.clear();
expect(bytes, 0); expect(bytes, 0);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
testUsingContext('delete all files from the asset bundle', () async { testUsingContext('delete all files from the asset bundle', () async {
assetBundle.entries.clear(); assetBundle.entries.clear();
final int bytes = await devFS.update(bundle: assetBundle, bundleDirty: true); final int bytes = await devFS.update(bundle: assetBundle, bundleDirty: true);
devFSOperations.expectMessages(<String>[ devFSOperations.expectMessages(<String>[
'deleteFile test ${_inAssetBuildDirectory('a.txt')}', 'deleteFile test ${_inAssetBuildDirectory(fs, 'a.txt')}',
'deleteFile test ${_inAssetBuildDirectory('b.txt')}', 'deleteFile test ${_inAssetBuildDirectory(fs, 'b.txt')}',
]); ]);
expect(devFS.assetPathsToEvict, unorderedMatches(<String>[ expect(devFS.assetPathsToEvict, unorderedMatches(<String>[
'a.txt', 'b.txt' 'a.txt', 'b.txt'
])); ]));
devFS.assetPathsToEvict.clear(); devFS.assetPathsToEvict.clear();
expect(bytes, 0); expect(bytes, 0);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
testUsingContext('delete dev file system', () async { testUsingContext('delete dev file system', () async {
await devFS.destroy(); await devFS.destroy();
devFSOperations.expectMessages(<String>['destroy test']); devFSOperations.expectMessages(<String>['destroy test']);
expect(devFS.assetPathsToEvict, isEmpty); expect(devFS.assetPathsToEvict, isEmpty);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
}); });
...@@ -238,7 +281,7 @@ void main() { ...@@ -238,7 +281,7 @@ void main() {
MockVMService vmService; MockVMService vmService;
setUpAll(() async { setUpAll(() async {
tempDir = _newTempDir(); tempDir = _newTempDir(fs);
basePath = tempDir.path; basePath = tempDir.path;
vmService = new MockVMService(); vmService = new MockVMService();
await vmService.setUp(); await vmService.setUp();
...@@ -255,7 +298,7 @@ void main() { ...@@ -255,7 +298,7 @@ void main() {
file.writeAsBytesSync(<int>[1, 2, 3]); file.writeAsBytesSync(<int>[1, 2, 3]);
// simulate package // simulate package
await _createPackage('somepkg', 'somefile.txt'); await _createPackage(fs, 'somepkg', 'somefile.txt');
devFS = new DevFS(vmService, 'test', tempDir); devFS = new DevFS(vmService, 'test', tempDir);
await devFS.create(); await devFS.create();
...@@ -270,6 +313,8 @@ void main() { ...@@ -270,6 +313,8 @@ void main() {
]); ]);
expect(devFS.assetPathsToEvict, isEmpty); expect(devFS.assetPathsToEvict, isEmpty);
expect(bytes, 48); expect(bytes, 48);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
testUsingContext('delete dev file system', () async { testUsingContext('delete dev file system', () async {
...@@ -277,6 +322,8 @@ void main() { ...@@ -277,6 +322,8 @@ void main() {
await devFS.destroy(); await devFS.destroy();
vmService.expectMessages(<String>['_deleteDevFS {fsName: test}']); vmService.expectMessages(<String>['_deleteDevFS {fsName: test}']);
expect(devFS.assetPathsToEvict, isEmpty); expect(devFS.assetPathsToEvict, isEmpty);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
}); });
}); });
} }
...@@ -349,7 +396,7 @@ class MockVM implements VM { ...@@ -349,7 +396,7 @@ class MockVM implements VM {
final List<Directory> _tempDirs = <Directory>[]; final List<Directory> _tempDirs = <Directory>[];
final Map <String, Uri> _packages = <String, Uri>{}; final Map <String, Uri> _packages = <String, Uri>{};
Directory _newTempDir() { Directory _newTempDir(FileSystem fs) {
final Directory tempDir = fs.systemTempDirectory.createTempSync('devfs${_tempDirs.length}'); final Directory tempDir = fs.systemTempDirectory.createTempSync('devfs${_tempDirs.length}');
_tempDirs.add(tempDir); _tempDirs.add(tempDir);
return tempDir; return tempDir;
...@@ -361,8 +408,8 @@ void _cleanupTempDirs() { ...@@ -361,8 +408,8 @@ void _cleanupTempDirs() {
} }
} }
Future<Null> _createPackage(String pkgName, String pkgFileName, { bool doubleSlash: false }) async { Future<Null> _createPackage(FileSystem fs, String pkgName, String pkgFileName, { bool doubleSlash: false }) async {
final Directory pkgTempDir = _newTempDir(); final Directory pkgTempDir = _newTempDir(fs);
String pkgFilePath = fs.path.join(pkgTempDir.path, pkgName, 'lib', pkgFileName); String pkgFilePath = fs.path.join(pkgTempDir.path, pkgName, 'lib', pkgFileName);
if (doubleSlash) { if (doubleSlash) {
// Force two separators into the path. // Force two separators into the path.
...@@ -380,6 +427,6 @@ Future<Null> _createPackage(String pkgName, String pkgFileName, { bool doubleSla ...@@ -380,6 +427,6 @@ Future<Null> _createPackage(String pkgName, String pkgFileName, { bool doubleSla
fs.file(fs.path.join(_tempDirs[0].path, '.packages')).writeAsStringSync(sb.toString()); fs.file(fs.path.join(_tempDirs[0].path, '.packages')).writeAsStringSync(sb.toString());
} }
String _inAssetBuildDirectory(String filename) { String _inAssetBuildDirectory(FileSystem fs, String filename) {
return '${fs.path.toUri(getAssetBuildDirectory()).path}/$filename'; return '${fs.path.toUri(getAssetBuildDirectory()).path}/$filename';
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment