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 unittest.unittest; | 5 library unittest.unittest; |
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:args/args.dart'; | 11 import 'package:args/args.dart'; |
12 import 'package:stack_trace/stack_trace.dart'; | 12 import 'package:stack_trace/stack_trace.dart'; |
13 | 13 |
14 import 'package:unittest/src/backend/test_platform.dart'; | 14 import 'package:unittest/src/backend/test_platform.dart'; |
15 import 'package:unittest/src/runner/reporter/compact.dart'; | 15 import 'package:unittest/src/runner/reporter/compact.dart'; |
16 import 'package:unittest/src/runner/load_exception.dart'; | 16 import 'package:unittest/src/runner/load_exception.dart'; |
17 import 'package:unittest/src/runner/loader.dart'; | 17 import 'package:unittest/src/runner/loader.dart'; |
18 import 'package:unittest/src/util/exit_codes.dart' as exit_codes; | 18 import 'package:unittest/src/util/exit_codes.dart' as exit_codes; |
19 import 'package:unittest/src/util/io.dart'; | 19 import 'package:unittest/src/util/io.dart'; |
20 import 'package:unittest/src/utils.dart'; | 20 import 'package:unittest/src/utils.dart'; |
21 | 21 |
22 /// The argument parser used to parse the executable arguments. | 22 /// The argument parser used to parse the executable arguments. |
23 final _parser = new ArgParser(allowTrailingOptions: true); | 23 final _parser = new ArgParser(allowTrailingOptions: true); |
24 | 24 |
25 void main(List<String> args) { | 25 void main(List<String> args) { |
26 _parser.addFlag("help", abbr: "h", negatable: false, | 26 _parser.addFlag("help", abbr: "h", negatable: false, |
27 help: "Shows this usage information."); | 27 help: "Shows this usage information."); |
28 _parser.addOption("package-root", hide: true); | 28 _parser.addOption("package-root", hide: true); |
29 _parser.addOption("name", | |
30 abbr: 'n', | |
31 help: 'A substring of the name of the test to run.\n' | |
32 'Regular expression syntax is supported.'); | |
kevmoo
2015/03/28 22:02:26
In theory I like regex support, but since we allow
nweiz
2015/03/30 19:45:41
I personally hate it when I have to switch up my c
kevmoo
2015/03/30 19:51:49
I'm fine if plain text is opt in. But we should ab
nweiz
2015/03/30 20:08:09
Done.
| |
29 _parser.addOption("platform", | 33 _parser.addOption("platform", |
30 abbr: 'p', | 34 abbr: 'p', |
31 help: 'The platform(s) on which to run the tests.', | 35 help: 'The platform(s) on which to run the tests.', |
32 allowed: TestPlatform.all.map((platform) => platform.identifier).toList(), | 36 allowed: TestPlatform.all.map((platform) => platform.identifier).toList(), |
33 defaultsTo: 'vm', | 37 defaultsTo: 'vm', |
34 allowMultiple: true); | 38 allowMultiple: true); |
35 _parser.addFlag("color", defaultsTo: null, | 39 _parser.addFlag("color", defaultsTo: null, |
36 help: 'Whether to use terminal colors.\n(auto-detected by default)'); | 40 help: 'Whether to use terminal colors.\n(auto-detected by default)'); |
37 | 41 |
38 var options; | 42 var options; |
(...skipping 26 matching lines...) Expand all Loading... | |
65 } | 69 } |
66 paths = ["test"]; | 70 paths = ["test"]; |
67 } | 71 } |
68 | 72 |
69 return Future.wait(paths.map((path) { | 73 return Future.wait(paths.map((path) { |
70 if (new Directory(path).existsSync()) return loader.loadDir(path); | 74 if (new Directory(path).existsSync()) return loader.loadDir(path); |
71 if (new File(path).existsSync()) return loader.loadFile(path); | 75 if (new File(path).existsSync()) return loader.loadFile(path); |
72 throw new LoadException(path, 'Does not exist.'); | 76 throw new LoadException(path, 'Does not exist.'); |
73 })); | 77 })); |
74 }).then((suites) { | 78 }).then((suites) { |
79 suites = flatten(suites); | |
80 | |
81 if (options["name"] != null) { | |
82 var expression = new RegExp(options["name"]); | |
83 suites = suites.map((suite) { | |
84 return suite.change( | |
85 tests: suite.tests.where((test) => expression.hasMatch(test.name))); | |
86 }).toList(); | |
87 | |
88 if (suites.every((suite) => suite.tests.isEmpty)) { | |
89 stderr.writeln('No tests match "${options["name"]}".'); | |
90 exitCode = exit_codes.data; | |
91 return null; | |
92 } | |
93 } | |
94 | |
75 var reporter = new CompactReporter(flatten(suites), color: color); | 95 var reporter = new CompactReporter(flatten(suites), color: color); |
76 return reporter.run().then((success) { | 96 return reporter.run().then((success) { |
77 exitCode = success ? 0 : 1; | 97 exitCode = success ? 0 : 1; |
78 }).whenComplete(() => reporter.close()); | 98 }).whenComplete(() => reporter.close()); |
79 }).catchError((error, stackTrace) { | 99 }).catchError((error, stackTrace) { |
80 if (error is LoadException) { | 100 if (error is LoadException) { |
81 stderr.writeln(error.toString(color: color)); | 101 stderr.writeln(error.toString(color: color)); |
82 | 102 |
83 // Only print stack traces for load errors that come from the user's | 103 // Only print stack traces for load errors that come from the user's |
84 if (error.innerError is! IOException && | 104 if (error.innerError is! IOException && |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 output = stderr; | 142 output = stderr; |
123 } | 143 } |
124 | 144 |
125 output.write("""$message | 145 output.write("""$message |
126 | 146 |
127 Usage: pub run unittest:unittest [files or directories...] | 147 Usage: pub run unittest:unittest [files or directories...] |
128 | 148 |
129 ${_parser.usage} | 149 ${_parser.usage} |
130 """); | 150 """); |
131 } | 151 } |
OLD | NEW |