Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: bin/unittest.dart

Issue 1041503002: Allow tests to be selected by name. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: cr Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.');
33 _parser.addOption("plain-name",
34 abbr: 'N',
35 help: 'A plain-text substring of the name of the test to run.');
29 _parser.addOption("platform", 36 _parser.addOption("platform",
30 abbr: 'p', 37 abbr: 'p',
31 help: 'The platform(s) on which to run the tests.', 38 help: 'The platform(s) on which to run the tests.',
32 allowed: TestPlatform.all.map((platform) => platform.identifier).toList(), 39 allowed: TestPlatform.all.map((platform) => platform.identifier).toList(),
33 defaultsTo: 'vm', 40 defaultsTo: 'vm',
34 allowMultiple: true); 41 allowMultiple: true);
35 _parser.addFlag("color", defaultsTo: null, 42 _parser.addFlag("color", defaultsTo: null,
36 help: 'Whether to use terminal colors.\n(auto-detected by default)'); 43 help: 'Whether to use terminal colors.\n(auto-detected by default)');
37 44
38 var options; 45 var options;
(...skipping 26 matching lines...) Expand all
65 } 72 }
66 paths = ["test"]; 73 paths = ["test"];
67 } 74 }
68 75
69 return Future.wait(paths.map((path) { 76 return Future.wait(paths.map((path) {
70 if (new Directory(path).existsSync()) return loader.loadDir(path); 77 if (new Directory(path).existsSync()) return loader.loadDir(path);
71 if (new File(path).existsSync()) return loader.loadFile(path); 78 if (new File(path).existsSync()) return loader.loadFile(path);
72 throw new LoadException(path, 'Does not exist.'); 79 throw new LoadException(path, 'Does not exist.');
73 })); 80 }));
74 }).then((suites) { 81 }).then((suites) {
82 suites = flatten(suites);
83
84 var pattern;
85 if (options["name"] != null) {
86 if (options["plain-name"] != null) {
87 _printUsage("--name and --plain-name may not both be passed.");
88 exitCode = exit_codes.data;
89 return null;
90 }
91 pattern = new RegExp(options["name"]);
92 } else if (options["plain-name"] != null) {
93 pattern = options["plain-name"];
94 }
95
96 if (pattern != null) {
97 suites = suites.map((suite) {
98 return suite.change(
99 tests: suite.tests.where((test) => test.name.contains(pattern)));
100 }).toList();
101
102 if (suites.every((suite) => suite.tests.isEmpty)) {
103 stderr.write('No tests match ');
104
105 if (pattern is RegExp) {
106 stderr.write('regular expression "${pattern.pattern}".');
107 } else {
108 stderr.writeln('"$pattern".');
109 }
110 exitCode = exit_codes.data;
111 return null;
112 }
113 }
114
75 var reporter = new CompactReporter(flatten(suites), color: color); 115 var reporter = new CompactReporter(flatten(suites), color: color);
76 return reporter.run().then((success) { 116 return reporter.run().then((success) {
77 exitCode = success ? 0 : 1; 117 exitCode = success ? 0 : 1;
78 }).whenComplete(() => reporter.close()); 118 }).whenComplete(() => reporter.close());
79 }).catchError((error, stackTrace) { 119 }).catchError((error, stackTrace) {
80 if (error is LoadException) { 120 if (error is LoadException) {
81 stderr.writeln(error.toString(color: color)); 121 stderr.writeln(error.toString(color: color));
82 122
83 // Only print stack traces for load errors that come from the user's 123 // Only print stack traces for load errors that come from the user's
84 if (error.innerError is! IOException && 124 if (error.innerError is! IOException &&
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 output = stderr; 162 output = stderr;
123 } 163 }
124 164
125 output.write("""$message 165 output.write("""$message
126 166
127 Usage: pub run unittest:unittest [files or directories...] 167 Usage: pub run unittest:unittest [files or directories...]
128 168
129 ${_parser.usage} 169 ${_parser.usage}
130 """); 170 """);
131 } 171 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698