|
| 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 | + |
| 14 | +import 'package:unittest/src/console_reporter.dart'; |
| 15 | +import 'package:unittest/src/exit_codes.dart' as exit_codes; |
| 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, |
| 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; |
| 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) { |
| 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 | + stderr.write(terseChain(stackTrace)); |
| 74 | + } |
| 75 | + |
| 76 | + exitCode = error.innerError is IOException |
| 77 | + ? exit_codes.io |
| 78 | + : exit_codes.data; |
| 79 | + } else { |
| 80 | + stderr.writeln(getErrorMessage(error)); |
| 81 | + stderr.writeln(new Trace.from(stackTrace).terse); |
| 82 | + stderr.writeln( |
| 83 | + "This is an unexpected error. Please file an issue at " |
| 84 | + "http://github.com/dart-lang/unittest\n" |
| 85 | + "with the stack trace and instructions for reproducing the error."); |
| 86 | + exitCode = exit_codes.software; |
| 87 | + } |
| 88 | + }).whenComplete(() => loader.close()); |
| 89 | +} |
| 90 | + |
| 91 | +/// Print usage information for this command. |
| 92 | +/// |
| 93 | +/// If [error] is passed, it's used in place of the usage message and the whole |
| 94 | +/// thing is printed to stderr instead of stdout. |
| 95 | +void _printUsage([String error]) { |
| 96 | + var output = stdout; |
| 97 | + |
| 98 | + var message = "Runs tests in this package."; |
| 99 | + if (error != null) { |
| 100 | + message = error; |
| 101 | + output = stderr; |
| 102 | + } |
| 103 | + |
| 104 | + output.write("""$message |
| 105 | +
|
| 106 | +Usage: pub run unittest:unittest [files or directories...] |
| 107 | +
|
| 108 | +${_parser.usage} |
| 109 | +"""); |
| 110 | +} |
0 commit comments