From f6985f96fecaf988e052e37cd2e4c26dfc40ec73 Mon Sep 17 00:00:00 2001
From: Jacob MacDonald <jakemac@google.com>
Date: Wed, 27 Jun 2018 14:55:46 -0700
Subject: [PATCH] Add support for loading precompiled kernel files for vm tests
 (#896)

* support loading .vm.app.dill files in precompiled mode

* add a test for loading from dill files

* update pubspec/changelog for 1.2.0 release
---
 CHANGELOG.md                      | 10 +++++++++-
 lib/src/runner/vm/platform.dart   |  9 +++++++--
 pubspec.yaml                      |  2 +-
 test/runner/precompiled_test.dart | 29 ++++++++++++++++++++++++++++-
 4 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index b020e726..90cc02a3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,11 @@
+## 1.2.0
+
+* Added support for using precompiled kernel files when running vm tests.
+  * When using the `--precompiled` flag we will now first check for a
+    `<original-test-path>.vm_test.vm.app.dill` file, and if present load that
+    directly in the isolate. Otherwise the `<original-test-path>.vm_test.dart`
+    file will be used.
+
 ## 1.1.0
 
 * Added a new `pid` field to the StartEvent in the json runner containing the
@@ -18,7 +26,7 @@
 
   - Many improvements to `TypeMatcher`
     - Can now be used directly as `const TypeMatcher<MyType>()`.
-    - Added a type parameter to specify the target `Type`. 
+    - Added a type parameter to specify the target `Type`.
       - Made the `name` constructor parameter optional and marked it deprecated.
         It's redundant to the type parameter.
     - Migrated all `isType` matchers to `TypeMatcher`.
diff --git a/lib/src/runner/vm/platform.dart b/lib/src/runner/vm/platform.dart
index 6b9db852..506fffa9 100644
--- a/lib/src/runner/vm/platform.dart
+++ b/lib/src/runner/vm/platform.dart
@@ -130,8 +130,13 @@ Future<Isolate> _spawnDataIsolate(String path, SendPort message) async {
 
 Future<Isolate> _spawnPrecompiledIsolate(
     String testPath, SendPort message, String precompiledPath) async {
-  testPath = p.join(precompiledPath, testPath) + '.vm_test.dart';
-  return await Isolate.spawnUri(p.toUri(p.absolute(testPath)), [], message,
+  testPath = p.absolute(p.join(precompiledPath, testPath) + '.vm_test.dart');
+  var dillTestpath =
+      testPath.substring(0, testPath.length - '.dart'.length) + '.vm.app.dill';
+  if (await new File(dillTestpath).exists()) {
+    testPath = dillTestpath;
+  }
+  return await Isolate.spawnUri(p.toUri(testPath), [], message,
       packageConfig: p.toUri(p.join(precompiledPath, '.packages')),
       checked: true);
 }
diff --git a/pubspec.yaml b/pubspec.yaml
index fbb57f80..6fdb0a3c 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
 name: test
-version: 1.1.0
+version: 1.2.0
 author: Dart Team <misc@dartlang.org>
 description: A library for writing dart unit tests.
 homepage: https://github.com/dart-lang/test
diff --git a/test/runner/precompiled_test.dart b/test/runner/precompiled_test.dart
index 2ec5c48e..6bcf3cc7 100644
--- a/test/runner/precompiled_test.dart
+++ b/test/runner/precompiled_test.dart
@@ -115,7 +115,7 @@ void main() {
   }, tags: const ["node"]);
 
   group("vm tests", () {
-    test("run in the precompiled directory", () async {
+    setUp(() async {
       await d.dir('test', [
         d.file("test.dart", """
           import "package:test/test.dart";
@@ -135,13 +135,40 @@ void main() {
         """),
       ]).create();
       await _writePackagesFile();
+    });
 
+    test("run in the precompiled directory", () async {
       var test = await runTest(
           ["-p", "vm", '--precompiled=${d.sandbox}', 'test/test.dart']);
       expect(test.stdout,
           containsInOrder(["+0: true is true", "+1: All tests passed!"]));
       await test.shouldExit(0);
     });
+
+    test("can load precompiled dill files if available", () async {
+      // Create the snapshot in the sandbox directory.
+      var snapshotProcess = await runDart([
+        '--snapshot_kind=script',
+        '--snapshot=test/test.dart.vm_test.vm.app.dill',
+        'test/test.dart.vm_test.dart'
+      ]);
+      await snapshotProcess.shouldExit(0);
+
+      // Modify the original test so it would fail if it actually got ran, this
+      // makes sure the test fails if the dill file isn't loaded.
+      var testFile = new File(p.join(d.sandbox, 'test', 'test.dart'));
+      expect(await testFile.exists(), isTrue);
+      var originalContent = await testFile.readAsString();
+      await testFile
+          .writeAsString(originalContent.replaceAll('isTrue', 'isFalse'));
+
+      // Actually invoke the test with the dill file.
+      var testProcess = await runTest(
+          ["-p", "vm", '--precompiled=${d.sandbox}', 'test/test.dart']);
+      expect(testProcess.stdout,
+          containsInOrder(["+0: true is true", "+1: All tests passed!"]));
+      await testProcess.shouldExit(0);
+    });
   });
 }
 
-- 
GitLab