From f04616f72340c0b2a43aba97b9e7b4982f31ebfc Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Wed, 27 Jan 2021 19:29:04 -0600 Subject: [PATCH] Migrate flutter_goldens to null safety (#74853) --- dev/bots/test.dart | 2 +- .../test/flutter_goldens_test.dart | 72 +++++++++---------- 2 files changed, 35 insertions(+), 39 deletions(-) diff --git a/dev/bots/test.dart b/dev/bots/test.dart index 7436c11000..b687ecce99 100644 --- a/dev/bots/test.dart +++ b/dev/bots/test.dart @@ -671,7 +671,7 @@ Future _runFrameworkTests() async { await _runFlutterTest(path.join(flutterRoot, 'dev', 'benchmarks', 'test_apps', 'stocks')); await _runFlutterTest(path.join(flutterRoot, 'packages', 'flutter_driver'), tests: [path.join('test', 'src', 'real_tests')]); await _runFlutterTest(path.join(flutterRoot, 'packages', 'integration_test')); - await _runFlutterTest(path.join(flutterRoot, 'packages', 'flutter_goldens')); + await _runFlutterTest(path.join(flutterRoot, 'packages', 'flutter_goldens'), options: soundNullSafetyOptions); await _runFlutterTest(path.join(flutterRoot, 'packages', 'flutter_localizations'), options: soundNullSafetyOptions); await _runFlutterTest(path.join(flutterRoot, 'packages', 'flutter_test'), options: soundNullSafetyOptions); await _runFlutterTest(path.join(flutterRoot, 'packages', 'fuchsia_remote_debug_protocol'), options: soundNullSafetyOptions); diff --git a/packages/flutter_goldens/test/flutter_goldens_test.dart b/packages/flutter_goldens/test/flutter_goldens_test.dart index a322d3b4b1..1261edc0c7 100644 --- a/packages/flutter_goldens/test/flutter_goldens_test.dart +++ b/packages/flutter_goldens/test/flutter_goldens_test.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - import 'dart:async'; import 'dart:convert'; import 'dart:core'; @@ -49,10 +47,10 @@ Future testWithOutput(String name, Future body(), String expectedOut } void main() { - MemoryFileSystem fs; - FakePlatform platform; - FakeProcessManager process; - FakeHttpClient fakeHttpClient; + late MemoryFileSystem fs; + late FakePlatform platform; + late FakeProcessManager process; + late FakeHttpClient fakeHttpClient; setUp(() { fs = MemoryFileSystem(); @@ -66,8 +64,8 @@ void main() { }); group('SkiaGoldClient', () { - SkiaGoldClient skiaClient; - Directory workDirectory; + late SkiaGoldClient skiaClient; + late Directory workDirectory; setUp(() { workDirectory = fs.directory('/workDirectory') @@ -144,6 +142,8 @@ void main() { httpClient: fakeHttpClient, ); + process.fallbackProcessResult = ProcessResult(123, 1, 'fail', 'fail'); + const RunInvocation gitInvocation = RunInvocation( ['git', 'rev-parse', 'HEAD'], '/flutter', @@ -285,11 +285,7 @@ void main() { }); group('Request Handling', () { - String expectation; - - setUp(() { - expectation = '55109a4bed52acc780530f7a9aeff6c0'; - }); + const String expectation = '55109a4bed52acc780530f7a9aeff6c0'; test('image bytes are processed properly', () async { final Uri imageUrl = Uri.parse( @@ -312,7 +308,7 @@ void main() { }); group('FlutterGoldenFileComparator', () { - FlutterPostSubmitFileComparator comparator; + late FlutterPostSubmitFileComparator comparator; setUp(() { final Directory basedir = fs.directory('flutter/test/library/') @@ -553,7 +549,7 @@ void main() { }); group('Local', () { - FlutterLocalFileComparator comparator; + late FlutterLocalFileComparator comparator; final FakeSkiaGoldClient fakeSkiaClient = FakeSkiaGoldClient(); setUp(() async { @@ -660,7 +656,7 @@ class RunInvocation { const RunInvocation(this.command, this.workingDirectory); final List command; - final String workingDirectory; + final String? workingDirectory; @override int get hashCode => hashValues(hashList(command), workingDirectory); @@ -697,69 +693,69 @@ class RunInvocation { class FakeProcessManager extends Fake implements ProcessManager { Map processResults = {}; - /// Used if [processResults] does not contain an matching invocation. - ProcessResult fallbackProcessResult; + /// Used if [processResults] does not contain a matching invocation. + ProcessResult? fallbackProcessResult; final List workingDirectories = []; @override Future run( List command, { - String workingDirectory, - Map environment, + String workingDirectory = '', + Map environment = const {}, bool includeParentEnvironment = true, bool runInShell = false, Encoding stdoutEncoding = systemEncoding, Encoding stderrEncoding = systemEncoding, }) async { workingDirectories.add(workingDirectory); - final ProcessResult result = processResults[RunInvocation(command.cast(), workingDirectory)]; + final ProcessResult? result = processResults[RunInvocation(command.cast(), workingDirectory)]; if (result == null && fallbackProcessResult == null) { // Throwing here might gobble up the exception message if a test fails. print('ProcessManager.run was called with $command ($workingDirectory) unexpectedly - $processResults.'); - fail('see above'); + fail('See above.'); } - return result ?? fallbackProcessResult; + return result ?? fallbackProcessResult!; } } class FakeSkiaGoldClient extends Fake implements SkiaGoldClient { Map expectationForTestValues = {}; - Object getExpectationForTestThrowable; + Object? getExpectationForTestThrowable; @override Future getExpectationForTest(String testName) async { if (getExpectationForTestThrowable != null) { - throw getExpectationForTestThrowable; + throw getExpectationForTestThrowable!; } - return expectationForTestValues[testName]; + return expectationForTestValues[testName] ?? ''; } Map> imageBytesValues = >{}; @override - Future> getImageBytes(String imageHash) async => imageBytesValues[imageHash]; + Future> getImageBytes(String imageHash) async => imageBytesValues[imageHash]!; Map cleanTestNameValues = {}; @override - String cleanTestName(String fileName) => cleanTestNameValues[fileName]; + String cleanTestName(String fileName) => cleanTestNameValues[fileName] ?? ''; } class FakeLocalFileComparator extends Fake implements LocalFileComparator { @override - Uri basedir; + late Uri basedir; } class FakeDirectory extends Fake implements Directory { - bool existsSyncValue; + late bool existsSyncValue; @override bool existsSync() => existsSyncValue; @override - Uri uri; + late Uri uri; } class FakeHttpClient extends Fake implements HttpClient { - Uri lastUri; - FakeHttpClientRequest request; + late Uri lastUri; + late FakeHttpClientRequest request; @override Future getUrl(Uri url) async { @@ -769,7 +765,7 @@ class FakeHttpClient extends Fake implements HttpClient { } class FakeHttpClientRequest extends Fake implements HttpClientRequest { - FakeHttpImageResponse response; + late FakeHttpImageResponse response; @override Future close() async { @@ -784,10 +780,10 @@ class FakeHttpClientResponse extends Fake implements HttpClientResponse { @override StreamSubscription> listen( - void onData(List event), { - Function onError, - void onDone(), - bool cancelOnError, + void onData(List event)?, { + Function? onError, + void onDone()?, + bool? cancelOnError, }) { return Stream>.fromFuture(Future>.value(response)) .listen(onData, onError: onError, onDone: onDone, cancelOnError: cancelOnError);