OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library test.runner.loader; | 5 library test.runner.loader; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 | 10 |
11 import 'package:analyzer/analyzer.dart'; | 11 import 'package:analyzer/analyzer.dart'; |
12 import 'package:path/path.dart' as p; | 12 import 'package:path/path.dart' as p; |
13 import 'package:stack_trace/stack_trace.dart'; | 13 import 'package:stack_trace/stack_trace.dart'; |
14 | 14 |
15 import '../backend/invoker.dart'; | 15 import '../backend/invoker.dart'; |
16 import '../backend/metadata.dart'; | 16 import '../backend/metadata.dart'; |
17 import '../backend/suite.dart'; | |
18 import '../backend/test_platform.dart'; | 17 import '../backend/test_platform.dart'; |
19 import '../util/async_thunk.dart'; | 18 import '../util/async_thunk.dart'; |
20 import '../util/dart.dart' as dart; | 19 import '../util/dart.dart' as dart; |
21 import '../util/io.dart'; | 20 import '../util/io.dart'; |
22 import '../util/remote_exception.dart'; | 21 import '../util/remote_exception.dart'; |
23 import '../utils.dart'; | 22 import '../utils.dart'; |
24 import 'browser/server.dart'; | 23 import 'browser/server.dart'; |
25 import 'load_exception.dart'; | 24 import 'load_exception.dart'; |
26 import 'load_suite.dart'; | 25 import 'load_suite.dart'; |
27 import 'parse_metadata.dart'; | 26 import 'parse_metadata.dart'; |
| 27 import 'runner_suite.dart'; |
28 import 'vm/isolate_test.dart'; | 28 import 'vm/isolate_test.dart'; |
29 | 29 |
30 /// A class for finding test files and loading them into a runnable form. | 30 /// A class for finding test files and loading them into a runnable form. |
31 class Loader { | 31 class Loader { |
32 /// All platforms for which tests should be loaded. | 32 /// All platforms for which tests should be loaded. |
33 final List<TestPlatform> _platforms; | 33 final List<TestPlatform> _platforms; |
34 | 34 |
35 /// Whether to enable colors for Dart compilation. | 35 /// Whether to enable colors for Dart compilation. |
36 final bool _color; | 36 final bool _color; |
37 | 37 |
38 /// Whether raw JavaScript stack traces should be used for tests that are | 38 /// Whether raw JavaScript stack traces should be used for tests that are |
39 /// compiled to JavaScript. | 39 /// compiled to JavaScript. |
40 final bool _jsTrace; | 40 final bool _jsTrace; |
41 | 41 |
42 /// Global metadata that applies to all test suites. | 42 /// Global metadata that applies to all test suites. |
43 final Metadata _metadata; | 43 final Metadata _metadata; |
44 | 44 |
45 /// The root directory that will be served for browser tests. | 45 /// The root directory that will be served for browser tests. |
46 final String _root; | 46 final String _root; |
47 | 47 |
48 /// The package root to use for loading tests. | 48 /// The package root to use for loading tests. |
49 final String _packageRoot; | 49 final String _packageRoot; |
50 | 50 |
51 /// The URL for the `pub serve` instance to use to load tests. | 51 /// The URL for the `pub serve` instance to use to load tests. |
52 /// | 52 /// |
53 /// This is `null` if tests should be loaded from the filesystem. | 53 /// This is `null` if tests should be loaded from the filesystem. |
54 final Uri _pubServeUrl; | 54 final Uri _pubServeUrl; |
55 | 55 |
56 /// All suites that have been created by the loader. | 56 /// All suites that have been created by the loader. |
57 final _suites = new Set<Suite>(); | 57 final _suites = new Set<RunnerSuite>(); |
58 | 58 |
59 /// The server that serves browser test pages. | 59 /// The server that serves browser test pages. |
60 /// | 60 /// |
61 /// This is lazily initialized the first time it's accessed. | 61 /// This is lazily initialized the first time it's accessed. |
62 Future<BrowserServer> get _browserServer { | 62 Future<BrowserServer> get _browserServer { |
63 return _browserServerThunk.run(() { | 63 return _browserServerThunk.run(() { |
64 return BrowserServer.start( | 64 return BrowserServer.start( |
65 root: _root, | 65 root: _root, |
66 packageRoot: _packageRoot, | 66 packageRoot: _packageRoot, |
67 pubServeUrl: _pubServeUrl, | 67 pubServeUrl: _pubServeUrl, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 _packageRoot = packageRootFor(root, packageRoot), | 99 _packageRoot = packageRootFor(root, packageRoot), |
100 _color = color, | 100 _color = color, |
101 _jsTrace = jsTrace, | 101 _jsTrace = jsTrace, |
102 _metadata = metadata == null ? new Metadata() : metadata; | 102 _metadata = metadata == null ? new Metadata() : metadata; |
103 | 103 |
104 /// Loads all test suites in [dir]. | 104 /// Loads all test suites in [dir]. |
105 /// | 105 /// |
106 /// This will load tests from files that end in "_test.dart". Any tests that | 106 /// This will load tests from files that end in "_test.dart". Any tests that |
107 /// fail to load will be emitted as [LoadException]s. | 107 /// fail to load will be emitted as [LoadException]s. |
108 /// | 108 /// |
109 /// This emits [LoadSuite]s that must then be run to emit the actual [Suite]s | 109 /// This emits [LoadSuite]s that must then be run to emit the actual |
110 /// defined in the file. | 110 /// [RunnerSuite]s defined in the file. |
111 Stream<LoadSuite> loadDir(String dir) { | 111 Stream<LoadSuite> loadDir(String dir) { |
112 return mergeStreams(new Directory(dir).listSync(recursive: true) | 112 return mergeStreams(new Directory(dir).listSync(recursive: true) |
113 .map((entry) { | 113 .map((entry) { |
114 if (entry is! File) return new Stream.fromIterable([]); | 114 if (entry is! File) return new Stream.fromIterable([]); |
115 | 115 |
116 if (!entry.path.endsWith("_test.dart")) { | 116 if (!entry.path.endsWith("_test.dart")) { |
117 return new Stream.fromIterable([]); | 117 return new Stream.fromIterable([]); |
118 } | 118 } |
119 | 119 |
120 if (p.split(entry.path).contains('packages')) { | 120 if (p.split(entry.path).contains('packages')) { |
121 return new Stream.fromIterable([]); | 121 return new Stream.fromIterable([]); |
122 } | 122 } |
123 | 123 |
124 return loadFile(entry.path); | 124 return loadFile(entry.path); |
125 })); | 125 })); |
126 } | 126 } |
127 | 127 |
128 /// Loads a test suite from the file at [path]. | 128 /// Loads a test suite from the file at [path]. |
129 /// | 129 /// |
130 /// This emits [LoadSuite]s that must then be run to emit the actual [Suite]s | 130 /// This emits [LoadSuite]s that must then be run to emit the actual |
131 /// defined in the file. | 131 /// [RunnerSuite]s defined in the file. |
132 /// | 132 /// |
133 /// This will emit a [LoadException] if the file fails to load. | 133 /// This will emit a [LoadException] if the file fails to load. |
134 Stream<LoadSuite> loadFile(String path) async* { | 134 Stream<LoadSuite> loadFile(String path) async* { |
135 var suiteMetadata; | 135 var suiteMetadata; |
136 try { | 136 try { |
137 suiteMetadata = parseMetadata(path); | 137 suiteMetadata = parseMetadata(path); |
138 } on AnalyzerErrorGroup catch (_) { | 138 } on AnalyzerErrorGroup catch (_) { |
139 // Ignore the analyzer's error, since its formatting is much worse than | 139 // Ignore the analyzer's error, since its formatting is much worse than |
140 // the VM's or dart2js's. | 140 // the VM's or dart2js's. |
141 suiteMetadata = new Metadata(); | 141 suiteMetadata = new Metadata(); |
(...skipping 10 matching lines...) Expand all Loading... |
152 return; | 152 return; |
153 } | 153 } |
154 | 154 |
155 for (var platform in _platforms) { | 155 for (var platform in _platforms) { |
156 if (!suiteMetadata.testOn.evaluate(platform, os: currentOS)) continue; | 156 if (!suiteMetadata.testOn.evaluate(platform, os: currentOS)) continue; |
157 | 157 |
158 var metadata = suiteMetadata.forPlatform(platform, os: currentOS); | 158 var metadata = suiteMetadata.forPlatform(platform, os: currentOS); |
159 | 159 |
160 // Don't load a skipped suite. | 160 // Don't load a skipped suite. |
161 if (metadata.skip) { | 161 if (metadata.skip) { |
162 yield new LoadSuite.forSuite(new Suite([ | 162 yield new LoadSuite.forSuite(new RunnerSuite([ |
163 new LocalTest(path, metadata, () {}) | 163 new LocalTest(path, metadata, () {}) |
164 ], path: path, platform: platform, metadata: metadata)); | 164 ], path: path, platform: platform, metadata: metadata)); |
165 continue; | 165 continue; |
166 } | 166 } |
167 | 167 |
168 var name = (platform.isJS ? "compiling " : "loading ") + path; | 168 var name = (platform.isJS ? "compiling " : "loading ") + path; |
169 yield new LoadSuite(name, () { | 169 yield new LoadSuite(name, () { |
170 return platform == TestPlatform.vm | 170 return platform == TestPlatform.vm |
171 ? _loadVmFile(path, metadata) | 171 ? _loadVmFile(path, metadata) |
172 : _loadBrowserFile(path, platform, metadata); | 172 : _loadBrowserFile(path, platform, metadata); |
173 }, platform: platform); | 173 }, platform: platform); |
174 } | 174 } |
175 } | 175 } |
176 | 176 |
177 /// Load the test suite at [path] in [platform]. | 177 /// Load the test suite at [path] in [platform]. |
178 /// | 178 /// |
179 /// [metadata] is the suite-level metadata for the test. | 179 /// [metadata] is the suite-level metadata for the test. |
180 Future<Suite> _loadBrowserFile(String path, TestPlatform platform, | 180 Future<RunnerSuite> _loadBrowserFile(String path, TestPlatform platform, |
181 Metadata metadata) async => | 181 Metadata metadata) async => |
182 (await _browserServer).loadSuite(path, platform, metadata); | 182 (await _browserServer).loadSuite(path, platform, metadata); |
183 | 183 |
184 /// Load the test suite at [path] in VM isolate. | 184 /// Load the test suite at [path] in VM isolate. |
185 /// | 185 /// |
186 /// [metadata] is the suite-level metadata for the test. | 186 /// [metadata] is the suite-level metadata for the test. |
187 Future<Suite> _loadVmFile(String path, Metadata metadata) async { | 187 Future<RunnerSuite> _loadVmFile(String path, Metadata metadata) async { |
188 var receivePort = new ReceivePort(); | 188 var receivePort = new ReceivePort(); |
189 | 189 |
190 var isolate; | 190 var isolate; |
191 try { | 191 try { |
192 if (_pubServeUrl != null) { | 192 if (_pubServeUrl != null) { |
193 var url = _pubServeUrl.resolveUri( | 193 var url = _pubServeUrl.resolveUri( |
194 p.toUri(p.relative(path, from: 'test') + '.vm_test.dart')); | 194 p.toUri(p.relative(path, from: 'test') + '.vm_test.dart')); |
195 | 195 |
196 // TODO(nweiz): Remove new Future.sync() once issue 23498 has been fixed | 196 // TODO(nweiz): Remove new Future.sync() once issue 23498 has been fixed |
197 // in two stable versions. | 197 // in two stable versions. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 completer.completeError( | 255 completer.completeError( |
256 new LoadException(path, asyncError.error), | 256 new LoadException(path, asyncError.error), |
257 asyncError.stackTrace); | 257 asyncError.stackTrace); |
258 } else { | 258 } else { |
259 assert(response["type"] == "success"); | 259 assert(response["type"] == "success"); |
260 completer.complete(response["tests"]); | 260 completer.complete(response["tests"]); |
261 } | 261 } |
262 }); | 262 }); |
263 | 263 |
264 try { | 264 try { |
265 var suite = new Suite((await completer.future).map((test) { | 265 var suite = new RunnerSuite((await completer.future).map((test) { |
266 var testMetadata = new Metadata.deserialize(test['metadata']); | 266 var testMetadata = new Metadata.deserialize(test['metadata']); |
267 return new IsolateTest(test['name'], testMetadata, test['sendPort']); | 267 return new IsolateTest(test['name'], testMetadata, test['sendPort']); |
268 }), | 268 }), |
269 metadata: metadata, | 269 metadata: metadata, |
270 path: path, | 270 path: path, |
271 platform: TestPlatform.vm, | 271 platform: TestPlatform.vm, |
272 os: currentOS, | 272 os: currentOS, |
273 onClose: isolate.kill); | 273 onClose: isolate.kill); |
274 _suites.add(suite); | 274 _suites.add(suite); |
275 return suite; | 275 return suite; |
276 } finally { | 276 } finally { |
277 subscription.cancel(); | 277 subscription.cancel(); |
278 } | 278 } |
279 } | 279 } |
280 | 280 |
281 /// Closes the loader and releases all resources allocated by it. | 281 /// Closes the loader and releases all resources allocated by it. |
282 Future close() { | 282 Future close() { |
283 return _closeThunk.run(() async { | 283 return _closeThunk.run(() async { |
284 await Future.wait(_suites.map((suite) => suite.close())); | 284 await Future.wait(_suites.map((suite) => suite.close())); |
285 _suites.clear(); | 285 _suites.clear(); |
286 | 286 |
287 if (!_browserServerThunk.hasRun) return; | 287 if (!_browserServerThunk.hasRun) return; |
288 await (await _browserServer).close(); | 288 await (await _browserServer).close(); |
289 }); | 289 }); |
290 } | 290 } |
291 } | 291 } |
OLD | NEW |