OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library test.runner.configuration; |
| 6 |
| 7 import 'dart:io'; |
| 8 import 'dart:math' as math; |
| 9 |
| 10 import 'package:args/args.dart'; |
| 11 import 'package:path/path.dart' as p; |
| 12 |
| 13 import '../backend/test_platform.dart'; |
| 14 import '../util/io.dart'; |
| 15 |
| 16 /// The default number of test suites to run at once. |
| 17 /// |
| 18 /// This defaults to half the available processors, since presumably some of |
| 19 /// them will be used for the OS and other processes. |
| 20 final _defaultConcurrency = math.max(1, Platform.numberOfProcessors ~/ 2); |
| 21 |
| 22 /// A class that encapsulates the command-line configuration of the test runner. |
| 23 class Configuration { |
| 24 /// The parser used to parse the command-line arguments. |
| 25 static final ArgParser _parser = (() { |
| 26 var parser = new ArgParser(allowTrailingOptions: true); |
| 27 |
| 28 var allPlatforms = TestPlatform.all.toList(); |
| 29 if (!Platform.isMacOS) allPlatforms.remove(TestPlatform.safari); |
| 30 if (!Platform.isWindows) allPlatforms.remove(TestPlatform.internetExplorer); |
| 31 |
| 32 parser.addFlag("help", abbr: "h", negatable: false, |
| 33 help: "Shows this usage information."); |
| 34 parser.addFlag("version", negatable: false, |
| 35 help: "Shows the package's version."); |
| 36 parser.addOption("package-root", hide: true); |
| 37 parser.addOption("name", |
| 38 abbr: 'n', |
| 39 help: 'A substring of the name of the test to run.\n' |
| 40 'Regular expression syntax is supported.'); |
| 41 parser.addOption("plain-name", |
| 42 abbr: 'N', |
| 43 help: 'A plain-text substring of the name of the test to run.'); |
| 44 parser.addOption("platform", |
| 45 abbr: 'p', |
| 46 help: 'The platform(s) on which to run the tests.', |
| 47 allowed: allPlatforms.map((platform) => platform.identifier).toList(), |
| 48 defaultsTo: 'vm', |
| 49 allowMultiple: true); |
| 50 parser.addOption("concurrency", |
| 51 abbr: 'j', |
| 52 help: 'The number of concurrent test suites run.\n' |
| 53 '(defaults to $_defaultConcurrency)', |
| 54 valueHelp: 'threads'); |
| 55 parser.addOption("pub-serve", |
| 56 help: 'The port of a pub serve instance serving "test/".', |
| 57 hide: !supportsPubServe, |
| 58 valueHelp: 'port'); |
| 59 parser.addOption("reporter", |
| 60 abbr: 'r', |
| 61 help: 'The runner used to print test results.', |
| 62 allowed: ['compact', 'expanded'], |
| 63 defaultsTo: Platform.isWindows ? 'expanded' : 'compact', |
| 64 allowedHelp: { |
| 65 'compact': 'A single line, updated continuously.', |
| 66 'expanded': 'A separate line for each update.' |
| 67 }); |
| 68 parser.addFlag("verbose-trace", negatable: false, |
| 69 help: 'Whether to emit stack traces with core library frames.'); |
| 70 parser.addFlag("js-trace", negatable: false, |
| 71 help: 'Whether to emit raw JavaScript stack traces for browser tests.'); |
| 72 parser.addFlag("color", defaultsTo: null, |
| 73 help: 'Whether to use terminal colors.\n(auto-detected by default)'); |
| 74 |
| 75 return parser; |
| 76 })(); |
| 77 |
| 78 /// The usage string for the command-line arguments. |
| 79 static String get usage => _parser.usage; |
| 80 |
| 81 /// The results of parsing the arguments. |
| 82 final ArgResults _options; |
| 83 |
| 84 /// Whether `--help` was passed. |
| 85 bool get help => _options['help']; |
| 86 |
| 87 /// Whether `--version` was passed. |
| 88 bool get version => _options['version']; |
| 89 |
| 90 /// Whether stack traces should be presented as-is or folded to remove |
| 91 /// irrelevant packages. |
| 92 bool get verboseTrace => _options['verbose-trace']; |
| 93 |
| 94 /// Whether JavaScript stack traces should be left as-is or converted to |
| 95 /// Dart-like traces. |
| 96 bool get jsTrace => _options['js-trace']; |
| 97 |
| 98 /// The package root for resolving "package:" URLs. |
| 99 String get packageRoot => _options['package-root'] == null |
| 100 ? p.join(p.current, 'packages') |
| 101 : _options['package-root']; |
| 102 |
| 103 /// The name of the reporter to use to display results. |
| 104 String get reporter => _options['reporter']; |
| 105 |
| 106 /// The URL for the `pub serve` instance from which to load tests, or `null` |
| 107 /// if tests should be loaded from the filesystem. |
| 108 Uri get pubServeUrl { |
| 109 if (_options['pub-serve'] == null) return null; |
| 110 return Uri.parse("http://localhost:${_options['pub-serve']}"); |
| 111 } |
| 112 |
| 113 /// Whether to use command-line color escapes. |
| 114 bool get color => |
| 115 _options["color"] == null ? canUseSpecialChars : _options["color"]; |
| 116 |
| 117 /// How many tests to run concurrently. |
| 118 int get concurrency => _concurrency; |
| 119 int _concurrency; |
| 120 |
| 121 /// The from which to load tests. |
| 122 List<String> get paths => _options.rest.isEmpty ? ["test"] : _options.rest; |
| 123 |
| 124 /// Whether the load paths were passed explicitly or the default was used. |
| 125 bool get explicitPaths => _options.rest.isNotEmpty; |
| 126 |
| 127 /// The pattern to match against test names to decide which to run, or `null` |
| 128 /// if all tests should be run. |
| 129 Pattern get pattern { |
| 130 if (_options["name"] != null) { |
| 131 return new RegExp(_options["name"]); |
| 132 } else if (_options["plain-name"] != null) { |
| 133 return _options["plain-name"]; |
| 134 } else { |
| 135 return null; |
| 136 } |
| 137 } |
| 138 |
| 139 /// The set of platforms on which to run tests. |
| 140 List<TestPlatform> get platforms => |
| 141 _options["platform"].map(TestPlatform.find).toList(); |
| 142 |
| 143 /// Parses the configuration from [args]. |
| 144 /// |
| 145 /// Throws a [FormatException] if [args] are invalid. |
| 146 Configuration.parse(List<String> args) |
| 147 : _options = _parser.parse(args) { |
| 148 _concurrency = _options['concurrency'] == null |
| 149 ? _defaultConcurrency |
| 150 : _wrapFormatException('concurrency', int.parse); |
| 151 |
| 152 if (_options["name"] != null && _options["plain-name"] != null) { |
| 153 throw new FormatException( |
| 154 "--name and --plain-name may not both be passed."); |
| 155 } |
| 156 } |
| 157 |
| 158 /// Runs [parse] on the value of the option [name], and wraps any |
| 159 /// [FormatException] it throws with additional information. |
| 160 _wrapFormatException(String name, parse(value)) { |
| 161 try { |
| 162 return parse(_options[name]); |
| 163 } on FormatException catch (error) { |
| 164 throw new FormatException('Couldn\'t parse --$name "${_options[name]}": ' |
| 165 '${error.message}'); |
| 166 } |
| 167 } |
| 168 } |
OLD | NEW |