| 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 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 /// If [color] is true, console colors will be used when compiling Dart. | 73 /// If [color] is true, console colors will be used when compiling Dart. |
| 74 Loader(Iterable<TestPlatform> platforms, {String packageRoot, | 74 Loader(Iterable<TestPlatform> platforms, {String packageRoot, |
| 75 Uri pubServeUrl, bool color: false}) | 75 Uri pubServeUrl, bool color: false}) |
| 76 : _platforms = platforms.toList(), | 76 : _platforms = platforms.toList(), |
| 77 _pubServeUrl = pubServeUrl, | 77 _pubServeUrl = pubServeUrl, |
| 78 _packageRoot = packageRoot, | 78 _packageRoot = packageRoot, |
| 79 _color = color; | 79 _color = color; |
| 80 | 80 |
| 81 /// Loads all test suites in [dir]. | 81 /// Loads all test suites in [dir]. |
| 82 /// | 82 /// |
| 83 /// This will load tests from files that end in "_test.dart". | 83 /// This will load tests from files that end in "_test.dart". Any tests that |
| 84 Future<List<Suite>> loadDir(String dir) { | 84 /// fail to load will be emitted as [LoadException]s. |
| 85 return Future.wait(new Directory(dir).listSync(recursive: true) | 85 Stream<Suite> loadDir(String dir) { |
| 86 return mergeStreams(new Directory(dir).listSync(recursive: true) |
| 86 .map((entry) { | 87 .map((entry) { |
| 87 if (entry is! File) return new Future.value([]); | 88 if (entry is! File) return new Stream.fromIterable([]); |
| 88 if (!entry.path.endsWith("_test.dart")) return new Future.value([]); | |
| 89 if (p.split(entry.path).contains('packages')) return new Future.value([]); | |
| 90 | 89 |
| 91 // TODO(nweiz): Provide a way for the caller to gracefully handle some | 90 if (!entry.path.endsWith("_test.dart")) { |
| 92 // suites failing to load without stopping the rest. | 91 return new Stream.fromIterable([]); |
| 92 } |
| 93 |
| 94 if (p.split(entry.path).contains('packages')) { |
| 95 return new Stream.fromIterable([]); |
| 96 } |
| 97 |
| 93 return loadFile(entry.path); | 98 return loadFile(entry.path); |
| 94 })).then((suites) => flatten(suites)); | 99 })); |
| 95 } | 100 } |
| 96 | 101 |
| 97 /// Loads a test suite from the file at [path]. | 102 /// Loads a test suite from the file at [path]. |
| 98 /// | 103 /// |
| 99 /// This will throw a [LoadException] if the file fails to load. | 104 /// This will emit a [LoadException] if the file fails to load. |
| 100 Future<List<Suite>> loadFile(String path) { | 105 Stream<Suite> loadFile(String path) { |
| 101 var metadata; | 106 var metadata; |
| 102 try { | 107 try { |
| 103 metadata = parseMetadata(path); | 108 metadata = parseMetadata(path); |
| 104 } on AnalyzerErrorGroup catch (_) { | 109 } on AnalyzerErrorGroup catch (_) { |
| 105 // Ignore the analyzer's error, since its formatting is much worse than | 110 // Ignore the analyzer's error, since its formatting is much worse than |
| 106 // the VM's or dart2js's. | 111 // the VM's or dart2js's. |
| 107 metadata = new Metadata(); | 112 metadata = new Metadata(); |
| 108 } on FormatException catch (error) { | 113 } on FormatException catch (error, stackTrace) { |
| 109 throw new LoadException(path, error); | 114 return new Stream.fromFuture( |
| 115 new Future.error(new LoadException(path, error), stackTrace)); |
| 110 } | 116 } |
| 111 | 117 |
| 112 return Future.wait(_platforms.map((platform) { | 118 var controller = new StreamController(); |
| 119 Future.forEach(_platforms, (platform) { |
| 120 if (!metadata.testOn.evaluate(platform, os: currentOS)) { |
| 121 return new Future.value(); |
| 122 } |
| 123 |
| 113 return new Future.sync(() { | 124 return new Future.sync(() { |
| 114 if (!metadata.testOn.evaluate(platform, os: currentOS)) return null; | |
| 115 | |
| 116 if (_pubServeUrl != null && !p.isWithin('test', path)) { | 125 if (_pubServeUrl != null && !p.isWithin('test', path)) { |
| 117 throw new LoadException(path, | 126 throw new LoadException(path, |
| 118 'When using "pub serve", all test files must be in test/.'); | 127 'When using "pub serve", all test files must be in test/.'); |
| 119 } | 128 } |
| 120 | 129 |
| 121 if (platform.isBrowser) return _loadBrowserFile(path, platform); | 130 if (platform.isBrowser) return _loadBrowserFile(path, platform); |
| 122 assert(platform == TestPlatform.vm); | 131 assert(platform == TestPlatform.vm); |
| 123 return _loadVmFile(path); | 132 return _loadVmFile(path); |
| 124 }).then((suite) { | 133 }).then((suite) { |
| 125 if (suite == null) return null; | 134 if (suite == null) return; |
| 126 return suite.change(metadata: metadata).filter(platform, os: currentOS); | 135 |
| 127 }); | 136 controller.add(suite |
| 128 })).then((suites) => suites.where((suite) => suite != null).toList()); | 137 .change(metadata: metadata).filter(platform, os: currentOS)); |
| 138 }).catchError(controller.addError); |
| 139 }).then((_) => controller.close()); |
| 140 |
| 141 return controller.stream; |
| 129 } | 142 } |
| 130 | 143 |
| 131 /// Load the test suite at [path] in a browser. | 144 /// Load the test suite at [path] in a browser. |
| 132 Future<Suite> _loadBrowserFile(String path, TestPlatform platform) => | 145 Future<Suite> _loadBrowserFile(String path, TestPlatform platform) => |
| 133 _browserServer.then((browserServer) => | 146 _browserServer.then((browserServer) => |
| 134 browserServer.loadSuite(path, platform)); | 147 browserServer.loadSuite(path, platform)); |
| 135 | 148 |
| 136 /// Load the test suite at [path] in VM isolate. | 149 /// Load the test suite at [path] in VM isolate. |
| 137 Future<Suite> _loadVmFile(String path) { | 150 Future<Suite> _loadVmFile(String path) { |
| 138 var packageRoot = packageRootFor(path, _packageRoot); | 151 var packageRoot = packageRootFor(path, _packageRoot); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 Future close() { | 215 Future close() { |
| 203 for (var isolate in _isolates) { | 216 for (var isolate in _isolates) { |
| 204 isolate.kill(); | 217 isolate.kill(); |
| 205 } | 218 } |
| 206 _isolates.clear(); | 219 _isolates.clear(); |
| 207 | 220 |
| 208 if (_browserServerCompleter == null) return new Future.value(); | 221 if (_browserServerCompleter == null) return new Future.value(); |
| 209 return _browserServer.then((browserServer) => browserServer.close()); | 222 return _browserServer.then((browserServer) => browserServer.close()); |
| 210 } | 223 } |
| 211 } | 224 } |
| OLD | NEW |