OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 library testing.run_tests; | 5 library testing.run_tests; |
6 | 6 |
7 import 'dart:async' show | 7 import 'dart:async' show Future; |
8 Future; | |
9 | 8 |
10 import 'dart:io' show | 9 import 'dart:io' show Directory, File, FileSystemEntity; |
11 Directory, | |
12 File, | |
13 FileSystemEntity; | |
14 | 10 |
15 import 'dart:io' as io show | 11 import 'dart:io' as io show exitCode; |
16 exitCode; | |
17 | 12 |
18 import 'dart:isolate' show | 13 import 'dart:isolate' show Isolate; |
19 Isolate; | |
20 | 14 |
21 import 'error_handling.dart' show | 15 import 'error_handling.dart' show withErrorHandling; |
22 withErrorHandling; | |
23 | 16 |
24 import 'test_root.dart' show | 17 import 'test_root.dart' show TestRoot; |
25 TestRoot; | |
26 | 18 |
27 import 'zone_helper.dart' show | 19 import 'zone_helper.dart' show runGuarded; |
28 runGuarded; | |
29 | 20 |
30 import 'log.dart' show | 21 import 'log.dart' |
31 enableVerboseOutput, | 22 show |
32 isVerbose, | 23 enableVerboseOutput, |
33 logMessage, | 24 isVerbose, |
34 logSuiteComplete, | 25 logMessage, |
35 logTestComplete; | 26 logSuiteComplete, |
| 27 logTestComplete; |
36 | 28 |
37 import 'run.dart' show | 29 import 'run.dart' show SuiteRunner, runProgram; |
38 SuiteRunner, | |
39 runProgram; | |
40 | 30 |
41 class CommandLine { | 31 class CommandLine { |
42 final Set<String> options; | 32 final Set<String> options; |
43 final List<String> arguments; | 33 final List<String> arguments; |
44 | 34 |
45 CommandLine(this.options, this.arguments); | 35 CommandLine(this.options, this.arguments); |
46 | 36 |
47 bool get verbose => options.contains("--verbose") || options.contains("-v"); | 37 bool get verbose => options.contains("--verbose") || options.contains("-v"); |
48 | 38 |
49 Set<String> get skip => commaSeparated("--skip="); | 39 Set<String> get skip => commaSeparated("--skip="); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 return fail("Only one --config option is supported"); | 80 return fail("Only one --config option is supported"); |
91 } | 81 } |
92 String configurationPath; | 82 String configurationPath; |
93 if (configurationPaths.length == 1) { | 83 if (configurationPaths.length == 1) { |
94 configurationPath = configurationPaths.single; | 84 configurationPath = configurationPaths.single; |
95 } else { | 85 } else { |
96 configurationPath = "testing.json"; | 86 configurationPath = "testing.json"; |
97 if (!await new File(configurationPath).exists()) { | 87 if (!await new File(configurationPath).exists()) { |
98 Directory test = new Directory("test"); | 88 Directory test = new Directory("test"); |
99 if (await test.exists()) { | 89 if (await test.exists()) { |
100 List<FileSystemEntity> candiates = | 90 List<FileSystemEntity> candiates = await test |
101 await test.list(recursive: true, followLinks: false) | 91 .list(recursive: true, followLinks: false) |
102 .where((FileSystemEntity entity) { | 92 .where((FileSystemEntity entity) { |
103 return entity is File && | 93 return entity is File && entity.uri.path.endsWith("/testing.json"); |
104 entity.uri.path.endsWith("/testing.json"); | 94 }).toList(); |
105 }).toList(); | |
106 switch (candiates.length) { | 95 switch (candiates.length) { |
107 case 0: | 96 case 0: |
108 return fail("Couldn't locate: '$configurationPath'."); | 97 return fail("Couldn't locate: '$configurationPath'."); |
109 | 98 |
110 case 1: | 99 case 1: |
111 configurationPath = candiates.single.path; | 100 configurationPath = candiates.single.path; |
112 break; | 101 break; |
113 | 102 |
114 default: | 103 default: |
115 return fail( | 104 return fail( |
(...skipping 14 matching lines...) Expand all Loading... |
130 return configuration; | 119 return configuration; |
131 } | 120 } |
132 | 121 |
133 static CommandLine parse(List<String> arguments) { | 122 static CommandLine parse(List<String> arguments) { |
134 int index = arguments.indexOf("--"); | 123 int index = arguments.indexOf("--"); |
135 Set<String> options; | 124 Set<String> options; |
136 if (index != -1) { | 125 if (index != -1) { |
137 options = new Set<String>.from(arguments.getRange(0, index - 1)); | 126 options = new Set<String>.from(arguments.getRange(0, index - 1)); |
138 arguments = arguments.sublist(index + 1); | 127 arguments = arguments.sublist(index + 1); |
139 } else { | 128 } else { |
140 options = | 129 options = arguments.where((argument) => argument.startsWith("-")).toSet(); |
141 arguments.where((argument) => argument.startsWith("-")).toSet(); | |
142 arguments = | 130 arguments = |
143 arguments.where((argument) => !argument.startsWith("-")).toList(); | 131 arguments.where((argument) => !argument.startsWith("-")).toList(); |
144 } | 132 } |
145 return new CommandLine(options, arguments); | 133 return new CommandLine(options, arguments); |
146 } | 134 } |
147 } | 135 } |
148 | 136 |
149 fail(String message) { | 137 fail(String message) { |
150 print(message); | 138 print(message); |
151 io.exitCode = 1; | 139 io.exitCode = 1; |
152 return null; | 140 return null; |
153 } | 141 } |
154 | 142 |
155 main(List<String> arguments) => withErrorHandling(() async { | 143 main(List<String> arguments) => withErrorHandling(() async { |
156 CommandLine cl = CommandLine.parse(arguments); | 144 CommandLine cl = CommandLine.parse(arguments); |
157 if (cl.verbose) { | 145 if (cl.verbose) { |
158 enableVerboseOutput(); | 146 enableVerboseOutput(); |
159 } | 147 } |
160 Map<String, String> environment = cl.environment; | 148 Map<String, String> environment = cl.environment; |
161 Uri configuration = await cl.configuration; | 149 Uri configuration = await cl.configuration; |
162 if (configuration == null) return; | 150 if (configuration == null) return; |
163 if (!isVerbose) { | 151 if (!isVerbose) { |
164 print("Use --verbose to display more details."); | 152 print("Use --verbose to display more details."); |
165 } | 153 } |
166 TestRoot root = await TestRoot.fromUri(configuration); | 154 TestRoot root = await TestRoot.fromUri(configuration); |
167 SuiteRunner runner = new SuiteRunner(root.suites, environment, cl.selectors, | 155 SuiteRunner runner = new SuiteRunner( |
168 cl.selectedSuites, cl.skip); | 156 root.suites, environment, cl.selectors, cl.selectedSuites, cl.skip); |
169 String program = await runner.generateDartProgram(); | 157 String program = await runner.generateDartProgram(); |
170 bool hasAnalyzerSuites = await runner.analyze(root.packages); | 158 bool hasAnalyzerSuites = await runner.analyze(root.packages); |
171 Stopwatch sw = new Stopwatch()..start(); | 159 Stopwatch sw = new Stopwatch()..start(); |
172 if (program == null) { | 160 if (program == null) { |
173 if (!hasAnalyzerSuites) { | 161 if (!hasAnalyzerSuites) { |
174 fail("No tests configured."); | 162 fail("No tests configured."); |
175 } | 163 } |
176 } else { | 164 } else { |
177 await runProgram(program, root.packages); | 165 await runProgram(program, root.packages); |
178 } | 166 } |
179 print("Running tests took: ${sw.elapsed}."); | 167 print("Running tests took: ${sw.elapsed}."); |
180 }); | 168 }); |
181 | 169 |
182 Future<Null> runTests(Map<String, Function> tests) => | 170 Future<Null> runTests(Map<String, Function> tests) => |
183 withErrorHandling(() async { | 171 withErrorHandling(() async { |
184 int completed = 0; | 172 int completed = 0; |
185 for (String name in tests.keys) { | 173 for (String name in tests.keys) { |
186 StringBuffer sb = new StringBuffer(); | 174 StringBuffer sb = new StringBuffer(); |
187 try { | 175 try { |
188 await runGuarded(() { | 176 await runGuarded(() { |
189 print("Running test $name"); | 177 print("Running test $name"); |
190 return tests[name](); | 178 return tests[name](); |
191 }, printLineOnStdout: sb.writeln); | 179 }, printLineOnStdout: sb.writeln); |
192 logMessage(sb); | 180 logMessage(sb); |
193 } catch (e) { | 181 } catch (e) { |
194 print(sb); | 182 print(sb); |
195 rethrow; | 183 rethrow; |
196 } | 184 } |
197 logTestComplete(++completed, 0, tests.length, null, null); | 185 logTestComplete(++completed, 0, tests.length, null, null); |
198 } | 186 } |
199 logSuiteComplete(); | 187 logSuiteComplete(); |
200 }); | 188 }); |
OLD | NEW |