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