From 1747adb6308741f503ee5ad45d7a4f170d11ff0e Mon Sep 17 00:00:00 2001 From: Dan Field Date: Wed, 27 Jan 2021 13:09:46 -0800 Subject: [PATCH] Drop mockito for flutter_goldens (#74782) --- packages/flutter_goldens/pubspec.yaml | 5 +- .../test/flutter_goldens_test.dart | 252 ++++++++++++------ .../lib/skia_client.dart | 48 ++-- 3 files changed, 196 insertions(+), 109 deletions(-) diff --git a/packages/flutter_goldens/pubspec.yaml b/packages/flutter_goldens/pubspec.yaml index 6548ed6bd7..d54df08e3d 100644 --- a/packages/flutter_goldens/pubspec.yaml +++ b/packages/flutter_goldens/pubspec.yaml @@ -30,7 +30,4 @@ dependencies: test_api: 0.2.19-nullsafety.6 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" vector_math: 2.1.0-nullsafety.5 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" -dev_dependencies: - mockito: 4.1.1 - -# PUBSPEC CHECKSUM: 49d5 +# PUBSPEC CHECKSUM: ae8f diff --git a/packages/flutter_goldens/test/flutter_goldens_test.dart b/packages/flutter_goldens/test/flutter_goldens_test.dart index bdb95676c0..a322d3b4b1 100644 --- a/packages/flutter_goldens/test/flutter_goldens_test.dart +++ b/packages/flutter_goldens/test/flutter_goldens_test.dart @@ -5,15 +5,17 @@ // @dart = 2.8 import 'dart:async'; +import 'dart:convert'; import 'dart:core'; import 'dart:io'; import 'dart:typed_data'; +import 'dart:ui' show hashValues, hashList; import 'package:file/file.dart'; import 'package:file/memory.dart'; import 'package:flutter_goldens/flutter_goldens.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:mockito/mockito.dart'; +import 'package:meta/meta.dart'; import 'package:platform/platform.dart'; import 'package:process/process.dart'; @@ -49,8 +51,8 @@ Future testWithOutput(String name, Future body(), String expectedOut void main() { MemoryFileSystem fs; FakePlatform platform; - MockProcessManager process; - MockHttpClient mockHttpClient; + FakeProcessManager process; + FakeHttpClient fakeHttpClient; setUp(() { fs = MemoryFileSystem(); @@ -58,8 +60,8 @@ void main() { environment: {'FLUTTER_ROOT': _kFlutterRoot}, operatingSystem: 'macos' ); - process = MockProcessManager(); - mockHttpClient = MockHttpClient(); + process = FakeProcessManager(); + fakeHttpClient = FakeHttpClient(); fs.directory(_kFlutterRoot).createSync(recursive: true); }); @@ -75,7 +77,7 @@ void main() { fs: fs, process: process, platform: platform, - httpClient: mockHttpClient, + httpClient: fakeHttpClient, ); }); @@ -83,15 +85,10 @@ void main() { final File authFile = fs.file('/workDirectory/temp/auth_opt.json') ..createSync(recursive: true); authFile.writeAsStringSync(authTemplate()); - when(process.run(any)) - .thenAnswer((_) => Future - .value(ProcessResult(123, 0, '', ''))); + process.fallbackProcessResult = ProcessResult(123, 0, '', ''); await skiaClient.auth(); - verifyNever(process.run( - captureAny, - workingDirectory: captureAnyNamed('workingDirectory'), - )); + expect(process.workingDirectories, isEmpty); }); test('gsutil is checked when authorization file is present', () async { @@ -119,16 +116,13 @@ void main() { fs: fs, process: process, platform: platform, - httpClient: mockHttpClient, + httpClient: fakeHttpClient, ); - when(process.run(any)) - .thenAnswer((_) => Future - .value(ProcessResult(123, 1, 'fail', 'fail'))); - final Future test = skiaClient.auth(); + process.fallbackProcessResult = ProcessResult(123, 1, 'fail', 'fail'); expect( - test, + skiaClient.auth(), throwsException, ); }); @@ -147,16 +141,14 @@ void main() { fs: fs, process: process, platform: platform, - httpClient: mockHttpClient, + httpClient: fakeHttpClient, ); - when(process.run( + const RunInvocation gitInvocation = RunInvocation( ['git', 'rev-parse', 'HEAD'], - workingDirectory: '/flutter', - )).thenAnswer((_) => Future - .value(ProcessResult(12345678, 0, '12345678', ''))); - - when(process.run( + '/flutter', + ); + const RunInvocation goldctlInvocation = RunInvocation( [ 'goldctl', 'imgtest', 'init', @@ -167,12 +159,13 @@ void main() { '--failure-file', '/workDirectory/failures.json', '--passfail', ], - )).thenAnswer((_) => Future - .value(ProcessResult(123, 1, 'fail', 'fail'))); - final Future test = skiaClient.imgtestInit(); + null, + ); + process.processResults[gitInvocation] = ProcessResult(12345678, 0, '12345678', ''); + process.processResults[goldctlInvocation] = ProcessResult(123, 1, 'fail', 'fail'); expect( - test, + skiaClient.imgtestInit(), throwsException, ); }); @@ -194,7 +187,7 @@ void main() { fs: fs, process: process, platform: platform, - httpClient: mockHttpClient, + httpClient: fakeHttpClient, ); final List ciArguments = skiaClient.getCIArguments(); @@ -229,7 +222,7 @@ void main() { fs: fs, process: process, platform: platform, - httpClient: mockHttpClient, + httpClient: fakeHttpClient, ); traceID = skiaClient.getTraceID('flutter.golden.1'); @@ -257,7 +250,7 @@ void main() { fs: fs, process: process, platform: platform, - httpClient: mockHttpClient, + httpClient: fakeHttpClient, ); traceID = skiaClient.getTraceID('flutter.golden.1'); @@ -280,7 +273,7 @@ void main() { fs: fs, process: process, platform: platform, - httpClient: mockHttpClient, + httpClient: fakeHttpClient, ); traceID = skiaClient.getTraceID('flutter.golden.1'); @@ -302,17 +295,17 @@ void main() { final Uri imageUrl = Uri.parse( 'https://flutter-gold.skia.org/img/images/$expectation.png' ); - final MockHttpClientRequest mockImageRequest = MockHttpClientRequest(); - final MockHttpImageResponse mockImageResponse = MockHttpImageResponse( + final FakeHttpClientRequest fakeImageRequest = FakeHttpClientRequest(); + final FakeHttpImageResponse fakeImageResponse = FakeHttpImageResponse( imageResponseTemplate() ); - when(mockHttpClient.getUrl(imageUrl)) - .thenAnswer((_) => Future.value(mockImageRequest)); - when(mockImageRequest.close()) - .thenAnswer((_) => Future.value(mockImageResponse)); + + fakeHttpClient.request = fakeImageRequest; + fakeImageRequest.response = fakeImageResponse; final List masterBytes = await skiaClient.getImageBytes(expectation); + expect(fakeHttpClient.lastUri, imageUrl); expect(masterBytes, equals(_kTestPngBytes)); }); }); @@ -326,17 +319,17 @@ void main() { ..createSync(recursive: true); comparator = FlutterPostSubmitFileComparator( basedir.uri, - MockSkiaGoldClient(), + FakeSkiaGoldClient(), fs: fs, platform: platform, ); }); test('calculates the basedir correctly from defaultComparator for local testing', () async { - final MockLocalFileComparator defaultComparator = MockLocalFileComparator(); + final FakeLocalFileComparator defaultComparator = FakeLocalFileComparator(); final Directory flutterRoot = fs.directory(platform.environment['FLUTTER_ROOT']) ..createSync(recursive: true); - when(defaultComparator.basedir).thenReturn(flutterRoot.childDirectory('baz').uri); + defaultComparator.basedir = flutterRoot.childDirectory('baz').uri; final Directory basedir = FlutterGoldenFileComparator.getBaseDirectory( defaultComparator, @@ -355,14 +348,14 @@ void main() { }); group('Post-Submit', () { - final MockSkiaGoldClient mockSkiaClient = MockSkiaGoldClient(); + final FakeSkiaGoldClient fakeSkiaClient = FakeSkiaGoldClient(); setUp(() { final Directory basedir = fs.directory('flutter/test/library/') ..createSync(recursive: true); comparator = FlutterPostSubmitFileComparator( basedir.uri, - mockSkiaClient, + fakeSkiaClient, fs: fs, platform: platform, ); @@ -561,14 +554,14 @@ void main() { group('Local', () { FlutterLocalFileComparator comparator; - final MockSkiaGoldClient mockSkiaClient = MockSkiaGoldClient(); + final FakeSkiaGoldClient fakeSkiaClient = FakeSkiaGoldClient(); setUp(() async { final Directory basedir = fs.directory('flutter/test/library/') ..createSync(recursive: true); comparator = FlutterLocalFileComparator( basedir.uri, - mockSkiaClient, + fakeSkiaClient, fs: fs, platform: FakePlatform( environment: {'FLUTTER_ROOT': _kFlutterRoot}, @@ -576,14 +569,11 @@ void main() { ), ); - when(mockSkiaClient.getExpectationForTest('flutter.golden_test.1')) - .thenAnswer((_) => Future.value('55109a4bed52acc780530f7a9aeff6c0')); - when(mockSkiaClient.getExpectationForTest('flutter.new_golden_test.2')) - .thenAnswer((_) => Future.value('')); - when(mockSkiaClient.getImageBytes('55109a4bed52acc780530f7a9aeff6c0')) - .thenAnswer((_) => Future>.value(_kTestPngBytes)); - when(mockSkiaClient.cleanTestName('library.flutter.golden_test.1.png')) - .thenReturn('flutter.golden_test.1'); + const String hash = '55109a4bed52acc780530f7a9aeff6c0'; + fakeSkiaClient.expectationForTestValues['flutter.golden_test.1'] = hash; + fakeSkiaClient.expectationForTestValues['flutter.new_golden_test.1'] = ''; + fakeSkiaClient.imageBytesValues[hash] =_kTestPngBytes; + fakeSkiaClient.cleanTestNameValues['library.flutter.golden_test.1.png'] = 'flutter.golden_test.1'; }); test('passes when bytes match', () async { @@ -639,25 +629,25 @@ void main() { }); test('returns FlutterSkippingGoldenFileComparator when network connection is unavailable', () async { - final MockDirectory mockDirectory = MockDirectory(); - when(mockDirectory.existsSync()).thenReturn(true); - when(mockDirectory.uri).thenReturn(Uri.parse('/flutter')); + final FakeDirectory fakeDirectory = FakeDirectory(); + fakeDirectory.existsSyncValue = true; + fakeDirectory.uri = Uri.parse('/flutter'); + + fakeSkiaClient.getExpectationForTestThrowable = const OSError("Can't reach Gold"); - when(mockSkiaClient.getExpectationForTest(any)) - .thenAnswer((_) => throw const OSError("Can't reach Gold")); FlutterGoldenFileComparator comparator = await FlutterLocalFileComparator.fromDefaultComparator( platform, - goldens: mockSkiaClient, - baseDirectory: mockDirectory, + goldens: fakeSkiaClient, + baseDirectory: fakeDirectory, ); expect(comparator.runtimeType, FlutterSkippingFileComparator); - when(mockSkiaClient.getExpectationForTest(any)) - .thenAnswer((_) => throw const SocketException("Can't reach Gold")); + fakeSkiaClient.getExpectationForTestThrowable = const SocketException("Can't reach Gold"); + comparator = await FlutterLocalFileComparator.fromDefaultComparator( platform, - goldens: mockSkiaClient, - baseDirectory: mockDirectory, + goldens: fakeSkiaClient, + baseDirectory: fakeDirectory, ); expect(comparator.runtimeType, FlutterSkippingFileComparator); }); @@ -665,20 +655,130 @@ void main() { }); } -class MockProcessManager extends Mock implements ProcessManager {} +@immutable +class RunInvocation { + const RunInvocation(this.command, this.workingDirectory); -class MockSkiaGoldClient extends Mock implements SkiaGoldClient {} + final List command; + final String workingDirectory; -class MockLocalFileComparator extends Mock implements LocalFileComparator {} + @override + int get hashCode => hashValues(hashList(command), workingDirectory); -class MockDirectory extends Mock implements Directory {} + bool _commandEquals(List other) { + if (other == command) { + return true; + } + if (other.length != command.length) { + return false; + } + for (int index = 0; index < other.length; index += 1) { + if (other[index] != command[index]) { + return false; + } + } + return true; + } -class MockHttpClient extends Mock implements HttpClient {} + @override + bool operator ==(Object other) { + if (other.runtimeType != runtimeType) { + return false; + } + return other is RunInvocation + && _commandEquals(other.command) + && other.workingDirectory == workingDirectory; + } -class MockHttpClientRequest extends Mock implements HttpClientRequest {} + @override + String toString() => '$command ($workingDirectory)'; +} -class MockHttpClientResponse extends Mock implements HttpClientResponse { - MockHttpClientResponse(this.response); +class FakeProcessManager extends Fake implements ProcessManager { + Map processResults = {}; + + /// Used if [processResults] does not contain an matching invocation. + ProcessResult fallbackProcessResult; + + final List workingDirectories = []; + + @override + Future run( + List command, { + String workingDirectory, + Map environment, + bool includeParentEnvironment = true, + bool runInShell = false, + Encoding stdoutEncoding = systemEncoding, + Encoding stderrEncoding = systemEncoding, + }) async { + workingDirectories.add(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'); + } + return result ?? fallbackProcessResult; + } +} + +class FakeSkiaGoldClient extends Fake implements SkiaGoldClient { + Map expectationForTestValues = {}; + Object getExpectationForTestThrowable; + @override + Future getExpectationForTest(String testName) async { + if (getExpectationForTestThrowable != null) { + throw getExpectationForTestThrowable; + } + return expectationForTestValues[testName]; + } + + Map> imageBytesValues = >{}; + @override + Future> getImageBytes(String imageHash) async => imageBytesValues[imageHash]; + + Map cleanTestNameValues = {}; + @override + String cleanTestName(String fileName) => cleanTestNameValues[fileName]; +} + +class FakeLocalFileComparator extends Fake implements LocalFileComparator { + @override + Uri basedir; +} + +class FakeDirectory extends Fake implements Directory { + bool existsSyncValue; + @override + bool existsSync() => existsSyncValue; + + @override + Uri uri; +} + +class FakeHttpClient extends Fake implements HttpClient { + Uri lastUri; + FakeHttpClientRequest request; + + @override + Future getUrl(Uri url) async { + lastUri = url; + return request; + } +} + +class FakeHttpClientRequest extends Fake implements HttpClientRequest { + FakeHttpImageResponse response; + + @override + Future close() async { + return response; + } +} + +class FakeHttpClientResponse extends Fake implements HttpClientResponse { + FakeHttpClientResponse(this.response); final List response; @@ -694,8 +794,8 @@ class MockHttpClientResponse extends Mock implements HttpClientResponse { } } -class MockHttpImageResponse extends Mock implements HttpClientResponse { - MockHttpImageResponse(this.response); +class FakeHttpImageResponse extends Fake implements HttpClientResponse { + FakeHttpImageResponse(this.response); final List> response; diff --git a/packages/flutter_goldens_client/lib/skia_client.dart b/packages/flutter_goldens_client/lib/skia_client.dart index 6f1ff71b7a..36586821b0 100644 --- a/packages/flutter_goldens_client/lib/skia_client.dart +++ b/packages/flutter_goldens_client/lib/skia_client.dart @@ -79,7 +79,8 @@ class SkiaGoldClient { Future auth() async { if (await clientIsAuthorized()) return; - final List authArguments = [ + final List authCommand = [ + _goldctl, 'auth', '--work-dir', workDirectory .childDirectory('temp') @@ -87,10 +88,7 @@ class SkiaGoldClient { '--luci', ]; - final io.ProcessResult result = await io.Process.run( - _goldctl, - authArguments, - ); + final io.ProcessResult result = await process.run(authCommand); if (result.exitCode != 0) { final StringBuffer buf = StringBuffer() @@ -118,7 +116,8 @@ class SkiaGoldClient { await failures.create(); final String commitHash = await _getCurrentCommit(); - final List imgtestInitArguments = [ + final List imgtestInitCommand = [ + _goldctl, 'imgtest', 'init', '--instance', 'flutter', '--work-dir', workDirectory @@ -130,19 +129,16 @@ class SkiaGoldClient { '--passfail', ]; - if (imgtestInitArguments.contains(null)) { + if (imgtestInitCommand.contains(null)) { final StringBuffer buf = StringBuffer() ..writeln('A null argument was provided for Skia Gold imgtest init.') ..writeln('Please confirm the settings of your golden file test.') ..writeln('Arguments provided:'); - imgtestInitArguments.forEach(buf.writeln); + imgtestInitCommand.forEach(buf.writeln); throw Exception(buf.toString()); } - final io.ProcessResult result = await io.Process.run( - _goldctl, - imgtestInitArguments, - ); + final io.ProcessResult result = await process.run(imgtestInitCommand); if (result.exitCode != 0) { final StringBuffer buf = StringBuffer() @@ -167,7 +163,8 @@ class SkiaGoldClient { /// The [testName] and [goldenFile] parameters reference the current /// comparison being evaluated by the [FlutterPostSubmitFileComparator]. Future imgtestAdd(String testName, File goldenFile) async { - final List imgtestArguments = [ + final List imgtestCommand = [ + _goldctl, 'imgtest', 'add', '--work-dir', workDirectory .childDirectory('temp') @@ -176,10 +173,7 @@ class SkiaGoldClient { '--png-file', goldenFile.path, ]; - final io.ProcessResult result = await io.Process.run( - _goldctl, - imgtestArguments, - ); + final io.ProcessResult result = await process.run(imgtestCommand); if (result.exitCode != 0) { // We do not want to throw for non-zero exit codes here, as an intentional @@ -205,7 +199,8 @@ class SkiaGoldClient { await failures.create(); final String commitHash = await _getCurrentCommit(); - final List imgtestInitArguments = [ + final List imgtestInitCommand = [ + _goldctl, 'imgtest', 'init', '--instance', 'flutter', '--work-dir', workDirectory @@ -220,19 +215,16 @@ class SkiaGoldClient { ...getCIArguments(), ]; - if (imgtestInitArguments.contains(null)) { + if (imgtestInitCommand.contains(null)) { final StringBuffer buf = StringBuffer() ..writeln('A null argument was provided for Skia Gold tryjob init.') ..writeln('Please confirm the settings of your golden file test.') ..writeln('Arguments provided:'); - imgtestInitArguments.forEach(buf.writeln); + imgtestInitCommand.forEach(buf.writeln); throw Exception(buf.toString()); } - final io.ProcessResult result = await io.Process.run( - _goldctl, - imgtestInitArguments, - ); + final io.ProcessResult result = await process.run(imgtestInitCommand); if (result.exitCode != 0) { final StringBuffer buf = StringBuffer() @@ -257,7 +249,8 @@ class SkiaGoldClient { /// The [testName] and [goldenFile] parameters reference the current /// comparison being evaluated by the [FlutterPreSubmitFileComparator]. Future tryjobAdd(String testName, File goldenFile) async { - final List imgtestArguments = [ + final List imgtestCommand = [ + _goldctl, 'imgtest', 'add', '--work-dir', workDirectory .childDirectory('temp') @@ -266,10 +259,7 @@ class SkiaGoldClient { '--png-file', goldenFile.path, ]; - final io.ProcessResult result = await io.Process.run( - _goldctl, - imgtestArguments, - ); + final io.ProcessResult result = await process.run(imgtestCommand); final String/*!*/ resultStdout = result.stdout.toString(); if (result.exitCode != 0 &&