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 // TODO(nweiz): This is under lib so that it can be used by the unittest dummy | 5 // TODO(nweiz): This is under lib so that it can be used by the unittest dummy |
6 // package. Once that package is no longer being updated, move this back into | 6 // package. Once that package is no longer being updated, move this back into |
7 // bin. | 7 // bin. |
8 library test.executable; | 8 library test.executable; |
9 | 9 |
10 import 'dart:async'; | 10 import 'dart:async'; |
11 import 'dart:io'; | 11 import 'dart:io'; |
12 import 'dart:isolate'; | 12 import 'dart:isolate'; |
13 | 13 |
14 import 'package:args/args.dart'; | 14 import 'package:args/args.dart'; |
15 import 'package:stack_trace/stack_trace.dart'; | 15 import 'package:stack_trace/stack_trace.dart'; |
| 16 import 'package:yaml/yaml.dart'; |
16 | 17 |
17 import 'backend/test_platform.dart'; | 18 import 'backend/test_platform.dart'; |
18 import 'runner/reporter/compact.dart'; | 19 import 'runner/reporter/compact.dart'; |
19 import 'runner/load_exception.dart'; | 20 import 'runner/load_exception.dart'; |
20 import 'runner/loader.dart'; | 21 import 'runner/loader.dart'; |
21 import 'util/exit_codes.dart' as exit_codes; | 22 import 'util/exit_codes.dart' as exit_codes; |
22 import 'util/io.dart'; | 23 import 'util/io.dart'; |
23 import 'utils.dart'; | 24 import 'utils.dart'; |
24 | 25 |
25 /// The argument parser used to parse the executable arguments. | 26 /// The argument parser used to parse the executable arguments. |
26 final _parser = new ArgParser(allowTrailingOptions: true); | 27 final _parser = new ArgParser(allowTrailingOptions: true); |
27 | 28 |
| 29 /// Returns whether the current package has a pubspec which uses the |
| 30 /// `test/pub_serve` transformer. |
| 31 bool get _usesTransformer { |
| 32 if (!new File('pubspec.yaml').existsSync()) return false; |
| 33 var contents = new File('pubspec.yaml').readAsStringSync(); |
| 34 |
| 35 var yaml; |
| 36 try { |
| 37 yaml = loadYaml(contents); |
| 38 } on FormatException { |
| 39 return false; |
| 40 } |
| 41 |
| 42 if (yaml is! Map) return false; |
| 43 |
| 44 var transformers = yaml['transformers']; |
| 45 if (transformers == null) return false; |
| 46 if (transformers is! List) return false; |
| 47 |
| 48 return transformers.any((transformer) { |
| 49 if (transformer is String) return transformer == 'test/pub_serve'; |
| 50 if (transformer is! Map) return false; |
| 51 if (transformer.keys.length != 1) return false; |
| 52 return transformer.keys.single == 'test/pub_serve'; |
| 53 }); |
| 54 } |
| 55 |
28 void main(List<String> args) { | 56 void main(List<String> args) { |
29 _parser.addFlag("help", abbr: "h", negatable: false, | 57 _parser.addFlag("help", abbr: "h", negatable: false, |
30 help: "Shows this usage information."); | 58 help: "Shows this usage information."); |
31 _parser.addOption("package-root", hide: true); | 59 _parser.addOption("package-root", hide: true); |
32 _parser.addOption("name", | 60 _parser.addOption("name", |
33 abbr: 'n', | 61 abbr: 'n', |
34 help: 'A substring of the name of the test to run.\n' | 62 help: 'A substring of the name of the test to run.\n' |
35 'Regular expression syntax is supported.'); | 63 'Regular expression syntax is supported.'); |
36 _parser.addOption("plain-name", | 64 _parser.addOption("plain-name", |
37 abbr: 'N', | 65 abbr: 'N', |
38 help: 'A plain-text substring of the name of the test to run.'); | 66 help: 'A plain-text substring of the name of the test to run.'); |
39 _parser.addOption("platform", | 67 _parser.addOption("platform", |
40 abbr: 'p', | 68 abbr: 'p', |
41 help: 'The platform(s) on which to run the tests.', | 69 help: 'The platform(s) on which to run the tests.', |
42 allowed: TestPlatform.all.map((platform) => platform.identifier).toList(), | 70 allowed: TestPlatform.all.map((platform) => platform.identifier).toList(), |
43 defaultsTo: 'vm', | 71 defaultsTo: 'vm', |
44 allowMultiple: true); | 72 allowMultiple: true); |
| 73 _parser.addOption("pub-serve", |
| 74 help: 'The port of a pub serve instance serving "test/".', |
| 75 hide: !supportsPubServe, |
| 76 valueHelp: 'port'); |
45 _parser.addFlag("color", defaultsTo: null, | 77 _parser.addFlag("color", defaultsTo: null, |
46 help: 'Whether to use terminal colors.\n(auto-detected by default)'); | 78 help: 'Whether to use terminal colors.\n(auto-detected by default)'); |
47 | 79 |
48 var options; | 80 var options; |
49 try { | 81 try { |
50 options = _parser.parse(args); | 82 options = _parser.parse(args); |
51 } on FormatException catch (error) { | 83 } on FormatException catch (error) { |
52 _printUsage(error.message); | 84 _printUsage(error.message); |
53 exitCode = exit_codes.usage; | 85 exitCode = exit_codes.usage; |
54 return; | 86 return; |
55 } | 87 } |
56 | 88 |
57 if (options["help"]) { | 89 if (options["help"]) { |
58 _printUsage(); | 90 _printUsage(); |
59 return; | 91 return; |
60 } | 92 } |
61 | 93 |
62 var color = options["color"]; | 94 var color = options["color"]; |
63 if (color == null) color = canUseSpecialChars; | 95 if (color == null) color = canUseSpecialChars; |
64 | 96 |
| 97 var pubServeUrl; |
| 98 if (options["pub-serve"] != null) { |
| 99 pubServeUrl = Uri.parse("http://localhost:${options['pub-serve']}"); |
| 100 if (!_usesTransformer) { |
| 101 stderr.write(''' |
| 102 When using --pub-serve, you must include the "test/pub_serve" transformer in |
| 103 your pubspec: |
| 104 |
| 105 transformers: |
| 106 - test/pub_serve: |
| 107 \$include: test/**_test.dart |
| 108 '''); |
| 109 exitCode = exit_codes.data; |
| 110 return; |
| 111 } |
| 112 } |
| 113 |
65 var platforms = options["platform"].map(TestPlatform.find); | 114 var platforms = options["platform"].map(TestPlatform.find); |
66 var loader = new Loader(platforms, | 115 var loader = new Loader(platforms, |
67 packageRoot: options["package-root"], color: color); | 116 pubServeUrl: pubServeUrl, |
| 117 packageRoot: options["package-root"], |
| 118 color: color); |
| 119 |
68 new Future.sync(() { | 120 new Future.sync(() { |
69 var paths = options.rest; | 121 var paths = options.rest; |
70 if (paths.isEmpty) { | 122 if (paths.isEmpty) { |
71 if (!new Directory("test").existsSync()) { | 123 if (!new Directory("test").existsSync()) { |
72 throw new LoadException("test", | 124 throw new LoadException("test", |
73 "No test files were passed and the default directory doesn't " | 125 "No test files were passed and the default directory doesn't " |
74 "exist."); | 126 "exist."); |
75 } | 127 } |
76 paths = ["test"]; | 128 paths = ["test"]; |
77 } | 129 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 output = stderr; | 217 output = stderr; |
166 } | 218 } |
167 | 219 |
168 output.write("""$message | 220 output.write("""$message |
169 | 221 |
170 Usage: pub run test:test [files or directories...] | 222 Usage: pub run test:test [files or directories...] |
171 | 223 |
172 ${_parser.usage} | 224 ${_parser.usage} |
173 """); | 225 """); |
174 } | 226 } |
OLD | NEW |