Unverified Commit e3625706 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove mocks, globals from golden comparator and test runner tests | Reland

This reverts commit 027d4eca.
parent 8041a2e5
...@@ -4,33 +4,44 @@ ...@@ -4,33 +4,44 @@
// @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.
/// ///
/// Golden file comparator is configured using flutter_test_config.dart and that /// The 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, {
: tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_web_platform.'); @required Logger logger,
@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;
...@@ -49,10 +60,10 @@ class TestGoldenComparator { ...@@ -49,10 +60,10 @@ class TestGoldenComparator {
return _previousComparator; return _previousComparator;
} }
final String bootstrap = TestGoldenComparatorProcess.generateBootstrap(testUri); final String bootstrap = TestGoldenComparatorProcess.generateBootstrap(_fileSystem.file(testUri), testUri, logger: _logger);
final Process process = await _startProcess(bootstrap); final Process process = await _startProcess(bootstrap);
unawaited(_previousComparator?.close()); unawaited(_previousComparator?.close());
_previousComparator = TestGoldenComparatorProcess(process); _previousComparator = TestGoldenComparatorProcess(process, logger: _logger);
_previousTestUri = testUri; _previousTestUri = testUri;
return _previousComparator; return _previousComparator;
...@@ -70,7 +81,7 @@ class TestGoldenComparator { ...@@ -70,7 +81,7 @@ class TestGoldenComparator {
shellPath, shellPath,
'--disable-observatory', '--disable-observatory',
'--non-interactive', '--non-interactive',
'--packages=${globals.fs.path.join('.dart_tool', 'package_config.json')}', '--packages=${_fileSystem.path.join('.dart_tool', 'package_config.json')}',
output, output,
]; ];
...@@ -78,7 +89,7 @@ class TestGoldenComparator { ...@@ -78,7 +89,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 globals.processManager.start(command, environment: environment); return _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 {
...@@ -100,7 +111,7 @@ class TestGoldenComparator { ...@@ -100,7 +111,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) { TestGoldenComparatorProcess(this.process, {@required Logger logger}) : _logger = logger {
// 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>>(
...@@ -108,7 +119,7 @@ class TestGoldenComparatorProcess { ...@@ -108,7 +119,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) {
globals.printTrace('<<< $line'); logger.printTrace('<<< $line');
return line.isNotEmpty && line[0] == '{'; return line.isNotEmpty && line[0] == '{';
}) })
.map<dynamic>(jsonDecode) .map<dynamic>(jsonDecode)
...@@ -118,16 +129,17 @@ class TestGoldenComparatorProcess { ...@@ -118,16 +129,17 @@ class TestGoldenComparatorProcess {
.transform<String>(utf8.decoder) .transform<String>(utf8.decoder)
.transform<String>(const LineSplitter()) .transform<String>(const LineSplitter())
.forEach((String line) { .forEach((String line) {
globals.printError('<<< $line'); logger.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) {
...@@ -136,7 +148,7 @@ class TestGoldenComparatorProcess { ...@@ -136,7 +148,7 @@ class TestGoldenComparatorProcess {
'key': goldenKey.toString(), 'key': goldenKey.toString(),
'update': updateGoldens, 'update': updateGoldens,
}); });
globals.printTrace('Preparing to send command: $command'); _logger.printTrace('Preparing to send command: $command');
process.stdin.writeln(command); process.stdin.writeln(command);
} }
...@@ -146,8 +158,8 @@ class TestGoldenComparatorProcess { ...@@ -146,8 +158,8 @@ class TestGoldenComparatorProcess {
return streamIterator.current; return streamIterator.current;
} }
static String generateBootstrap(Uri testUri) { static String generateBootstrap(File testFile, Uri testUri, {@required Logger logger}) {
final File testConfigFile = findTestConfigFile(globals.fs.file(testUri), globals.logger); final File testConfigFile = findTestConfigFile(testFile, 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,6 +12,7 @@ import 'package:http_multi_server/http_multi_server.dart'; ...@@ -12,6 +12,7 @@ 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';
...@@ -51,6 +52,7 @@ class FlutterWebPlatform extends PlatformPlugin { ...@@ -51,6 +52,7 @@ 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,
...@@ -75,6 +77,9 @@ class FlutterWebPlatform extends PlatformPlugin { ...@@ -75,6 +77,9 @@ class FlutterWebPlatform extends PlatformPlugin {
_testGoldenComparator = TestGoldenComparator( _testGoldenComparator = TestGoldenComparator(
shellPath, shellPath,
() => TestCompiler(buildInfo, flutterProject), () => TestCompiler(buildInfo, flutterProject),
fileSystem: _fileSystem,
logger: _logger,
processManager: processManager,
); );
} }
...@@ -111,6 +116,7 @@ class FlutterWebPlatform extends PlatformPlugin { ...@@ -111,6 +116,7 @@ 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(
...@@ -138,6 +144,7 @@ class FlutterWebPlatform extends PlatformPlugin { ...@@ -138,6 +144,7 @@ class FlutterWebPlatform extends PlatformPlugin {
artifacts: artifacts, artifacts: artifacts,
logger: logger, logger: logger,
nullAssertions: nullAssertions, nullAssertions: nullAssertions,
processManager: processManager,
); );
} }
......
...@@ -168,6 +168,7 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner { ...@@ -168,6 +168,7 @@ 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': 'null', 'SERVER_PORT': '0',
'APP_NAME': '', 'APP_NAME': '',
}); });
} }
...@@ -244,17 +244,28 @@ class TestFlutterTesterDevice extends FlutterTesterTestDevice { ...@@ -244,17 +244,28 @@ class TestFlutterTesterDevice extends FlutterTesterTestDevice {
@override @override
Future<DartDevelopmentService> startDds(Uri uri) async { Future<DartDevelopmentService> startDds(Uri uri) async {
_ddsServiceUriCompleter.complete(uri); _ddsServiceUriCompleter.complete(uri);
final MockDartDevelopmentService mock = MockDartDevelopmentService(); return FakeDartDevelopmentService(Uri.parse('http://localhost:${debuggingOptions.hostVmServicePort}'), Uri.parse('http://localhost:8080'));
when(mock.uri).thenReturn(Uri.parse('http://localhost:${debuggingOptions.hostVmServicePort}'));
return mock;
} }
@override @override
Future<HttpServer> bind(InternetAddress host, int port) async => MockHttpServer(); Future<HttpServer> bind(InternetAddress host, int port) async => FakeHttpServer();
@override @override
Future<StreamChannel<String>> get remoteChannel async => StreamChannelController<String>().foreign; Future<StreamChannel<String>> get remoteChannel async => StreamChannelController<String>().foreign;
} }
class MockDartDevelopmentService extends Mock implements DartDevelopmentService {} class FakeDartDevelopmentService extends Fake implements DartDevelopmentService {
class MockHttpServer extends Mock implements HttpServer {} FakeDartDevelopmentService(this.uri, this.original);
final Uri original;
@override
final Uri uri;
@override
Uri get remoteVmServiceUri => original;
}
class FakeHttpServer extends Fake implements HttpServer {
@override
int get port => 0;
}
...@@ -7,16 +7,14 @@ ...@@ -7,16 +7,14 @@
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;
...@@ -35,7 +33,7 @@ void main() { ...@@ -35,7 +33,7 @@ void main() {
); );
}); });
test('can pass data', () => testbed.run(() async { testWithoutContext('can pass data', () 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',
...@@ -44,7 +42,7 @@ void main() { ...@@ -44,7 +42,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); final TestGoldenComparatorProcess process = TestGoldenComparatorProcess(mockProcess, logger: BufferLogger.test());
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();
...@@ -52,9 +50,9 @@ void main() { ...@@ -52,9 +50,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');
})); });
test('can handle multiple requests', () => testbed.run(() async { testWithoutContext('can handle multiple requests', () 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',
...@@ -67,7 +65,7 @@ void main() { ...@@ -67,7 +65,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); final TestGoldenComparatorProcess process = TestGoldenComparatorProcess(mockProcess, logger: BufferLogger.test());
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();
...@@ -80,9 +78,9 @@ void main() { ...@@ -80,9 +78,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');
})); });
test('ignores anything that does not look like JSON', () => testbed.run(() async { testWithoutContext('ignores anything that does not look like JSON', () 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',
...@@ -97,7 +95,7 @@ Other JSON data after the initial data ...@@ -97,7 +95,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); final TestGoldenComparatorProcess process = TestGoldenComparatorProcess(mockProcess,logger: BufferLogger.test());
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();
...@@ -105,7 +103,7 @@ Other JSON data after the initial data ...@@ -105,7 +103,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