Collect test coverage

This commit is contained in:
Simon Binder 2019-03-19 12:52:02 +01:00
parent d4b97ab6fc
commit 74ee36bc98
No known key found for this signature in database
GPG Key ID: B807FDF954BA00CF
5 changed files with 61 additions and 2 deletions

3
moor/.gitignore vendored
View File

@ -2,6 +2,9 @@
# Created by https://www.gitignore.io/api/dart,intellij
# Edit at https://www.gitignore.io/?templates=dart,intellij
coverage.json
lcov.info
### Dart ###
# See https://www.dartlang.org/guides/libraries/private-files

View File

@ -6,4 +6,4 @@ stages:
- dartanalyzer: --fatal-infos --fatal-warnings .
- dartfmt
- unit_test:
- command: pub run build_runner test --delete-conflicting-outputs
- command: dart tool/coverage.dart

View File

@ -19,7 +19,13 @@ dev_dependencies:
test: ^1.5.3
test_api:
mockito: ^4.0.0
grinder: ^0.8.0
coverage: ^0.12.0
dependency_overrides:
moor_generator:
path: ../moor_generator
path: ../moor_generator
coveralls: ^5.1.0
# Temporarily use my fork because it can collect coverage when running tests with the test runner
coverage:
git: https://github.com/simolus3/coverage.git

41
moor/tool/coverage.dart Normal file
View File

@ -0,0 +1,41 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:grinder/grinder_sdk.dart';
import 'package:coverage/coverage.dart';
import 'package:path/path.dart';
const int _vmPort = 9876;
Future<void> main(List<String> args) async {
// First, generate the build script, see
// https://github.com/dart-lang/build/blob/3208cfe94c475ed3e1ec44c227aadaddaeac263d/build_runner/bin/build_runner.dart#L65
Pub.run('build_runner', arguments: ['generate-build-script']);
// Start coverage collection (resume isolates, don't wait for pause, collect
// when isolates exit).
final collectorFuture = collect(Uri.parse('http://localhost:$_vmPort'), true, false, true);
// Next, run the test script in another dart process that has the vm services
// enabled.
final tests = join(File.fromUri(Platform.script).parent.path, 'tester.dart');
// not using Dart.run because that only prints to stdout after the process has
// completed.
await Dart.runAsync(tests, vmArgs: ['--enable-vm-service=$_vmPort', '--pause-isolates-on-exit']);
File('coverage.json').writeAsStringSync(json.encode(await collectorFuture));
print('coverage collected - formatting as lcov');
print('formatting to .lcov format');
await Pub.runAsync('coverage:format_coverage', arguments: [
'--lcov',
'--packages=.packages',
'--report-on=lib',
'-i',
'coverage.json',
'-o',
'lcov.info'
]);
}

9
moor/tool/tester.dart Normal file
View File

@ -0,0 +1,9 @@
import 'package:test_core/src/executable.dart' as test;
import '../.dart_tool/build/entrypoint/build.dart' as builder;
void main() async {
// Run the build runner
await builder.main(['build', '--delete-conflicting-outputs']);
// Run tests
await test.main([]);
}