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.configuration; | 5 library test.runner.configuration; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
9 | 9 |
10 import 'package:args/args.dart'; | 10 import 'package:args/args.dart'; |
11 import 'package:path/path.dart' as p; | 11 import 'package:path/path.dart' as p; |
12 | 12 |
| 13 import '../frontend/timeout.dart'; |
13 import '../backend/test_platform.dart'; | 14 import '../backend/test_platform.dart'; |
14 import '../util/io.dart'; | 15 import '../util/io.dart'; |
15 | 16 |
16 /// The default number of test suites to run at once. | 17 /// The default number of test suites to run at once. |
17 /// | 18 /// |
18 /// This defaults to half the available processors, since presumably some of | 19 /// This defaults to half the available processors, since presumably some of |
19 /// them will be used for the OS and other processes. | 20 /// them will be used for the OS and other processes. |
20 final _defaultConcurrency = math.max(1, Platform.numberOfProcessors ~/ 2); | 21 final _defaultConcurrency = math.max(1, Platform.numberOfProcessors ~/ 2); |
21 | 22 |
22 /// A class that encapsulates the command-line configuration of the test runner. | 23 /// A class that encapsulates the command-line configuration of the test runner. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 help: 'Whether to emit raw JavaScript stack traces for browser tests.'); | 77 help: 'Whether to emit raw JavaScript stack traces for browser tests.'); |
77 parser.addFlag("color", defaultsTo: null, | 78 parser.addFlag("color", defaultsTo: null, |
78 help: 'Whether to use terminal colors.\n(auto-detected by default)'); | 79 help: 'Whether to use terminal colors.\n(auto-detected by default)'); |
79 | 80 |
80 return parser; | 81 return parser; |
81 })(); | 82 })(); |
82 | 83 |
83 /// The usage string for the command-line arguments. | 84 /// The usage string for the command-line arguments. |
84 static String get usage => _parser.usage; | 85 static String get usage => _parser.usage; |
85 | 86 |
86 /// The results of parsing the arguments. | |
87 final ArgResults _options; | |
88 | |
89 /// Whether `--help` was passed. | 87 /// Whether `--help` was passed. |
90 bool get help => _options['help']; | 88 final bool help; |
91 | 89 |
92 /// Whether `--version` was passed. | 90 /// Whether `--version` was passed. |
93 bool get version => _options['version']; | 91 final bool version; |
94 | 92 |
95 /// Whether stack traces should be presented as-is or folded to remove | 93 /// Whether stack traces should be presented as-is or folded to remove |
96 /// irrelevant packages. | 94 /// irrelevant packages. |
97 bool get verboseTrace => _options['verbose-trace']; | 95 final bool verboseTrace; |
98 | 96 |
99 /// Whether JavaScript stack traces should be left as-is or converted to | 97 /// Whether JavaScript stack traces should be left as-is or converted to |
100 /// Dart-like traces. | 98 /// Dart-like traces. |
101 bool get jsTrace => _options['js-trace']; | 99 final bool jsTrace; |
102 | 100 |
103 /// Whether to pause for debugging after loading each test suite. | 101 /// Whether to pause for debugging after loading each test suite. |
104 bool get pauseAfterLoad => _options['pause-after-load']; | 102 final bool pauseAfterLoad; |
105 | 103 |
106 /// The package root for resolving "package:" URLs. | 104 /// The package root for resolving "package:" URLs. |
107 String get packageRoot => _options['package-root'] == null | 105 final String packageRoot; |
108 ? p.join(p.current, 'packages') | |
109 : _options['package-root']; | |
110 | 106 |
111 /// The name of the reporter to use to display results. | 107 /// The name of the reporter to use to display results. |
112 String get reporter => _options['reporter']; | 108 final String reporter; |
113 | 109 |
114 /// The URL for the `pub serve` instance from which to load tests, or `null` | 110 /// The URL for the `pub serve` instance from which to load tests, or `null` |
115 /// if tests should be loaded from the filesystem. | 111 /// if tests should be loaded from the filesystem. |
116 Uri get pubServeUrl { | 112 final Uri pubServeUrl; |
117 if (_options['pub-serve'] == null) return null; | |
118 return Uri.parse("http://localhost:${_options['pub-serve']}"); | |
119 } | |
120 | 113 |
121 /// Whether to use command-line color escapes. | 114 /// Whether to use command-line color escapes. |
122 bool get color => | 115 final bool color; |
123 _options["color"] == null ? canUseSpecialChars : _options["color"]; | |
124 | 116 |
125 /// How many tests to run concurrently. | 117 /// How many tests to run concurrently. |
126 int get concurrency => _concurrency; | 118 final int concurrency; |
127 int _concurrency; | |
128 | 119 |
129 /// The from which to load tests. | 120 /// The from which to load tests. |
130 List<String> get paths => _options.rest.isEmpty ? ["test"] : _options.rest; | 121 final List<String> paths; |
131 | 122 |
132 /// Whether the load paths were passed explicitly or the default was used. | 123 /// Whether the load paths were passed explicitly or the default was used. |
133 bool get explicitPaths => _options.rest.isNotEmpty; | 124 final bool explicitPaths; |
134 | 125 |
135 /// The pattern to match against test names to decide which to run, or `null` | 126 /// The pattern to match against test names to decide which to run, or `null` |
136 /// if all tests should be run. | 127 /// if all tests should be run. |
137 Pattern get pattern { | 128 final Pattern pattern; |
138 if (_options["name"] != null) { | |
139 return new RegExp(_options["name"]); | |
140 } else if (_options["plain-name"] != null) { | |
141 return _options["plain-name"]; | |
142 } else { | |
143 return null; | |
144 } | |
145 } | |
146 | 129 |
147 /// The set of platforms on which to run tests. | 130 /// The set of platforms on which to run tests. |
148 List<TestPlatform> get platforms => | 131 final List<TestPlatform> platforms; |
149 _options["platform"].map(TestPlatform.find).toList(); | |
150 | 132 |
151 /// Parses the configuration from [args]. | 133 /// Parses the configuration from [args]. |
152 /// | 134 /// |
153 /// Throws a [FormatException] if [args] are invalid. | 135 /// Throws a [FormatException] if [args] are invalid. |
154 Configuration.parse(List<String> args) | 136 factory Configuration.parse(List<String> args) { |
155 : _options = _parser.parse(args) { | 137 var options = _parser.parse(args); |
156 if (pauseAfterLoad) { | 138 |
157 _concurrency = 1; | 139 var pattern; |
158 } else if (_options['concurrency'] == null) { | 140 if (options['name'] != null) { |
159 _concurrency = _defaultConcurrency; | 141 if (options["plain-name"] != null) { |
160 } else { | 142 throw new FormatException( |
161 _concurrency = _wrapFormatException('concurrency', int.parse); | 143 "--name and --plain-name may not both be passed."); |
| 144 } |
| 145 |
| 146 pattern = _wrapFormatException( |
| 147 options, 'name', (value) => new RegExp(value)); |
| 148 } else if (options['plain-name'] != null) { |
| 149 pattern = options['plain-name']; |
162 } | 150 } |
163 | 151 |
164 if (_options["name"] != null && _options["plain-name"] != null) { | 152 return new Configuration( |
165 throw new FormatException( | 153 help: options['help'], |
166 "--name and --plain-name may not both be passed."); | 154 version: options['version'], |
167 } | 155 verboseTrace: options['verbose-trace'], |
| 156 jsTrace: options['js-trace'], |
| 157 pauseAfterLoad: options['pause-after-load'], |
| 158 color: options['color'], |
| 159 packageRoot: options['package-root'], |
| 160 reporter: options['reporter'], |
| 161 pubServePort: _wrapFormatException(options, 'pub-serve', int.parse), |
| 162 concurrency: _wrapFormatException(options, 'concurrency', int.parse, |
| 163 orElse: () => _defaultConcurrency), |
| 164 pattern: pattern, |
| 165 platforms: options['platforms'].map(TestPlatform.find).toList(), |
| 166 paths: options.rest.isEmpty ? null : options.rest); |
168 } | 167 } |
169 | 168 |
170 /// Runs [parse] on the value of the option [name], and wraps any | 169 /// Runs [parse] on the value of the option [name], and wraps any |
171 /// [FormatException] it throws with additional information. | 170 /// [FormatException] it throws with additional information. |
172 _wrapFormatException(String name, parse(value)) { | 171 static _wrapFormatException(ArgResults options, String name, parse(value), |
| 172 {orElse()}) { |
| 173 var value = options[name]; |
| 174 if (value == null) return orElse == null ? null : orElse(); |
| 175 |
173 try { | 176 try { |
174 return parse(_options[name]); | 177 return parse(value); |
175 } on FormatException catch (error) { | 178 } on FormatException catch (error) { |
176 throw new FormatException('Couldn\'t parse --$name "${_options[name]}": ' | 179 throw new FormatException('Couldn\'t parse --$name "${options[name]}": ' |
177 '${error.message}'); | 180 '${error.message}'); |
178 } | 181 } |
179 } | 182 } |
| 183 |
| 184 Configuration({this.help: false, this.version: false, |
| 185 this.verboseTrace: false, this.jsTrace: false, |
| 186 bool pauseAfterLoad: false, bool color, String packageRoot, |
| 187 String reporter, int pubServePort, int concurrency, this.pattern, |
| 188 Iterable<TestPlatform> platforms, Iterable<String> paths}) |
| 189 : pauseAfterLoad = pauseAfterLoad, |
| 190 color = color == null ? canUseSpecialChars : color, |
| 191 packageRoot = packageRoot == null |
| 192 ? p.join(p.current, 'packages') |
| 193 : packageRoot, |
| 194 reporter = reporter == null ? 'compact' : reporter, |
| 195 pubServeUrl = pubServePort == null |
| 196 ? null |
| 197 : Uri.parse("http://localhost:$pubServePort"), |
| 198 concurrency = pauseAfterLoad |
| 199 ? 1 |
| 200 : (concurrency == null ? _defaultConcurrency : concurrency), |
| 201 platforms = platforms.toList(), |
| 202 paths = paths == null ? ["test"] : paths.toList(), |
| 203 explicitPaths = paths != null; |
180 } | 204 } |
OLD | NEW |