flutter test: add --machine flag (#10520)
Currently this just prints the observatory URL as a JSON event. Refactored the code to make this fit in.
This commit is contained in:
@@ -11,10 +11,18 @@ import '../base/io.dart';
|
||||
import '../dart/package_map.dart';
|
||||
import '../globals.dart';
|
||||
|
||||
import 'watcher.dart';
|
||||
|
||||
/// A class that's used to collect coverage data during tests.
|
||||
class CoverageCollector {
|
||||
class CoverageCollector extends TestWatcher {
|
||||
Map<String, dynamic> _globalHitmap;
|
||||
|
||||
@override
|
||||
Future<Null> onFinishedTests(ProcessEvent event) async {
|
||||
printTrace('test ${event.childIndex}: collecting coverage');
|
||||
await collectCoverage(event.process, event.observatoryUri);
|
||||
}
|
||||
|
||||
void _addHitmap(Map<String, dynamic> hitmap) {
|
||||
if (_globalHitmap == null)
|
||||
_globalHitmap = hitmap;
|
||||
|
||||
41
packages/flutter_tools/lib/src/test/event_printer.dart
Normal file
41
packages/flutter_tools/lib/src/test/event_printer.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert' show JSON;
|
||||
|
||||
import '../base/io.dart' show stdout;
|
||||
import 'watcher.dart';
|
||||
|
||||
/// Prints JSON events when running a test in --machine mode.
|
||||
class EventPrinter extends TestWatcher {
|
||||
EventPrinter({StringSink out}) : this._out = out == null ? stdout: out;
|
||||
|
||||
final StringSink _out;
|
||||
|
||||
@override
|
||||
void onStartedProcess(ProcessEvent event) {
|
||||
_sendEvent("test.startedProcess",
|
||||
<String, dynamic>{"observatoryUri": event.observatoryUri.toString()});
|
||||
}
|
||||
|
||||
void _sendEvent(String name, [dynamic params]) {
|
||||
final Map<String, dynamic> map = <String, dynamic>{ 'event': name};
|
||||
if (params != null) {
|
||||
map['params'] = params;
|
||||
}
|
||||
_send(map);
|
||||
}
|
||||
|
||||
void _send(Map<String, dynamic> command) {
|
||||
final String encoded = JSON.encode(command, toEncodable: _jsonEncodeObject);
|
||||
_out.writeln('\n[$encoded]');
|
||||
}
|
||||
|
||||
dynamic _jsonEncodeObject(dynamic object) {
|
||||
if (object is Uri) {
|
||||
return object.toString();
|
||||
}
|
||||
return object;
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import '../base/process_manager.dart';
|
||||
import '../dart/package_map.dart';
|
||||
import '../globals.dart';
|
||||
import 'coverage_collector.dart';
|
||||
import 'watcher.dart';
|
||||
|
||||
/// The timeout we give the test process to connect to the test harness
|
||||
/// once the process has entered its main method.
|
||||
@@ -53,17 +54,22 @@ final Map<InternetAddressType, InternetAddress> _kHosts = <InternetAddressType,
|
||||
void installHook({
|
||||
@required String shellPath,
|
||||
CoverageCollector collector,
|
||||
bool debuggerMode: false,
|
||||
TestWatcher watcher,
|
||||
bool enableObservatory: false,
|
||||
bool startPaused: false,
|
||||
int observatoryPort,
|
||||
int diagnosticPort,
|
||||
InternetAddressType serverType: InternetAddressType.IP_V4,
|
||||
}) {
|
||||
if (startPaused || observatoryPort != null || diagnosticPort != null)
|
||||
assert(enableObservatory);
|
||||
hack.registerPlatformPlugin(
|
||||
<TestPlatform>[TestPlatform.vm],
|
||||
() => new _FlutterPlatform(
|
||||
shellPath: shellPath,
|
||||
collector: collector,
|
||||
debuggerMode: debuggerMode,
|
||||
watcher: watcher,
|
||||
enableObservatory: enableObservatory,
|
||||
startPaused: startPaused,
|
||||
explicitObservatoryPort: observatoryPort,
|
||||
explicitDiagnosticPort: diagnosticPort,
|
||||
host: _kHosts[serverType],
|
||||
@@ -78,8 +84,9 @@ typedef Future<Null> _Finalizer();
|
||||
class _FlutterPlatform extends PlatformPlugin {
|
||||
_FlutterPlatform({
|
||||
@required this.shellPath,
|
||||
this.collector,
|
||||
this.debuggerMode,
|
||||
this.watcher,
|
||||
this.enableObservatory,
|
||||
this.startPaused,
|
||||
this.explicitObservatoryPort,
|
||||
this.explicitDiagnosticPort,
|
||||
this.host,
|
||||
@@ -88,8 +95,9 @@ class _FlutterPlatform extends PlatformPlugin {
|
||||
}
|
||||
|
||||
final String shellPath;
|
||||
final CoverageCollector collector;
|
||||
final bool debuggerMode;
|
||||
final TestWatcher watcher;
|
||||
final bool enableObservatory;
|
||||
final bool startPaused;
|
||||
final int explicitObservatoryPort;
|
||||
final int explicitDiagnosticPort;
|
||||
final InternetAddress host;
|
||||
@@ -105,7 +113,7 @@ class _FlutterPlatform extends PlatformPlugin {
|
||||
|
||||
@override
|
||||
StreamChannel<dynamic> loadChannel(String testPath, TestPlatform platform) {
|
||||
if (explicitObservatoryPort != null || explicitDiagnosticPort != null || debuggerMode) {
|
||||
if (enableObservatory || explicitDiagnosticPort != null) {
|
||||
if (_testCount > 0)
|
||||
throwToolExit('installHook() was called with an observatory port, a diagnostic port, both, or debugger mode enabled, but then more than one test suite was run.');
|
||||
}
|
||||
@@ -190,8 +198,8 @@ class _FlutterPlatform extends PlatformPlugin {
|
||||
shellPath,
|
||||
listenerFile.path,
|
||||
packages: PackageMap.globalPackagesPath,
|
||||
enableObservatory: collector != null || debuggerMode,
|
||||
startPaused: debuggerMode,
|
||||
enableObservatory: enableObservatory,
|
||||
startPaused: startPaused,
|
||||
observatoryPort: explicitObservatoryPort,
|
||||
diagnosticPort: explicitDiagnosticPort,
|
||||
);
|
||||
@@ -216,19 +224,23 @@ class _FlutterPlatform extends PlatformPlugin {
|
||||
// Pipe stdout and stderr from the subprocess to our printStatus console.
|
||||
// We also keep track of what observatory port the engine used, if any.
|
||||
Uri processObservatoryUri;
|
||||
|
||||
_pipeStandardStreamsToConsole(
|
||||
process,
|
||||
reportObservatoryUri: (Uri detectedUri) {
|
||||
assert(processObservatoryUri == null);
|
||||
assert(explicitObservatoryPort == null ||
|
||||
explicitObservatoryPort == detectedUri.port);
|
||||
if (debuggerMode) {
|
||||
if (startPaused) {
|
||||
printStatus('The test process has been started.');
|
||||
printStatus('You can now connect to it using observatory. To connect, load the following Web site in your browser:');
|
||||
printStatus(' $detectedUri');
|
||||
printStatus('You should first set appropriate breakpoints, then resume the test in the debugger.');
|
||||
} else {
|
||||
printTrace('test $ourTestCount: using observatory uri $detectedUri from pid ${process.pid} to collect coverage');
|
||||
printTrace('test $ourTestCount: using observatory uri $detectedUri from pid ${process.pid}');
|
||||
}
|
||||
if (watcher != null) {
|
||||
watcher.onStartedProcess(new ProcessEvent(ourTestCount, process, detectedUri));
|
||||
}
|
||||
processObservatoryUri = detectedUri;
|
||||
},
|
||||
@@ -341,9 +353,9 @@ class _FlutterPlatform extends PlatformPlugin {
|
||||
break;
|
||||
}
|
||||
|
||||
if (subprocessActive && collector != null) {
|
||||
printTrace('test $ourTestCount: collecting coverage');
|
||||
await collector.collectCoverage(process, processObservatoryUri);
|
||||
if (subprocessActive && watcher != null) {
|
||||
await watcher.onFinishedTests(
|
||||
new ProcessEvent(ourTestCount, process, processObservatoryUri));
|
||||
}
|
||||
} catch (error, stack) {
|
||||
printTrace('test $ourTestCount: error caught during test; ${controllerSinkClosed ? "reporting to console" : "sending to test framework"}');
|
||||
|
||||
81
packages/flutter_tools/lib/src/test/runner.dart
Normal file
81
packages/flutter_tools/lib/src/test/runner.dart
Normal file
@@ -0,0 +1,81 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
// ignore: implementation_imports
|
||||
import 'package:test/src/executable.dart' as test;
|
||||
|
||||
import '../artifacts.dart';
|
||||
import '../base/common.dart';
|
||||
import '../base/file_system.dart';
|
||||
import '../base/io.dart';
|
||||
import '../base/terminal.dart';
|
||||
import '../dart/package_map.dart';
|
||||
import '../globals.dart';
|
||||
import '../test/flutter_platform.dart' as loader;
|
||||
import 'watcher.dart';
|
||||
|
||||
/// Runs tests using package:test and the Flutter engine.
|
||||
Future<int> runTests(
|
||||
List<String> testFiles, {
|
||||
Directory workDir,
|
||||
bool enableObservatory: false,
|
||||
bool startPaused: false,
|
||||
bool ipv6: false,
|
||||
TestWatcher watcher,
|
||||
}) async {
|
||||
// Compute the command-line arguments for package:test.
|
||||
final List<String> testArgs = <String>[];
|
||||
if (!terminal.supportsColor)
|
||||
testArgs.addAll(<String>['--no-color', '-rexpanded']);
|
||||
|
||||
if (enableObservatory) {
|
||||
testArgs.add('--concurrency=1');
|
||||
}
|
||||
|
||||
testArgs.add('--');
|
||||
testArgs.addAll(testFiles);
|
||||
|
||||
// Configure package:test to use the Flutter engine for child processes.
|
||||
final String shellPath = artifacts.getArtifactPath(Artifact.flutterTester);
|
||||
if (!fs.isFileSync(shellPath))
|
||||
throwToolExit('Cannot find Flutter shell at $shellPath');
|
||||
|
||||
final InternetAddressType serverType =
|
||||
ipv6 ? InternetAddressType.IP_V6 : InternetAddressType.IP_V4;
|
||||
|
||||
loader.installHook(
|
||||
shellPath: shellPath,
|
||||
watcher: watcher,
|
||||
enableObservatory: enableObservatory,
|
||||
startPaused: startPaused,
|
||||
serverType: serverType,
|
||||
);
|
||||
|
||||
// Set the package path used for child processes.
|
||||
// TODO(skybrian): why is this global? Move to installHook?
|
||||
PackageMap.globalPackagesPath =
|
||||
fs.path.normalize(fs.path.absolute(PackageMap.globalPackagesPath));
|
||||
|
||||
// Call package:test's main method in the appropriate directory.
|
||||
final Directory saved = fs.currentDirectory;
|
||||
try {
|
||||
if (workDir != null) {
|
||||
printTrace('switching to directory $workDir to run tests');
|
||||
fs.currentDirectory = workDir;
|
||||
}
|
||||
|
||||
printTrace('running test package with arguments: $testArgs');
|
||||
await test.main(testArgs);
|
||||
|
||||
// test.main() sets dart:io's exitCode global.
|
||||
// TODO(skybrian): restore previous value?
|
||||
printTrace('test package returned with exit code $exitCode');
|
||||
|
||||
return exitCode;
|
||||
} finally {
|
||||
fs.currentDirectory = saved;
|
||||
}
|
||||
}
|
||||
39
packages/flutter_tools/lib/src/test/watcher.dart
Normal file
39
packages/flutter_tools/lib/src/test/watcher.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import '../base/io.dart' show Process;
|
||||
|
||||
/// Callbacks for reporting progress while running tests.
|
||||
class TestWatcher {
|
||||
|
||||
/// Called after a child process starts.
|
||||
///
|
||||
/// If startPaused was true, the caller needs to resume in Observatory to
|
||||
/// start running the tests.
|
||||
void onStartedProcess(ProcessEvent event) {}
|
||||
|
||||
/// Called after the tests finish but before the process exits.
|
||||
///
|
||||
/// The child process won't exit until this method completes.
|
||||
/// Not called if the process died.
|
||||
Future<Null> onFinishedTests(ProcessEvent event) async {}
|
||||
}
|
||||
|
||||
/// Describes a child process started during testing.
|
||||
class ProcessEvent {
|
||||
ProcessEvent(this.childIndex, this.process, this.observatoryUri);
|
||||
|
||||
/// The index assigned when the child process was launched.
|
||||
///
|
||||
/// Indexes are assigned consecutively starting from zero.
|
||||
/// When debugging, there should only be one child process so this will
|
||||
/// always be zero.
|
||||
final int childIndex;
|
||||
|
||||
final Process process;
|
||||
|
||||
/// The observatory Uri or null if not debugging.
|
||||
final Uri observatoryUri;
|
||||
}
|
||||
Reference in New Issue
Block a user