From 251e82d2111fe9821ad6038c57842f535598d6cc Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Fri, 31 Aug 2018 13:31:56 -0700 Subject: [PATCH] Migrate devfs tests to Dart 2 (#21285) DevFS.update only runs in Dart 2 mode when the generator parameter is supplied. In Dart 2 mode, both mainPath and pathToReload are required parameters; this patch marks them as such. generator is required for running in Dart 2. All call sites other than tests already explicitly set this value. Note the statements on line 510 and line 516 for why mainPath and pathToReload are required. --- packages/flutter_tools/lib/src/devfs.dart | 7 +- packages/flutter_tools/test/devfs_test.dart | 183 +++++++++++++------- packages/flutter_tools/test/src/mocks.dart | 33 ++++ 3 files changed, 155 insertions(+), 68 deletions(-) diff --git a/packages/flutter_tools/lib/src/devfs.dart b/packages/flutter_tools/lib/src/devfs.dart index 3d31b833db..4689e4c830 100644 --- a/packages/flutter_tools/lib/src/devfs.dart +++ b/packages/flutter_tools/lib/src/devfs.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'dart:convert' show base64, utf8; import 'package:json_rpc_2/json_rpc_2.dart' as rpc; +import 'package:meta/meta.dart'; import 'asset.dart'; import 'base/context.dart'; @@ -401,18 +402,18 @@ class DevFS { /// /// Returns the number of bytes synced. Future update({ - String mainPath, + @required String mainPath, String target, AssetBundle bundle, DateTime firstBuildTime, bool bundleFirstUpload = false, bool bundleDirty = false, Set fileFilter, - ResidentCompiler generator, + @required ResidentCompiler generator, String dillOutputPath, bool fullRestart = false, String projectRootPath, - String pathToReload, + @required String pathToReload, }) async { // Mark all entries as possibly deleted. for (DevFSContent content in _entries.values) { diff --git a/packages/flutter_tools/test/devfs_test.dart b/packages/flutter_tools/test/devfs_test.dart index b0d33a135d..52ade17329 100644 --- a/packages/flutter_tools/test/devfs_test.dart +++ b/packages/flutter_tools/test/devfs_test.dart @@ -66,6 +66,7 @@ void main() { group('devfs local', () { final MockDevFSOperations devFSOperations = new MockDevFSOperations(); + final MockResidentCompiler residentCompiler = new MockResidentCompiler(); setUpAll(() { tempDir = _newTempDir(fs); @@ -88,22 +89,17 @@ void main() { devFSOperations.expectMessages(['create test']); expect(devFS.assetPathsToEvict, isEmpty); - final int bytes = await devFS.update(); + final int bytes = await devFS.update( + mainPath: 'lib/foo.txt', + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); devFSOperations.expectMessages([ - 'writeFile test .packages', - 'writeFile test lib/foo.txt', - 'writeFile test packages/somepkg/somefile.txt', + 'writeFile test lib/foo.txt.dill', ]); expect(devFS.assetPathsToEvict, isEmpty); - final List packageSpecOnDevice = LineSplitter.split(utf8.decode( - await devFSOperations.devicePathToContent[fs.path.toUri('.packages')].contentsAsBytes() - )).toList(); - expect(packageSpecOnDevice, - unorderedEquals(['my_project:lib/', 'somepkg:packages/somepkg/']) - ); - - expect(bytes, 48); + expect(bytes, 22); }, overrides: { FileSystem: () => fs, }); @@ -112,48 +108,57 @@ void main() { final File file = fs.file(fs.path.join(basePath, filePath2)); await file.parent.create(recursive: true); file.writeAsBytesSync([1, 2, 3, 4, 5, 6, 7]); - final int bytes = await devFS.update(); + final int bytes = await devFS.update( + mainPath: 'lib/foo.txt', + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); devFSOperations.expectMessages([ - 'writeFile test foo/bar.txt', + 'writeFile test lib/foo.txt.dill', ]); expect(devFS.assetPathsToEvict, isEmpty); - expect(bytes, 7); - }, overrides: { - FileSystem: () => fs, - }); - - testUsingContext('add new file to local file system and preserve unusual file name casing', () async { - final String filePathWithUnusualCasing = fs.path.join('FooBar', 'TEST.txt'); - final File file = fs.file(fs.path.join(basePath, filePathWithUnusualCasing)); - await file.parent.create(recursive: true); - file.writeAsBytesSync([1, 2, 3, 4, 5, 6, 7]); - final int bytes = await devFS.update(); - devFSOperations.expectMessages([ - 'writeFile test FooBar/TEST.txt', - ]); - expect(devFS.assetPathsToEvict, isEmpty); - expect(bytes, 7); + expect(bytes, 22); }, overrides: { FileSystem: () => fs, }); testUsingContext('modify existing file on local file system', () async { - await devFS.update(); + int bytes = await devFS.update( + mainPath: 'lib/foo.txt', + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); + devFSOperations.expectMessages([ + 'writeFile test lib/foo.txt.dill', + ]); + expect(devFS.assetPathsToEvict, isEmpty); + expect(bytes, 22); + final File file = fs.file(fs.path.join(basePath, filePath)); // Set the last modified time to 5 seconds in the past. updateFileModificationTime(file.path, new DateTime.now(), -5); - int bytes = await devFS.update(); - devFSOperations.expectMessages([]); - expect(devFS.assetPathsToEvict, isEmpty); - expect(bytes, 0); - - await file.writeAsBytes([1, 2, 3, 4, 5, 6]); - bytes = await devFS.update(); + bytes = await devFS.update( + mainPath: 'lib/foo.txt', + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); devFSOperations.expectMessages([ - 'writeFile test lib/foo.txt', + 'writeFile test lib/foo.txt.dill', ]); expect(devFS.assetPathsToEvict, isEmpty); - expect(bytes, 6); + expect(bytes, 22); + + await file.writeAsBytes([1, 2, 3, 4, 5, 6]); + bytes = await devFS.update( + mainPath: 'lib/foo.txt', + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); + devFSOperations.expectMessages([ + 'writeFile test lib/foo.txt.dill', + ]); + expect(devFS.assetPathsToEvict, isEmpty); + expect(bytes, 22); }, overrides: { FileSystem: () => fs, }); @@ -161,25 +166,33 @@ void main() { testUsingContext('delete a file from the local file system', () async { final File file = fs.file(fs.path.join(basePath, filePath)); await file.delete(); - final int bytes = await devFS.update(); + final int bytes = await devFS.update( + mainPath: 'lib/foo.txt', + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); devFSOperations.expectMessages([ 'deleteFile test lib/foo.txt', + 'writeFile test lib/foo.txt.dill', ]); expect(devFS.assetPathsToEvict, isEmpty); - expect(bytes, 0); + expect(bytes, 22); }, overrides: { FileSystem: () => fs, }); testUsingContext('add new package', () async { await _createPackage(fs, 'newpkg', 'anotherfile.txt'); - final int bytes = await devFS.update(); + final int bytes = await devFS.update( + mainPath: 'lib/foo.txt', + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); devFSOperations.expectMessages([ - 'writeFile test .packages', - 'writeFile test packages/newpkg/anotherfile.txt', + 'writeFile test lib/foo.txt.dill', ]); expect(devFS.assetPathsToEvict, isEmpty); - expect(bytes, 69); + expect(bytes, 22); }, overrides: { FileSystem: () => fs, }); @@ -200,85 +213,122 @@ void main() { .map((File file) => canonicalizePath(file.path)) .toList()); } - final int bytes = await devFS.update(fileFilter: fileFilter); + final int bytes = await devFS.update( + mainPath: 'lib/foo.txt', + fileFilter: fileFilter, + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); devFSOperations.expectMessages([ - 'writeFile test .packages', - 'writeFile test packages/doubleslashpkg/somefile.txt', + 'writeFile test lib/foo.txt.dill', ]); expect(devFS.assetPathsToEvict, isEmpty); - expect(bytes, 109); + expect(bytes, 22); }, overrides: { FileSystem: () => fs, }); testUsingContext('add an asset bundle', () async { assetBundle.entries['a.txt'] = new DevFSStringContent('abc'); - final int bytes = await devFS.update(bundle: assetBundle, bundleDirty: true); + final int bytes = await devFS.update( + mainPath: 'lib/foo.txt', + bundle: assetBundle, + bundleDirty: true, + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); devFSOperations.expectMessages([ 'writeFile test ${_inAssetBuildDirectory(fs, 'a.txt')}', + 'writeFile test lib/foo.txt.dill', ]); expect(devFS.assetPathsToEvict, unorderedMatches(['a.txt'])); devFS.assetPathsToEvict.clear(); - expect(bytes, 3); + expect(bytes, 25); }, overrides: { FileSystem: () => fs, }); testUsingContext('add a file to the asset bundle - bundleDirty', () async { assetBundle.entries['b.txt'] = new DevFSStringContent('abcd'); - final int bytes = await devFS.update(bundle: assetBundle, bundleDirty: true); + final int bytes = await devFS.update( + mainPath: 'lib/foo.txt', + bundle: assetBundle, + bundleDirty: true, + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); // Expect entire asset bundle written because bundleDirty is true devFSOperations.expectMessages([ 'writeFile test ${_inAssetBuildDirectory(fs, 'a.txt')}', 'writeFile test ${_inAssetBuildDirectory(fs, 'b.txt')}', + 'writeFile test lib/foo.txt.dill', ]); expect(devFS.assetPathsToEvict, unorderedMatches([ 'a.txt', 'b.txt'])); devFS.assetPathsToEvict.clear(); - expect(bytes, 7); + expect(bytes, 29); }, overrides: { FileSystem: () => fs, }); testUsingContext('add a file to the asset bundle', () async { assetBundle.entries['c.txt'] = new DevFSStringContent('12'); - final int bytes = await devFS.update(bundle: assetBundle); + final int bytes = await devFS.update( + mainPath: 'lib/foo.txt', + bundle: assetBundle, + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); devFSOperations.expectMessages([ 'writeFile test ${_inAssetBuildDirectory(fs, 'c.txt')}', + 'writeFile test lib/foo.txt.dill', ]); expect(devFS.assetPathsToEvict, unorderedMatches([ 'c.txt'])); devFS.assetPathsToEvict.clear(); - expect(bytes, 2); + expect(bytes, 24); }, overrides: { FileSystem: () => fs, }); testUsingContext('delete a file from the asset bundle', () async { assetBundle.entries.remove('c.txt'); - final int bytes = await devFS.update(bundle: assetBundle); + final int bytes = await devFS.update( + mainPath: 'lib/foo.txt', + bundle: assetBundle, + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); devFSOperations.expectMessages([ 'deleteFile test ${_inAssetBuildDirectory(fs, 'c.txt')}', + 'writeFile test lib/foo.txt.dill', ]); expect(devFS.assetPathsToEvict, unorderedMatches(['c.txt'])); devFS.assetPathsToEvict.clear(); - expect(bytes, 0); + expect(bytes, 22); }, overrides: { FileSystem: () => fs, }); testUsingContext('delete all files from the asset bundle', () async { assetBundle.entries.clear(); - final int bytes = await devFS.update(bundle: assetBundle, bundleDirty: true); + final int bytes = await devFS.update( + mainPath: 'lib/foo.txt', + bundle: assetBundle, + bundleDirty: true, + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); devFSOperations.expectMessages([ 'deleteFile test ${_inAssetBuildDirectory(fs, 'a.txt')}', 'deleteFile test ${_inAssetBuildDirectory(fs, 'b.txt')}', + 'writeFile test lib/foo.txt.dill', ]); expect(devFS.assetPathsToEvict, unorderedMatches([ 'a.txt', 'b.txt' ])); devFS.assetPathsToEvict.clear(); - expect(bytes, 0); + expect(bytes, 22); }, overrides: { FileSystem: () => fs, }); @@ -294,6 +344,7 @@ void main() { group('devfs remote', () { MockVMService vmService; + final MockResidentCompiler residentCompiler = new MockResidentCompiler(); setUpAll(() async { tempDir = _newTempDir(fs); @@ -320,14 +371,16 @@ void main() { vmService.expectMessages(['create test']); expect(devFS.assetPathsToEvict, isEmpty); - final int bytes = await devFS.update(); + final int bytes = await devFS.update( + mainPath: 'lib/foo.txt', + generator: residentCompiler, + pathToReload: 'lib/foo.txt.dill', + ); vmService.expectMessages([ - 'writeFile test .packages', - 'writeFile test lib/foo.txt', - 'writeFile test packages/somepkg/somefile.txt', + 'writeFile test lib/foo.txt.dill', ]); expect(devFS.assetPathsToEvict, isEmpty); - expect(bytes, 48); + expect(bytes, 22); }, overrides: { FileSystem: () => fs, }); diff --git a/packages/flutter_tools/test/src/mocks.dart b/packages/flutter_tools/test/src/mocks.dart index 2ed3e0a87b..b87a343ec8 100644 --- a/packages/flutter_tools/test/src/mocks.dart +++ b/packages/flutter_tools/test/src/mocks.dart @@ -12,6 +12,7 @@ import 'package:flutter_tools/src/application_package.dart'; import 'package:flutter_tools/src/base/file_system.dart' hide IOSink; import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/build_info.dart'; +import 'package:flutter_tools/src/compile.dart'; import 'package:flutter_tools/src/devfs.dart'; import 'package:flutter_tools/src/device.dart'; import 'package:flutter_tools/src/ios/devices.dart'; @@ -431,3 +432,35 @@ class MockDevFSOperations extends BasicMock implements DevFSOperations { devicePathToContent.remove(deviceUri); } } + +class MockResidentCompiler extends BasicMock implements ResidentCompiler { + @override + void accept() {} + + @override + void reject() {} + + @override + void reset() {} + + @override + Future shutdown() async {} + + @override + Future compileExpression( + String expression, + List definitions, + List typeDefinitions, + String libraryUri, + String klass, + bool isStatic + ) async { + return null; + } + @override + Future recompile(String mainPath, List invalidatedFiles, {String outputPath, String packagesFilePath}) async { + fs.file(outputPath).createSync(recursive: true); + fs.file(outputPath).writeAsStringSync('compiled_kernel_output'); + return new CompilerOutput(outputPath, 0); + } +}