Skip to content

Commit ee7c02b

Browse files
authored
Conditionally import coverage logic (#1091)
* Conditionally import coverage logic
1 parent 08bf237 commit ee7c02b

File tree

7 files changed

+58
-33
lines changed

7 files changed

+58
-33
lines changed

pkgs/test/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.9.1
2+
3+
* Depend on latest `test_core`.
4+
15
## 1.9.0
26

37
* Implement code coverage collection for VM based tests

pkgs/test/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: test
2-
version: 1.9.0
2+
version: 1.9.1
33
author: Dart Team <[email protected]>
44
description: A full featured library for writing and running Dart tests.
55
homepage: https://github.com/dart-lang/test/blob/master/pkgs/test
@@ -32,7 +32,7 @@ dependencies:
3232
yaml: ^2.0.0
3333
# Use an exact version until the test_api and test_core package are stable.
3434
test_api: 0.2.8
35-
test_core: 0.2.11
35+
test_core: 0.2.12
3636

3737
dev_dependencies:
3838
fake_async: ^1.0.0

pkgs/test_core/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.2.12
2+
3+
* Conditionally import coverage logic in `engine.dart`. This ensures the engine
4+
is platform agnostic.
5+
16
## 0.2.11
27

38
* Implement code coverage gathering for VM tests.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:convert';
6+
import 'dart:io';
7+
8+
import 'package:coverage/coverage.dart';
9+
import 'package:path/path.dart' as p;
10+
11+
import 'live_suite_controller.dart';
12+
import 'runner_suite.dart';
13+
14+
/// Collects coverage and outputs to the [coverage] path.
15+
Future<void> gatherCoverage(
16+
String coverage, LiveSuiteController controller) async {
17+
final RunnerSuite suite = controller.liveSuite.suite;
18+
19+
if (!suite.platform.runtime.isDartVM) return;
20+
21+
final String isolateId = Uri.parse(suite.environment.observatoryUrl.fragment)
22+
.queryParameters['isolateId'];
23+
24+
final cov = await collect(
25+
suite.environment.observatoryUrl, false, false, false, Set(),
26+
isolateIds: {isolateId});
27+
28+
final outfile = File(p.join('$coverage', '${suite.path}.vm.json'))
29+
..createSync(recursive: true);
30+
final IOSink out = outfile.openWrite();
31+
out.write(json.encode(cov));
32+
await out.flush();
33+
await out.close();
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'live_suite_controller.dart';
6+
7+
Future<void> gatherCoverage(String coverage, LiveSuiteController controller) =>
8+
throw UnsupportedError(
9+
'Coverage is only supported through the test runner.');

pkgs/test_core/lib/src/runner/engine.dart

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@
44

55
import 'dart:async';
66
import 'dart:collection';
7-
import 'dart:convert';
8-
import 'dart:io';
97

108
import 'package:async/async.dart' hide Result;
119
import 'package:collection/collection.dart';
12-
import 'package:coverage/coverage.dart';
13-
import 'package:path/path.dart' as p;
1410
import 'package:pedantic/pedantic.dart';
1511
import 'package:pool/pool.dart';
16-
1712
import 'package:test_api/src/backend/group.dart'; // ignore: implementation_imports
1813
import 'package:test_api/src/backend/invoker.dart'; // ignore: implementation_imports
1914
import 'package:test_api/src/backend/live_test.dart'; // ignore: implementation_imports
@@ -23,10 +18,11 @@ import 'package:test_api/src/backend/state.dart'; // ignore: implementation_impo
2318
import 'package:test_api/src/backend/test.dart'; // ignore: implementation_imports
2419
import 'package:test_api/src/util/iterable_set.dart'; // ignore: implementation_imports
2520

26-
import 'runner_suite.dart';
21+
import 'coverage_stub.dart' if (dart.library.io) 'coverage.dart';
2722
import 'live_suite.dart';
2823
import 'live_suite_controller.dart';
2924
import 'load_suite.dart';
25+
import 'runner_suite.dart';
3026

3127
/// An [Engine] manages a run that encompasses multiple test suites.
3228
///
@@ -288,7 +284,7 @@ class Engine {
288284
if (_closed) return;
289285
await _runGroup(controller, controller.liveSuite.suite.group, []);
290286
controller.noMoreLiveTests();
291-
await _gatherCoverage(controller);
287+
if (_coverage != null) await gatherCoverage(_coverage, controller);
292288
loadResource.allowRelease(() => controller.close());
293289
});
294290
}());
@@ -303,29 +299,6 @@ class Engine {
303299
return success;
304300
}
305301

306-
Future<Null> _gatherCoverage(LiveSuiteController controller) async {
307-
if (_coverage == null) return;
308-
309-
final RunnerSuite suite = controller.liveSuite.suite;
310-
311-
if (!suite.platform.runtime.isDartVM) return;
312-
313-
final String isolateId =
314-
Uri.parse(suite.environment.observatoryUrl.fragment)
315-
.queryParameters['isolateId'];
316-
317-
final cov = await collect(
318-
suite.environment.observatoryUrl, false, false, false, Set(),
319-
isolateIds: {isolateId});
320-
321-
final outfile = File(p.join('$_coverage', '${suite.path}.vm.json'))
322-
..createSync(recursive: true);
323-
final IOSink out = outfile.openWrite();
324-
out.write(json.encode(cov));
325-
await out.flush();
326-
await out.close();
327-
}
328-
329302
/// Runs all the entries in [group] in sequence.
330303
///
331304
/// [suiteController] is the controller fo the suite that contains [group].

pkgs/test_core/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: test_core
2-
version: 0.2.11
2+
version: 0.2.12
33
author: Dart Team <[email protected]>
44
description: A basic library for writing tests and running them on the VM.
55
homepage: https://github.com/dart-lang/test/blob/master/pkgs/test_core

0 commit comments

Comments
 (0)