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 unittest.unittest; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:io'; | |
9 import 'dart:isolate'; | |
10 | |
11 import 'package:args/args.dart'; | |
12 import 'package:stack_trace/stack_trace.dart'; | |
13 import 'package:unittest/src/console_reporter.dart'; | |
14 import 'package:unittest/src/exit_codes.dart' as exit_codes; | |
15 import 'package:unittest/src/io.dart'; | |
kevmoo
2015/02/19 01:54:08
unused import
nweiz
2015/02/19 02:10:18
Done.
| |
16 import 'package:unittest/src/load_exception.dart'; | |
17 import 'package:unittest/src/loader.dart'; | |
18 import 'package:unittest/src/utils.dart'; | |
19 | |
20 /// The argument parser used to parse the executable arguments. | |
21 final _parser = new ArgParser(); | |
22 | |
23 void main(List<String> args) { | |
24 _parser.addFlag("help", abbr: "h", negatable: false, | |
kevmoo
2015/02/19 01:54:08
Just set these up in the field declaration. Use ca
nweiz
2015/02/19 02:10:18
Setting them up here has several advantages. It's
| |
25 help: "Shows this usage information."); | |
26 _parser.addOption("package-root", hide: true); | |
27 | |
28 var options; | |
29 try { | |
30 options = _parser.parse(args); | |
31 } on FormatException catch (error) { | |
32 _printUsage(error.message); | |
33 exitCode = exit_codes.usage; | |
34 return; | |
35 } | |
36 | |
37 if (options["help"]) { | |
38 _printUsage(); | |
39 return; | |
40 } | |
41 | |
42 var loader = new Loader(packageRoot: options["package-root"]); | |
43 new Future.sync(() { | |
44 var paths = options.rest; | |
kevmoo
2015/02/19 01:54:08
Could all of this be outside of the Future.sync ca
nweiz
2015/02/19 02:10:18
We need the [LoadException] to be handled by the [
| |
45 if (paths.isEmpty) { | |
46 if (!new Directory("test").existsSync()) { | |
47 throw new LoadException("test", | |
48 "No test files were passed and the default directory doesn't " | |
49 "exist."); | |
50 } | |
51 paths = ["test"]; | |
52 } | |
53 | |
54 return Future.wait(paths.map((path) { | |
kevmoo
2015/02/19 01:54:08
Could this be huge? Do we want to "pool" this to s
nweiz
2015/02/19 02:10:18
We don't run them all at once, and loading them in
| |
55 if (new Directory(path).existsSync()) return loader.loadDir(path); | |
56 if (new File(path).existsSync()) return loader.loadFile(path); | |
57 throw new LoadException(path, 'Does not exist.'); | |
58 })); | |
59 }).then((suites) { | |
60 var reporter = new ConsoleReporter(flatten(suites)); | |
61 return reporter.run().then((success) { | |
62 exitCode = success ? 0 : 1; | |
63 }).whenComplete(() => reporter.close()); | |
64 }).catchError((error, stackTrace) { | |
65 if (error is LoadException) { | |
66 // TODO(nweiz): color this message? | |
67 stderr.writeln(getErrorMessage(error)); | |
68 | |
69 // Only print stack traces for load errors that come from the user's | |
70 if (error.innerError is! IOException && | |
71 error.innerError is! IsolateSpawnException && | |
72 error.innerError is! String) { | |
73 // TODO(nweiz): filter out runInIsolate and package:unittest stuff. | |
74 stderr.write(terseChain(stackTrace)); | |
75 } | |
76 | |
77 exitCode = error.innerError is IOException | |
78 ? exit_codes.io | |
79 : exit_codes.data; | |
80 } else { | |
81 stderr.writeln(getErrorMessage(error)); | |
82 stderr.writeln(new Trace.from(stackTrace).terse); | |
83 stderr.writeln( | |
84 "This is an unexpected error. Please file an issue at " | |
85 "http://github.com/dart-lang/unittest\n" | |
86 "with the stack trace and instructions for reproducing the error."); | |
87 exitCode = exit_codes.software; | |
88 } | |
89 }).whenComplete(() => loader.close()); | |
90 } | |
91 | |
92 /// Print usage information for this command. | |
93 /// | |
94 /// If [error] is passed, it's used in place of the usage message and the whole | |
95 /// thing is printed to stderr instead of stdout. | |
96 void _printUsage([String error]) { | |
97 var output = stdout; | |
98 | |
99 var message = "Runs tests in this package."; | |
100 if (error != null) { | |
101 message = error; | |
102 output = stderr; | |
103 } | |
104 | |
105 output.write("""$message | |
106 | |
107 Usage: pub run unittest:unittest [files or directories...] | |
108 | |
109 ${_parser.usage} | |
110 """); | |
111 } | |
OLD | NEW |