| 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 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/runner/reporter/compact.dart'; | 14 import 'package:unittest/src/runner/reporter/compact.dart'; |
| 15 import 'package:unittest/src/runner/test_platform.dart'; |
| 15 import 'package:unittest/src/runner/load_exception.dart'; | 16 import 'package:unittest/src/runner/load_exception.dart'; |
| 16 import 'package:unittest/src/runner/loader.dart'; | 17 import 'package:unittest/src/runner/loader.dart'; |
| 17 import 'package:unittest/src/util/exit_codes.dart' as exit_codes; | 18 import 'package:unittest/src/util/exit_codes.dart' as exit_codes; |
| 18 import 'package:unittest/src/util/io.dart'; | 19 import 'package:unittest/src/util/io.dart'; |
| 19 import 'package:unittest/src/utils.dart'; | 20 import 'package:unittest/src/utils.dart'; |
| 20 | 21 |
| 21 /// The argument parser used to parse the executable arguments. | 22 /// The argument parser used to parse the executable arguments. |
| 22 final _parser = new ArgParser(); | 23 final _parser = new ArgParser(allowTrailingOptions: true); |
| 23 | 24 |
| 24 void main(List<String> args) { | 25 void main(List<String> args) { |
| 25 _parser.addFlag("help", abbr: "h", negatable: false, | 26 _parser.addFlag("help", abbr: "h", negatable: false, |
| 26 help: "Shows this usage information."); | 27 help: "Shows this usage information."); |
| 27 _parser.addOption("package-root", hide: true); | 28 _parser.addOption("package-root", hide: true); |
| 29 _parser.addOption("platform", |
| 30 abbr: 'p', |
| 31 help: 'The platform(s) on which to run the tests.', |
| 32 allowed: TestPlatform.all.map((platform) => platform.identifier).toList(), |
| 33 defaultsTo: 'vm', |
| 34 allowMultiple: true); |
| 28 _parser.addFlag("color", defaultsTo: null, | 35 _parser.addFlag("color", defaultsTo: null, |
| 29 help: 'Whether to use terminal colors.\n(auto-detected by default)'); | 36 help: 'Whether to use terminal colors.\n(auto-detected by default)'); |
| 30 | 37 |
| 31 var options; | 38 var options; |
| 32 try { | 39 try { |
| 33 options = _parser.parse(args); | 40 options = _parser.parse(args); |
| 34 } on FormatException catch (error) { | 41 } on FormatException catch (error) { |
| 35 _printUsage(error.message); | 42 _printUsage(error.message); |
| 36 exitCode = exit_codes.usage; | 43 exitCode = exit_codes.usage; |
| 37 return; | 44 return; |
| 38 } | 45 } |
| 39 | 46 |
| 40 if (options["help"]) { | 47 if (options["help"]) { |
| 41 _printUsage(); | 48 _printUsage(); |
| 42 return; | 49 return; |
| 43 } | 50 } |
| 44 | 51 |
| 45 var loader = new Loader(packageRoot: options["package-root"]); | 52 var color = options["color"]; |
| 53 if (color == null) color = canUseSpecialChars; |
| 54 |
| 55 var platforms = options["platform"].map(TestPlatform.find); |
| 56 var loader = new Loader(platforms, |
| 57 packageRoot: options["package-root"], color: color); |
| 46 new Future.sync(() { | 58 new Future.sync(() { |
| 47 var paths = options.rest; | 59 var paths = options.rest; |
| 48 if (paths.isEmpty) { | 60 if (paths.isEmpty) { |
| 49 if (!new Directory("test").existsSync()) { | 61 if (!new Directory("test").existsSync()) { |
| 50 throw new LoadException("test", | 62 throw new LoadException("test", |
| 51 "No test files were passed and the default directory doesn't " | 63 "No test files were passed and the default directory doesn't " |
| 52 "exist."); | 64 "exist."); |
| 53 } | 65 } |
| 54 paths = ["test"]; | 66 paths = ["test"]; |
| 55 } | 67 } |
| 56 | 68 |
| 57 return Future.wait(paths.map((path) { | 69 return Future.wait(paths.map((path) { |
| 58 if (new Directory(path).existsSync()) return loader.loadDir(path); | 70 if (new Directory(path).existsSync()) return loader.loadDir(path); |
| 59 if (new File(path).existsSync()) return loader.loadFile(path); | 71 if (new File(path).existsSync()) return loader.loadFile(path); |
| 60 throw new LoadException(path, 'Does not exist.'); | 72 throw new LoadException(path, 'Does not exist.'); |
| 61 })); | 73 })); |
| 62 }).then((suites) { | 74 }).then((suites) { |
| 63 var color = options["color"]; | |
| 64 if (color == null) color = canUseSpecialChars; | |
| 65 var reporter = new CompactReporter(flatten(suites), color: color); | 75 var reporter = new CompactReporter(flatten(suites), color: color); |
| 66 return reporter.run().then((success) { | 76 return reporter.run().then((success) { |
| 67 exitCode = success ? 0 : 1; | 77 exitCode = success ? 0 : 1; |
| 68 }).whenComplete(() => reporter.close()); | 78 }).whenComplete(() => reporter.close()); |
| 69 }).catchError((error, stackTrace) { | 79 }).catchError((error, stackTrace) { |
| 70 if (error is LoadException) { | 80 if (error is LoadException) { |
| 71 // TODO(nweiz): color this message? | 81 // TODO(nweiz): color this message? |
| 72 stderr.writeln(getErrorMessage(error)); | 82 stderr.writeln(getErrorMessage(error)); |
| 73 | 83 |
| 74 // Only print stack traces for load errors that come from the user's | 84 // Only print stack traces for load errors that come from the user's |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 output = stderr; | 122 output = stderr; |
| 113 } | 123 } |
| 114 | 124 |
| 115 output.write("""$message | 125 output.write("""$message |
| 116 | 126 |
| 117 Usage: pub run unittest:unittest [files or directories...] | 127 Usage: pub run unittest:unittest [files or directories...] |
| 118 | 128 |
| 119 ${_parser.usage} | 129 ${_parser.usage} |
| 120 """); | 130 """); |
| 121 } | 131 } |
| OLD | NEW |