Conditionally import coverage logic (#1091)

* Conditionally import coverage logic
diff --git a/pkgs/test/CHANGELOG.md b/pkgs/test/CHANGELOG.md
index 9278b43..377bd8d 100644
--- a/pkgs/test/CHANGELOG.md
+++ b/pkgs/test/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 1.9.1
+
+* Depend on latest `test_core`.
+
 ## 1.9.0
 
 * Implement code coverage collection for VM based tests
diff --git a/pkgs/test/pubspec.yaml b/pkgs/test/pubspec.yaml
index c94a1ad..6164aa7 100644
--- a/pkgs/test/pubspec.yaml
+++ b/pkgs/test/pubspec.yaml
@@ -1,5 +1,5 @@
 name: test
-version: 1.9.0
+version: 1.9.1
 author: Dart Team <misc@dartlang.org>
 description: A full featured library for writing and running Dart tests.
 homepage: https://github.com/dart-lang/test/blob/master/pkgs/test
@@ -32,7 +32,7 @@
   yaml: ^2.0.0
   # Use an exact version until the test_api and test_core package are stable.
   test_api: 0.2.8
-  test_core: 0.2.11
+  test_core: 0.2.12
 
 dev_dependencies:
   fake_async: ^1.0.0
diff --git a/pkgs/test_core/CHANGELOG.md b/pkgs/test_core/CHANGELOG.md
index e25a3c4..f39f92a 100644
--- a/pkgs/test_core/CHANGELOG.md
+++ b/pkgs/test_core/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 0.2.12
+
+* Conditionally import coverage logic in `engine.dart`. This ensures the engine
+  is platform agnostic.
+
 ## 0.2.11
 
 * Implement code coverage gathering for VM tests.
diff --git a/pkgs/test_core/lib/src/runner/coverage.dart b/pkgs/test_core/lib/src/runner/coverage.dart
new file mode 100644
index 0000000..7dc2184
--- /dev/null
+++ b/pkgs/test_core/lib/src/runner/coverage.dart
@@ -0,0 +1,34 @@
+// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
+// for details. 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';
+import 'dart:io';
+
+import 'package:coverage/coverage.dart';
+import 'package:path/path.dart' as p;
+
+import 'live_suite_controller.dart';
+import 'runner_suite.dart';
+
+/// Collects coverage and outputs to the [coverage] path.
+Future<void> gatherCoverage(
+    String coverage, LiveSuiteController controller) async {
+  final RunnerSuite suite = controller.liveSuite.suite;
+
+  if (!suite.platform.runtime.isDartVM) return;
+
+  final String isolateId = Uri.parse(suite.environment.observatoryUrl.fragment)
+      .queryParameters['isolateId'];
+
+  final cov = await collect(
+      suite.environment.observatoryUrl, false, false, false, Set(),
+      isolateIds: {isolateId});
+
+  final outfile = File(p.join('$coverage', '${suite.path}.vm.json'))
+    ..createSync(recursive: true);
+  final IOSink out = outfile.openWrite();
+  out.write(json.encode(cov));
+  await out.flush();
+  await out.close();
+}
diff --git a/pkgs/test_core/lib/src/runner/coverage_stub.dart b/pkgs/test_core/lib/src/runner/coverage_stub.dart
new file mode 100644
index 0000000..5a2285f
--- /dev/null
+++ b/pkgs/test_core/lib/src/runner/coverage_stub.dart
@@ -0,0 +1,9 @@
+// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'live_suite_controller.dart';
+
+Future<void> gatherCoverage(String coverage, LiveSuiteController controller) =>
+    throw UnsupportedError(
+        'Coverage is only supported through the test runner.');
diff --git a/pkgs/test_core/lib/src/runner/engine.dart b/pkgs/test_core/lib/src/runner/engine.dart
index 11ae315..c0f922d 100644
--- a/pkgs/test_core/lib/src/runner/engine.dart
+++ b/pkgs/test_core/lib/src/runner/engine.dart
@@ -4,16 +4,11 @@
 
 import 'dart:async';
 import 'dart:collection';
-import 'dart:convert';
-import 'dart:io';
 
 import 'package:async/async.dart' hide Result;
 import 'package:collection/collection.dart';
-import 'package:coverage/coverage.dart';
-import 'package:path/path.dart' as p;
 import 'package:pedantic/pedantic.dart';
 import 'package:pool/pool.dart';
-
 import 'package:test_api/src/backend/group.dart'; // ignore: implementation_imports
 import 'package:test_api/src/backend/invoker.dart'; // ignore: implementation_imports
 import 'package:test_api/src/backend/live_test.dart'; // ignore: implementation_imports
@@ -23,10 +18,11 @@
 import 'package:test_api/src/backend/test.dart'; // ignore: implementation_imports
 import 'package:test_api/src/util/iterable_set.dart'; // ignore: implementation_imports
 
-import 'runner_suite.dart';
+import 'coverage_stub.dart' if (dart.library.io) 'coverage.dart';
 import 'live_suite.dart';
 import 'live_suite_controller.dart';
 import 'load_suite.dart';
+import 'runner_suite.dart';
 
 /// An [Engine] manages a run that encompasses multiple test suites.
 ///
@@ -288,7 +284,7 @@
           if (_closed) return;
           await _runGroup(controller, controller.liveSuite.suite.group, []);
           controller.noMoreLiveTests();
-          await _gatherCoverage(controller);
+          if (_coverage != null) await gatherCoverage(_coverage, controller);
           loadResource.allowRelease(() => controller.close());
         });
       }());
@@ -303,29 +299,6 @@
     return success;
   }
 
-  Future<Null> _gatherCoverage(LiveSuiteController controller) async {
-    if (_coverage == null) return;
-
-    final RunnerSuite suite = controller.liveSuite.suite;
-
-    if (!suite.platform.runtime.isDartVM) return;
-
-    final String isolateId =
-        Uri.parse(suite.environment.observatoryUrl.fragment)
-            .queryParameters['isolateId'];
-
-    final cov = await collect(
-        suite.environment.observatoryUrl, false, false, false, Set(),
-        isolateIds: {isolateId});
-
-    final outfile = File(p.join('$_coverage', '${suite.path}.vm.json'))
-      ..createSync(recursive: true);
-    final IOSink out = outfile.openWrite();
-    out.write(json.encode(cov));
-    await out.flush();
-    await out.close();
-  }
-
   /// Runs all the entries in [group] in sequence.
   ///
   /// [suiteController] is the controller fo the suite that contains [group].
diff --git a/pkgs/test_core/pubspec.yaml b/pkgs/test_core/pubspec.yaml
index 9aec1fb..0e05027 100644
--- a/pkgs/test_core/pubspec.yaml
+++ b/pkgs/test_core/pubspec.yaml
@@ -1,5 +1,5 @@
 name: test_core
-version: 0.2.11
+version: 0.2.12
 author: Dart Team <misc@dartlang.org>
 description: A basic library for writing tests and running them on the VM.
 homepage: https://github.com/dart-lang/test/blob/master/pkgs/test_core