Unverified Commit 027d4eca authored by Michael Goderbauer's avatar Michael Goderbauer Committed by GitHub

Revert "[flutter_tools] remove mocks, globals from golden comparator and test...

Revert "[flutter_tools] remove mocks, globals from golden comparator and test runner tests (#81423)" (#81487)

This reverts commit 8fe4cc79.
parent 8fe4cc79
...@@ -4,44 +4,33 @@ ...@@ -4,44 +4,33 @@
// @dart = 2.8 // @dart = 2.8
import 'dart:async'; import 'dart:async';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:meta/meta.dart';
import 'package:process/process.dart';
import '../base/common.dart'; import '../base/common.dart';
import '../base/file_system.dart'; import '../base/file_system.dart';
import '../base/io.dart'; import '../base/io.dart';
import '../base/logger.dart';
import '../convert.dart'; import '../convert.dart';
import '../globals_null_migrated.dart' as globals;
import 'test_compiler.dart'; import 'test_compiler.dart';
import 'test_config.dart'; import 'test_config.dart';
/// Helper class to start golden file comparison in a separate process. /// Helper class to start golden file comparison in a separate process.
/// ///
/// The golden file comparator is configured using flutter_test_config.dart and that /// Golden file comparator is configured using flutter_test_config.dart and that
/// file can contain arbitrary Dart code that depends on dart:ui. Thus it has to /// file can contain arbitrary Dart code that depends on dart:ui. Thus it has to
/// be executed in a `flutter_tester` environment. This helper class generates a /// be executed in a `flutter_tester` environment. This helper class generates a
/// Dart file configured with flutter_test_config.dart to perform the comparison /// Dart file configured with flutter_test_config.dart to perform the comparison
/// of golden files. /// of golden files.
class TestGoldenComparator { class TestGoldenComparator {
/// Creates a [TestGoldenComparator] instance. /// Creates a [TestGoldenComparator] instance.
TestGoldenComparator(this.shellPath, this.compilerFactory, { TestGoldenComparator(this.shellPath, this.compilerFactory)
@required Logger logger, : tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_web_platform.');
@required FileSystem fileSystem,
@required ProcessManager processManager,
}) : tempDir = fileSystem.systemTempDirectory.createTempSync('flutter_web_platform.'),
_logger = logger,
_fileSystem = fileSystem,
_processManager = processManager;
final String shellPath; final String shellPath;
final Directory tempDir; final Directory tempDir;
final TestCompiler Function() compilerFactory; final TestCompiler Function() compilerFactory;
final Logger _logger;
final FileSystem _fileSystem;
final ProcessManager _processManager;
TestCompiler _compiler; TestCompiler _compiler;
TestGoldenComparatorProcess _previousComparator; TestGoldenComparatorProcess _previousComparator;
...@@ -60,10 +49,10 @@ class TestGoldenComparator { ...@@ -60,10 +49,10 @@ class TestGoldenComparator {
return _previousComparator; return _previousComparator;
} }
final String bootstrap = TestGoldenComparatorProcess.generateBootstrap(_fileSystem.file(testUri), testUri, logger: _logger); final String bootstrap = TestGoldenComparatorProcess.generateBootstrap(testUri);
final Process process = await _startProcess(bootstrap); final Process process = await _startProcess(bootstrap);
unawaited(_previousComparator?.close()); unawaited(_previousComparator?.close());
_previousComparator = TestGoldenComparatorProcess(process, logger: _logger); _previousComparator = TestGoldenComparatorProcess(process);
_previousTestUri = testUri; _previousTestUri = testUri;
return _previousComparator; return _previousComparator;
...@@ -81,7 +70,7 @@ class TestGoldenComparator { ...@@ -81,7 +70,7 @@ class TestGoldenComparator {
shellPath, shellPath,
'--disable-observatory', '--disable-observatory',
'--non-interactive', '--non-interactive',
'--packages=${_fileSystem.path.join('.dart_tool', 'package_config.json')}', '--packages=${globals.fs.path.join('.dart_tool', 'package_config.json')}',
output, output,
]; ];
...@@ -89,7 +78,7 @@ class TestGoldenComparator { ...@@ -89,7 +78,7 @@ class TestGoldenComparator {
// Chrome is the only supported browser currently. // Chrome is the only supported browser currently.
'FLUTTER_TEST_BROWSER': 'chrome', 'FLUTTER_TEST_BROWSER': 'chrome',
}; };
return _processManager.start(command, environment: environment); return globals.processManager.start(command, environment: environment);
} }
Future<String> compareGoldens(Uri testUri, Uint8List bytes, Uri goldenKey, bool updateGoldens) async { Future<String> compareGoldens(Uri testUri, Uint8List bytes, Uri goldenKey, bool updateGoldens) async {
...@@ -111,7 +100,7 @@ class TestGoldenComparator { ...@@ -111,7 +100,7 @@ class TestGoldenComparator {
/// handles communication with the child process. /// handles communication with the child process.
class TestGoldenComparatorProcess { class TestGoldenComparatorProcess {
/// Creates a [TestGoldenComparatorProcess] backed by [process]. /// Creates a [TestGoldenComparatorProcess] backed by [process].
TestGoldenComparatorProcess(this.process, {@required Logger logger}) : _logger = logger { TestGoldenComparatorProcess(this.process) {
// Pipe stdout and stderr to printTrace and printError. // Pipe stdout and stderr to printTrace and printError.
// Also parse stdout as a stream of JSON objects. // Also parse stdout as a stream of JSON objects.
streamIterator = StreamIterator<Map<String, dynamic>>( streamIterator = StreamIterator<Map<String, dynamic>>(
...@@ -119,7 +108,7 @@ class TestGoldenComparatorProcess { ...@@ -119,7 +108,7 @@ class TestGoldenComparatorProcess {
.transform<String>(utf8.decoder) .transform<String>(utf8.decoder)
.transform<String>(const LineSplitter()) .transform<String>(const LineSplitter())
.where((String line) { .where((String line) {
logger.printTrace('<<< $line'); globals.printTrace('<<< $line');
return line.isNotEmpty && line[0] == '{'; return line.isNotEmpty && line[0] == '{';
}) })
.map<dynamic>(jsonDecode) .map<dynamic>(jsonDecode)
...@@ -129,17 +118,16 @@ class TestGoldenComparatorProcess { ...@@ -129,17 +118,16 @@ class TestGoldenComparatorProcess {
.transform<String>(utf8.decoder) .transform<String>(utf8.decoder)
.transform<String>(const LineSplitter()) .transform<String>(const LineSplitter())
.forEach((String line) { .forEach((String line) {
logger.printError('<<< $line'); globals.printError('<<< $line');
}); });
} }
final Logger _logger;
final Process process; final Process process;
StreamIterator<Map<String, dynamic>> streamIterator; StreamIterator<Map<String, dynamic>> streamIterator;
Future<void> close() async { Future<void> close() async {
await process.stdin.close();
process.kill(); process.kill();
await process.exitCode;
} }
void sendCommand(File imageFile, Uri goldenKey, bool updateGoldens) { void sendCommand(File imageFile, Uri goldenKey, bool updateGoldens) {
...@@ -148,7 +136,7 @@ class TestGoldenComparatorProcess { ...@@ -148,7 +136,7 @@ class TestGoldenComparatorProcess {
'key': goldenKey.toString(), 'key': goldenKey.toString(),
'update': updateGoldens, 'update': updateGoldens,
}); });
_logger.printTrace('Preparing to send command: $command'); globals.printTrace('Preparing to send command: $command');
process.stdin.writeln(command); process.stdin.writeln(command);
} }
...@@ -158,8 +146,8 @@ class TestGoldenComparatorProcess { ...@@ -158,8 +146,8 @@ class TestGoldenComparatorProcess {
return streamIterator.current; return streamIterator.current;
} }
static String generateBootstrap(File testFile, Uri testUri, {@required Logger logger}) { static String generateBootstrap(Uri testUri) {
final File testConfigFile = findTestConfigFile(testFile, logger); final File testConfigFile = findTestConfigFile(globals.fs.file(testUri), globals.logger);
// Generate comparator process for the file. // Generate comparator process for the file.
return ''' return '''
import 'dart:convert'; // flutter_ignore: dart_convert_import import 'dart:convert'; // flutter_ignore: dart_convert_import
......
...@@ -12,7 +12,6 @@ import 'package:http_multi_server/http_multi_server.dart'; ...@@ -12,7 +12,6 @@ import 'package:http_multi_server/http_multi_server.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:package_config/package_config.dart'; import 'package:package_config/package_config.dart';
import 'package:pool/pool.dart'; import 'package:pool/pool.dart';
import 'package:process/process.dart';
import 'package:shelf/shelf.dart' as shelf; import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as shelf_io; import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_static/shelf_static.dart'; import 'package:shelf_static/shelf_static.dart';
...@@ -52,7 +51,6 @@ class FlutterWebPlatform extends PlatformPlugin { ...@@ -52,7 +51,6 @@ class FlutterWebPlatform extends PlatformPlugin {
@required ChromiumLauncher chromiumLauncher, @required ChromiumLauncher chromiumLauncher,
@required Logger logger, @required Logger logger,
@required Artifacts artifacts, @required Artifacts artifacts,
@required ProcessManager processManager,
}) : _fileSystem = fileSystem, }) : _fileSystem = fileSystem,
_flutterToolPackageConfig = flutterToolPackageConfig, _flutterToolPackageConfig = flutterToolPackageConfig,
_chromiumLauncher = chromiumLauncher, _chromiumLauncher = chromiumLauncher,
...@@ -77,9 +75,6 @@ class FlutterWebPlatform extends PlatformPlugin { ...@@ -77,9 +75,6 @@ class FlutterWebPlatform extends PlatformPlugin {
_testGoldenComparator = TestGoldenComparator( _testGoldenComparator = TestGoldenComparator(
shellPath, shellPath,
() => TestCompiler(buildInfo, flutterProject), () => TestCompiler(buildInfo, flutterProject),
fileSystem: _fileSystem,
logger: _logger,
processManager: processManager,
); );
} }
...@@ -116,7 +111,6 @@ class FlutterWebPlatform extends PlatformPlugin { ...@@ -116,7 +111,6 @@ class FlutterWebPlatform extends PlatformPlugin {
@required Logger logger, @required Logger logger,
@required ChromiumLauncher chromiumLauncher, @required ChromiumLauncher chromiumLauncher,
@required Artifacts artifacts, @required Artifacts artifacts,
@required ProcessManager processManager,
}) async { }) async {
final shelf_io.IOServer server = shelf_io.IOServer(await HttpMultiServer.loopback(0)); final shelf_io.IOServer server = shelf_io.IOServer(await HttpMultiServer.loopback(0));
final PackageConfig packageConfig = await loadPackageConfigWithLogging( final PackageConfig packageConfig = await loadPackageConfigWithLogging(
...@@ -144,7 +138,6 @@ class FlutterWebPlatform extends PlatformPlugin { ...@@ -144,7 +138,6 @@ class FlutterWebPlatform extends PlatformPlugin {
artifacts: artifacts, artifacts: artifacts,
logger: logger, logger: logger,
nullAssertions: nullAssertions, nullAssertions: nullAssertions,
processManager: processManager,
); );
} }
......
...@@ -168,7 +168,6 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner { ...@@ -168,7 +168,6 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
logger: globals.logger, logger: globals.logger,
fileSystem: globals.fs, fileSystem: globals.fs,
artifacts: globals.artifacts, artifacts: globals.artifacts,
processManager: globals.processManager,
chromiumLauncher: ChromiumLauncher( chromiumLauncher: ChromiumLauncher(
fileSystem: globals.fs, fileSystem: globals.fs,
platform: globals.platform, platform: globals.platform,
......
...@@ -17,8 +17,8 @@ import 'package:flutter_tools/src/device.dart'; ...@@ -17,8 +17,8 @@ import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/test/flutter_tester_device.dart'; import 'package:flutter_tools/src/test/flutter_tester_device.dart';
import 'package:flutter_tools/src/test/font_config_manager.dart'; import 'package:flutter_tools/src/test/font_config_manager.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:mockito/mockito.dart';
import 'package:stream_channel/stream_channel.dart'; import 'package:stream_channel/stream_channel.dart';
import 'package:test/fake.dart';
import '../src/common.dart'; import '../src/common.dart';
import '../src/context.dart'; import '../src/context.dart';
...@@ -80,7 +80,7 @@ void main() { ...@@ -80,7 +80,7 @@ void main() {
], environment: <String, String>{ ], environment: <String, String>{
'FLUTTER_TEST': expectedFlutterTestValue, 'FLUTTER_TEST': expectedFlutterTestValue,
'FONTCONFIG_FILE': device.fontConfigManager.fontConfigFile.path, 'FONTCONFIG_FILE': device.fontConfigManager.fontConfigFile.path,
'SERVER_PORT': '0', 'SERVER_PORT': 'null',
'APP_NAME': '', 'APP_NAME': '',
}); });
} }
...@@ -244,28 +244,17 @@ class TestFlutterTesterDevice extends FlutterTesterTestDevice { ...@@ -244,28 +244,17 @@ class TestFlutterTesterDevice extends FlutterTesterTestDevice {
@override @override
Future<DartDevelopmentService> startDds(Uri uri) async { Future<DartDevelopmentService> startDds(Uri uri) async {
_ddsServiceUriCompleter.complete(uri); _ddsServiceUriCompleter.complete(uri);
return FakeDartDevelopmentService(Uri.parse('http://localhost:${debuggingOptions.hostVmServicePort}'), Uri.parse('http://localhost:8080')); final MockDartDevelopmentService mock = MockDartDevelopmentService();
when(mock.uri).thenReturn(Uri.parse('http://localhost:${debuggingOptions.hostVmServicePort}'));
return mock;
} }
@override @override
Future<HttpServer> bind(InternetAddress host, int port) async => FakeHttpServer(); Future<HttpServer> bind(InternetAddress host, int port) async => MockHttpServer();
@override @override
Future<StreamChannel<String>> get remoteChannel async => StreamChannelController<String>().foreign; Future<StreamChannel<String>> get remoteChannel async => StreamChannelController<String>().foreign;
} }
class FakeDartDevelopmentService extends Fake implements DartDevelopmentService { class MockDartDevelopmentService extends Mock implements DartDevelopmentService {}
FakeDartDevelopmentService(this.uri, this.original); class MockHttpServer extends Mock implements HttpServer {}
final Uri original;
@override
final Uri uri;
@override
Uri get remoteVmServiceUri => original;
}
class FakeHttpServer extends Fake implements HttpServer {
@override
int get port => 0;
}
...@@ -7,14 +7,16 @@ ...@@ -7,14 +7,16 @@
import 'dart:convert'; import 'dart:convert';
import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/globals_null_migrated.dart' as globals; import 'package:flutter_tools/src/globals_null_migrated.dart' as globals;
import 'package:flutter_tools/src/test/flutter_web_goldens.dart'; import 'package:flutter_tools/src/test/flutter_web_goldens.dart';
import '../../src/common.dart'; import '../../src/common.dart';
import '../../src/fakes.dart'; import '../../src/fakes.dart';
import '../../src/testbed.dart';
void main() { void main() {
final Testbed testbed = Testbed();
group('Test that TestGoldenComparatorProcess', () { group('Test that TestGoldenComparatorProcess', () {
File imageFile; File imageFile;
Uri goldenKey; Uri goldenKey;
...@@ -33,7 +35,7 @@ void main() { ...@@ -33,7 +35,7 @@ void main() {
); );
}); });
testWithoutContext('can pass data', () async { test('can pass data', () => testbed.run(() async {
final Map<String, dynamic> expectedResponse = <String, dynamic>{ final Map<String, dynamic> expectedResponse = <String, dynamic>{
'success': true, 'success': true,
'message': 'some message', 'message': 'some message',
...@@ -42,7 +44,7 @@ void main() { ...@@ -42,7 +44,7 @@ void main() {
final FakeProcess mockProcess = createFakeProcess(jsonEncode(expectedResponse) + '\n'); final FakeProcess mockProcess = createFakeProcess(jsonEncode(expectedResponse) + '\n');
final MemoryIOSink ioSink = mockProcess.stdin as MemoryIOSink; final MemoryIOSink ioSink = mockProcess.stdin as MemoryIOSink;
final TestGoldenComparatorProcess process = TestGoldenComparatorProcess(mockProcess, logger: BufferLogger.test()); final TestGoldenComparatorProcess process = TestGoldenComparatorProcess(mockProcess);
process.sendCommand(imageFile, goldenKey, false); process.sendCommand(imageFile, goldenKey, false);
final Map<String, dynamic> response = await process.getResponse(); final Map<String, dynamic> response = await process.getResponse();
...@@ -50,9 +52,9 @@ void main() { ...@@ -50,9 +52,9 @@ void main() {
expect(response, expectedResponse); expect(response, expectedResponse);
expect(stringToStdin, '{"imageFile":"test_image_file","key":"file://golden_key/","update":false}\n'); expect(stringToStdin, '{"imageFile":"test_image_file","key":"file://golden_key/","update":false}\n');
}); }));
testWithoutContext('can handle multiple requests', () async { test('can handle multiple requests', () => testbed.run(() async {
final Map<String, dynamic> expectedResponse1 = <String, dynamic>{ final Map<String, dynamic> expectedResponse1 = <String, dynamic>{
'success': true, 'success': true,
'message': 'some message', 'message': 'some message',
...@@ -65,7 +67,7 @@ void main() { ...@@ -65,7 +67,7 @@ void main() {
final FakeProcess mockProcess = createFakeProcess(jsonEncode(expectedResponse1) + '\n' + jsonEncode(expectedResponse2) + '\n'); final FakeProcess mockProcess = createFakeProcess(jsonEncode(expectedResponse1) + '\n' + jsonEncode(expectedResponse2) + '\n');
final MemoryIOSink ioSink = mockProcess.stdin as MemoryIOSink; final MemoryIOSink ioSink = mockProcess.stdin as MemoryIOSink;
final TestGoldenComparatorProcess process = TestGoldenComparatorProcess(mockProcess, logger: BufferLogger.test()); final TestGoldenComparatorProcess process = TestGoldenComparatorProcess(mockProcess);
process.sendCommand(imageFile, goldenKey, false); process.sendCommand(imageFile, goldenKey, false);
final Map<String, dynamic> response1 = await process.getResponse(); final Map<String, dynamic> response1 = await process.getResponse();
...@@ -78,9 +80,9 @@ void main() { ...@@ -78,9 +80,9 @@ void main() {
expect(response1, expectedResponse1); expect(response1, expectedResponse1);
expect(response2, expectedResponse2); expect(response2, expectedResponse2);
expect(stringToStdin, '{"imageFile":"test_image_file","key":"file://golden_key/","update":false}\n{"imageFile":"second_test_image_file","key":"file://second_golden_key/","update":true}\n'); expect(stringToStdin, '{"imageFile":"test_image_file","key":"file://golden_key/","update":false}\n{"imageFile":"second_test_image_file","key":"file://second_golden_key/","update":true}\n');
}); }));
testWithoutContext('ignores anything that does not look like JSON', () async { test('ignores anything that does not look like JSON', () => testbed.run(() async {
final Map<String, dynamic> expectedResponse = <String, dynamic>{ final Map<String, dynamic> expectedResponse = <String, dynamic>{
'success': true, 'success': true,
'message': 'some message', 'message': 'some message',
...@@ -95,7 +97,7 @@ Other JSON data after the initial data ...@@ -95,7 +97,7 @@ Other JSON data after the initial data
'''); ''');
final MemoryIOSink ioSink = mockProcess.stdin as MemoryIOSink; final MemoryIOSink ioSink = mockProcess.stdin as MemoryIOSink;
final TestGoldenComparatorProcess process = TestGoldenComparatorProcess(mockProcess,logger: BufferLogger.test()); final TestGoldenComparatorProcess process = TestGoldenComparatorProcess(mockProcess);
process.sendCommand(imageFile, goldenKey, false); process.sendCommand(imageFile, goldenKey, false);
final Map<String, dynamic> response = await process.getResponse(); final Map<String, dynamic> response = await process.getResponse();
...@@ -103,7 +105,7 @@ Other JSON data after the initial data ...@@ -103,7 +105,7 @@ Other JSON data after the initial data
expect(response, expectedResponse); expect(response, expectedResponse);
expect(stringToStdin, '{"imageFile":"test_image_file","key":"file://golden_key/","update":false}\n'); expect(stringToStdin, '{"imageFile":"test_image_file","key":"file://golden_key/","update":false}\n');
}); }));
}); });
} }
......
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