Attempt to enable tool coverage redux (#35074)

This commit is contained in:
Jonah Williams
2019-06-26 16:02:49 -07:00
committed by GitHub
parent 206d43de97
commit 33ad5bac34
13 changed files with 279 additions and 142 deletions

View File

@@ -3,165 +3,174 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'dart:isolate';
import 'package:args/args.dart';
import 'package:async/async.dart';
import 'package:coverage/coverage.dart';
import 'package:flutter_tools/src/context_runner.dart';
import 'package:path/path.dart' as p;
import 'package:pedantic/pedantic.dart';
import 'package:stream_channel/isolate_channel.dart';
import 'package:stream_channel/stream_channel.dart';
import 'package:test_core/src/runner/hack_register_platform.dart' as hack; // ignore: implementation_imports
import 'package:test_core/src/executable.dart' as test; // ignore: implementation_imports
import 'package:vm_service_client/vm_service_client.dart';
import 'package:test_api/src/backend/runtime.dart'; // ignore: implementation_imports
import 'package:test_api/src/backend/suite_platform.dart'; // ignore: implementation_imports
import 'package:test_core/src/runner/platform.dart'; // ignore: implementation_imports
import 'package:test_core/src/runner/runner_suite.dart'; // ignore: implementation_imports
import 'package:test_core/src/runner/suite.dart'; // ignore: implementation_imports
import 'package:test_core/src/runner/plugin/platform_helpers.dart'; // ignore: implementation_imports
import 'package:test_core/src/runner/environment.dart'; // ignore: implementation_imports
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/test/coverage_collector.dart';
import 'package:pool/pool.dart';
import 'package:path/path.dart' as path;
final ArgParser argParser = ArgParser()
..addOption('output-html',
defaultsTo: 'coverage/report.html',
help: 'The output path for the genhtml report.'
)
..addOption('output-lcov',
defaultsTo: 'coverage/lcov.info',
help: 'The output path for the lcov data.'
)
..addOption('test-directory',
defaultsTo: 'test/',
help: 'The path to the test directory.'
)
..addOption('packages',
defaultsTo: '.packages',
help: 'The path to the .packages file.'
)
..addOption('genhtml',
defaultsTo: 'genhtml',
help: 'The genhtml executable.');
/// Generates an html coverage report for the flutter_tool.
/// Generates an lcov report for the flutter tool unit tests.
///
/// Example invocation:
///
/// dart tool/tool_coverage.dart --packages=.packages --test-directory=test
/// dart tool/tool_coverage.dart.
Future<void> main(List<String> arguments) async {
final ArgResults argResults = argParser.parse(arguments);
await runInContext(() async {
final CoverageCollector coverageCollector = CoverageCollector(
flutterProject: FlutterProject.current(),
return runInContext(() async {
final VMPlatform vmPlatform = VMPlatform();
hack.registerPlatformPlugin(
<Runtime>[Runtime.vm],
() => vmPlatform,
);
/// A temp directory to create synthetic test files in.
final Directory tempDirectory = Directory.systemTemp.createTempSync('_flutter_coverage')
..createSync();
final String flutterRoot = File(Platform.script.toFilePath()).parent.parent.parent.parent.path;
await ToolCoverageRunner(tempDirectory, coverageCollector, flutterRoot, argResults).collectCoverage();
await test.main(<String>['-x', 'no_coverage', '--no-color', '-r', 'compact', '-j', '1', ...arguments]);
exit(exitCode);
});
}
class ToolCoverageRunner {
ToolCoverageRunner(
this.tempDirectory,
this.coverageCollector,
this.flutterRoot,
this.argResults,
/// A platform that loads tests in isolates spawned within this Dart process.
class VMPlatform extends PlatformPlugin {
final CoverageCollector coverageCollector = CoverageCollector(
flutterProject: FlutterProject.current(),
);
final Map<String, Future<void>> _pending = <String, Future<void>>{};
final String precompiledPath = p.join('.dart_tool', 'build', 'generated', 'flutter_tools');
final ArgResults argResults;
final Pool pool = Pool(Platform.numberOfProcessors);
final Directory tempDirectory;
final CoverageCollector coverageCollector;
final String flutterRoot;
@override
StreamChannel<void> loadChannel(String path, SuitePlatform platform) =>
throw UnimplementedError();
Future<void> collectCoverage() async {
final List<Future<void>> pending = <Future<void>>[];
final Directory testDirectory = Directory(argResults['test-directory']);
final List<FileSystemEntity> fileSystemEntities = testDirectory.listSync(recursive: true);
for (FileSystemEntity fileSystemEntity in fileSystemEntities) {
if (!fileSystemEntity.path.endsWith('_test.dart')) {
continue;
}
pending.add(_runTest(fileSystemEntity));
@override
Future<RunnerSuite> load(String path, SuitePlatform platform,
SuiteConfiguration suiteConfig, Object message) async {
final ReceivePort receivePort = ReceivePort();
Isolate isolate;
try {
isolate = await _spawnIsolate(path, receivePort.sendPort);
} catch (error) {
receivePort.close();
rethrow;
}
await Future.wait(pending);
final Completer<void> completer = Completer<void>();
// When this is completed we remove it from the map of pending so we can
// log the futures that get "stuck".
unawaited(completer.future.whenComplete(() {
_pending.remove(path);
}));
final ServiceProtocolInfo info = await Service.controlWebServer(enable: true);
final dynamic channel = IsolateChannel<Object>.connectReceive(receivePort)
.transformStream(StreamTransformer<Object, Object>.fromHandlers(handleDone: (EventSink<Object> sink) async {
try {
// this will throw if collection fails.
await coverageCollector.collectCoverageIsolate(info.serverUri);
} finally {
isolate.kill(priority: Isolate.immediate);
isolate = null;
sink.close();
completer.complete();
}
}, handleError: (dynamic error, StackTrace stackTrace, EventSink<Object> sink) {
isolate.kill(priority: Isolate.immediate);
isolate = null;
sink.close();
completer.complete();
}));
final String lcovData = await coverageCollector.finalizeCoverage();
final String outputLcovPath = argResults['output-lcov'];
final String outputHtmlPath = argResults['output-html'];
final String genHtmlExecutable = argResults['genhtml'];
VMEnvironment environment;
final RunnerSuiteController controller = deserializeSuite(
path,
platform,
suiteConfig,
environment,
channel,
message,
);
_pending[path] = completer.future;
return await controller.suite;
}
/// Spawns an isolate and passes it [message].
///
/// This isolate connects an [IsolateChannel] to [message] and sends the
/// serialized tests over that channel.
Future<Isolate> _spawnIsolate(String path, SendPort message) async {
String testPath = p.absolute(p.join(precompiledPath, path) + '.vm_test.dart');
testPath = testPath.substring(0, testPath.length - '.dart'.length) + '.vm.app.dill';
return await Isolate.spawnUri(p.toUri(testPath), <String>[], message,
packageConfig: p.toUri('.packages'),
checked: true,
);
}
@override
Future<void> close() async {
try {
await Future.wait(_pending.values).timeout(const Duration(seconds: 10));
} on TimeoutException {
// TODO(jonahwilliams): resolve whether there are any specific tests that
// get stuck or if it is a general infra issue with how we are collecting
// coverage.
// Log tests that are "Stuck" waiuting for coverage.
print('The folllowing tests timed out waiting for coverage:');
print(_pending.keys.join(', '));
}
final String packagePath = Directory.current.path;
final Resolver resolver = Resolver(packagesPath: '.packages');
final Formatter formatter = LcovFormatter(resolver, reportOn: <String>[
'lib',
], basePath: packagePath);
final String result = await coverageCollector.finalizeCoverage(
formatter: formatter,
);
final String prefix = Platform.environment['SUBSHARD'] ?? '';
final String outputLcovPath = p.join('coverage', '$prefix.lcov.info');
File(outputLcovPath)
..createSync(recursive: true)
..writeAsStringSync(lcovData);
await Process.run(genHtmlExecutable, <String>[outputLcovPath, '-o', outputHtmlPath], runInShell: true);
}
// Creates a synthetic test file to wrap the test main in a group invocation.
// This will set up several fields used by the test methods on the context. Normally
// this would be handled automatically by the test runner, but since we're executing
// the files directly with dart we need to handle it manually.
String _createTest(File testFile) {
final File fakeTest = File(path.join(tempDirectory.path, testFile.path))
..createSync(recursive: true)
..writeAsStringSync('''
import "package:test/test.dart";
import "${path.absolute(testFile.path)}" as entrypoint;
void main() {
group('', entrypoint.main);
}
''');
return fakeTest.path;
}
Future<void> _runTest(File testFile) async {
final PoolResource resource = await pool.request();
final String testPath = _createTest(testFile);
final int port = await _findPort();
final Uri coverageUri = Uri.parse('http://127.0.0.1:$port');
final Completer<void> completer = Completer<void>();
final String packagesPath = argResults['packages'];
final Process testProcess = await Process.start(
Platform.resolvedExecutable,
<String>[
'--packages=$packagesPath',
'--pause-isolates-on-exit',
'--enable-asserts',
'--enable-vm-service=${coverageUri.port}',
testPath,
],
runInShell: true,
environment: <String, String>{
'FLUTTER_ROOT': flutterRoot,
}).timeout(const Duration(seconds: 30));
testProcess.stdout
.transform(utf8.decoder)
.transform(const LineSplitter())
.listen((String line) {
print(line);
if (line.contains('All tests passed') || line.contains('Some tests failed')) {
completer.complete(null);
}
});
try {
await completer.future;
await coverageCollector.collectCoverage(testProcess, coverageUri).timeout(const Duration(seconds: 30));
testProcess?.kill();
} on TimeoutException {
print('Failed to collect coverage for ${testFile.path} after 30 seconds');
} finally {
resource.release();
}
}
Future<int> _findPort() async {
int port = 0;
ServerSocket serverSocket;
try {
serverSocket = await ServerSocket.bind(InternetAddress.loopbackIPv4.address, 0);
port = serverSocket.port;
} catch (e) {
// Failures are signaled by a return value of 0 from this function.
print('_findPort failed: $e');
}
if (serverSocket != null) {
await serverSocket.close();
}
return port;
..writeAsStringSync(result);
}
}
class VMEnvironment implements Environment {
VMEnvironment(this.observatoryUrl, this._isolate);
@override
final bool supportsDebugging = false;
@override
final Uri observatoryUrl;
/// The VM service isolate object used to control this isolate.
final VMIsolateRef _isolate;
@override
Uri get remoteDebuggerUrl => null;
@override
Stream<void> get onRestart => StreamController<dynamic>.broadcast().stream;
@override
CancelableOperation<void> displayPause() {
final CancelableCompleter<dynamic> completer = CancelableCompleter<dynamic>(onCancel: () => _isolate.resume());
completer.complete(_isolate.pause().then((dynamic _) => _isolate.onPauseOrResume
.firstWhere((VMPauseEvent event) => event is VMResumeEvent)));
return completer.operation;
}
}