OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, 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 library unittest.loader; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:io'; | |
9 import 'dart:isolate'; | |
10 | |
11 import 'package:path/path.dart' as p; | |
12 | |
13 import 'dart.dart'; | |
14 import 'io.dart'; | |
15 import 'isolate_test.dart'; | |
16 import 'load_exception.dart'; | |
17 import 'remote_exception.dart'; | |
18 import 'suite.dart'; | |
19 | |
20 /// A class for finding test files and loading them into a runnable form. | |
21 class Loader { | |
22 /// The package root to use for loading tests, or `null` to use the automatic | |
23 /// root. | |
24 final String _packageRoot; | |
25 | |
26 /// All isolates that have been spun up by the loader. | |
27 final _isolates = new Set<Isolate>(); | |
28 | |
29 /// Creates a new loader. | |
30 /// | |
31 /// If [packageRoot] is passed, it's used as the package root for all loaded | |
32 /// tests. Otherwise, the `packages/` directories next to the test entrypoints | |
33 /// will be used. | |
34 Loader({String packageRoot}) | |
35 : _packageRoot = packageRoot; | |
36 | |
37 /// Loads all test suites in [dir]. | |
38 /// | |
39 /// This will load tests from files that end in "_test.dart". | |
40 Future<Set<Suite>> loadDir(String dir) { | |
41 return Future.wait(new Directory(dir).listSync(recursive: true) | |
42 .map((entry) { | |
43 if (entry is! File) return new Future.value(); | |
44 if (!entry.path.endsWith("_test.dart")) return new Future.value(); | |
45 if (p.split(entry.path).contains('packages')) return new Future.value(); | |
46 | |
47 // TODO(nweiz): Provide a way for the caller to gracefully handle some | |
48 // isolates failing to load without stopping the rest. | |
49 return loadFile(entry.path); | |
50 })).then((suites) => suites.toSet()..remove(null)); | |
51 } | |
52 | |
53 /// Loads a test suite from the file at [path]. | |
54 /// | |
55 /// This will throw a [LoadException] if the file fails to load. | |
56 Future<Suite> loadFile(String path) { | |
57 // TODO(nweiz): Support browser tests. | |
58 var packageRoot = _packageRoot == null | |
59 ? p.join(p.dirname(path), 'packages') | |
60 : _packageRoot; | |
61 | |
62 if (!new Directory(packageRoot).existsSync()) { | |
63 throw new LoadException(path, "Directory $packageRoot does not exist."); | |
64 } | |
65 | |
66 var receivePort = new ReceivePort(); | |
67 return runInIsolate(''' | |
68 import "package:unittest/src/vm_listener.dart"; | |
69 | |
70 import "${p.toUri(p.absolute(path))}" as test; | |
71 | |
72 void main(_, Map message) { | |
73 var sendPort = message['reply']; | |
74 VmListener.start(sendPort, () => test.main); | |
75 } | |
76 ''', { | |
77 'reply': receivePort.sendPort | |
78 }, packageRoot: packageRoot).catchError((error, stackTrace) { | |
79 receivePort.close(); | |
80 return new Future.error(new LoadException(path, error), stackTrace); | |
81 }).then((isolate) { | |
82 _isolates.add(isolate); | |
83 return receivePort.first; | |
84 }).then((response) { | |
85 if (response["type"] == "loadException") { | |
86 return new Future.error(new LoadException(path, response["message"])); | |
87 } else if (response["type"] == "error") { | |
88 var asyncError = RemoteException.deserialize(response["error"]); | |
89 return new Future.error( | |
90 new LoadException(path, asyncError.error), | |
91 asyncError.stackTrace); | |
92 } | |
93 | |
94 return new Suite(path, response["tests"].map((test) { | |
95 return new IsolateTest(test['name'], test['sendPort']); | |
96 })); | |
97 }); | |
98 } | |
99 | |
100 /// Closes the loader and releases all resources allocated by it. | |
101 Future close() { | |
102 for (var isolate in _isolates) { | |
103 isolate.kill(); | |
104 } | |
105 _isolates.clear(); | |
106 return new Future.value(); | |
107 } | |
108 } | |
OLD | NEW |