Skip to content

Commit 533b58c

Browse files
committed
Add a test runner executable.
This is still extremely bare-bones and won't work with tests that actually import "package:unittest/unittest.dart", but it's something. [email protected] See #2 Review URL: https://codereview.chromium.org//933083002
1 parent b9b6002 commit 533b58c

18 files changed

Lines changed: 867 additions & 53 deletions

‎.status‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ lib/*/*/*/*: SkipByDesign
2626
# dart:io-specific tests.
2727
[ $browser ]
2828
test/loader_test: SkipByDesign
29+
test/runner_test: SkipByDesign
2930
test/vm_listener_test: SkipByDesign
3031

3132
[ $runtime == safari ]

‎bin/unittest.dart‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}

‎lib/src/console_reporter.dart‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ class ConsoleReporter {
4949
/// The set of tests that have completed and been marked as failing or error.
5050
final _failed = new Set<LiveTest>();
5151

52+
/// The size of [_passed] last time a progress notification was printed.
53+
int _lastProgressPassed;
54+
55+
/// The size of [_failed] last time a progress notification was printed.
56+
int _lastProgressFailed;
57+
58+
/// The message printed for the last progress notification.
59+
String _lastProgressMessage;
60+
5261
/// Creates a [ConsoleReporter] that will run all tests in [suites].
5362
ConsoleReporter(Iterable<Suite> suites)
5463
: _multipleSuites = suites.length > 1,
@@ -70,11 +79,10 @@ class ConsoleReporter {
7079
liveTest.onError.listen((error) {
7180
if (liveTest.state.status != Status.complete) return;
7281

73-
// TODO(nweiz): don't re-print the progress line if a test has multiple
74-
// errors in a row.
7582
_progressLine(_description(liveTest));
7683
print('');
77-
print(indent("${error.error}\n${error.stackTrace}"));
84+
print(indent(error.error.toString()));
85+
print(indent(terseChain(error.stackTrace).toString()));
7886
});
7987
});
8088
}
@@ -89,11 +97,14 @@ class ConsoleReporter {
8997
"once.");
9098
}
9199

100+
if (_engine.liveTests.isEmpty) {
101+
print("No tests ran.");
102+
return new Future.value(true);
103+
}
104+
92105
_stopwatch.start();
93106
return _engine.run().then((success) {
94-
if (_engine.liveTests.isEmpty) {
95-
print("\nNo tests ran.");
96-
} else if (success) {
107+
if (success) {
97108
_progressLine("All tests passed!");
98109
print('');
99110
} else {
@@ -105,12 +116,27 @@ class ConsoleReporter {
105116
});
106117
}
107118

119+
/// Signals that the caller is done with any test output and the reporter
120+
/// should release any resources it has allocated.
121+
Future close() => _engine.close();
122+
108123
/// Prints a line representing the current state of the tests.
109124
///
110125
/// [message] goes after the progress report, and may be truncated to fit the
111126
/// entire line within [_lineLength]. If [color] is passed, it's used as the
112127
/// color for [message].
113128
void _progressLine(String message, {String color}) {
129+
// Print nothing if nothing has changed since the last progress line.
130+
if (_passed.length == _lastProgressPassed &&
131+
_failed.length == _lastProgressFailed &&
132+
message == _lastProgressMessage) {
133+
return;
134+
}
135+
136+
_lastProgressPassed = _passed.length;
137+
_lastProgressFailed = _failed.length;
138+
_lastProgressMessage = message;
139+
114140
if (color == null) color = '';
115141
var duration = _stopwatch.elapsed;
116142
var buffer = new StringBuffer();

‎lib/src/engine.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ class Engine {
6464
}).then((_) =>
6565
liveTests.every((liveTest) => liveTest.state.result == Result.success));
6666
}
67+
68+
/// Signals that the caller is done paying attention to test results and the
69+
/// engine should release any resources it has allocated.
70+
Future close() =>
71+
Future.wait(liveTests.map((liveTest) => liveTest.close()));
6772
}

‎lib/src/exit_codes.dart‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
/// Exit code constants.
6+
///
7+
/// From [the BSD sysexits manpage][manpage]. Not every constant here is used.
8+
///
9+
/// [manpage]: http://www.freebsd.org/cgi/man.cgi?query=sysexits
10+
library unittest.exit_codes;
11+
12+
/// The command completely successfully.
13+
const success = 0;
14+
15+
/// The command was used incorrectly.
16+
const usage = 64;
17+
18+
/// The input data was incorrect.
19+
const data = 65;
20+
21+
/// An input file did not exist or was unreadable.
22+
const noInput = 66;
23+
24+
/// The user specified did not exist.
25+
const noUser = 67;
26+
27+
/// The host specified did not exist.
28+
const noHost = 68;
29+
30+
/// A service is unavailable.
31+
const unavailable = 69;
32+
33+
/// An internal software error has been detected.
34+
const software = 70;
35+
36+
/// An operating system error has been detected.
37+
const os = 71;
38+
39+
/// Some system file did not exist or was unreadable.
40+
const osFile = 72;
41+
42+
/// A user-specified output file cannot be created.
43+
const cantCreate = 73;
44+
45+
/// An error occurred while doing I/O on some file.
46+
const io = 74;
47+
48+
/// Temporary failure, indicating something that is not really an error.
49+
const tempFail = 75;
50+
51+
/// The remote system returned something invalid during a protocol exchange.
52+
const protocol = 76;
53+
54+
/// The user did not have sufficient permissions.
55+
const noPerm = 77;
56+
57+
/// Something was unconfigured or mis-configured.
58+
const config = 78;

‎lib/src/invoker.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ class Invoker {
172172
timer.cancel();
173173
_controller.setState(
174174
new State(Status.complete, liveTest.state.result));
175-
_controller.completer.complete();
175+
176+
// Use [Timer.run] here to avoid starving the DOM or other
177+
// non-microtask events.
178+
Timer.run(_controller.completer.complete);
176179
});
177180
}, zoneValues: {#unittest.invoker: this}, onError: handleError);
178181
});

‎lib/src/load_exception.dart‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.load_exception;
6+
7+
import 'dart:isolate';
8+
9+
import 'package:path/path.dart' as p;
10+
11+
import 'utils.dart';
12+
13+
class LoadException implements Exception {
14+
final String path;
15+
16+
final innerError;
17+
18+
LoadException(this.path, this.innerError);
19+
20+
String toString() {
21+
var buffer = new StringBuffer('Failed to load "$path":');
22+
23+
var innerString = getErrorMessage(innerError);
24+
if (innerError is IsolateSpawnException) {
25+
// If this is a parse error, get rid of the noisy preamble.
26+
innerString = innerString
27+
.replaceFirst("'${p.toUri(p.absolute(path))}': error: ", "");
28+
29+
// If this is a file system error, get rid of both the preamble and the
30+
// useless stack trace.
31+
innerString = innerString.replaceFirst(
32+
"Unhandled exception:\n"
33+
"Uncaught Error: Load Error: FileSystemException: ",
34+
"");
35+
innerString = innerString.split("Stack Trace:\n").first.trim();
36+
}
37+
38+
buffer.write(innerString.contains("\n") ? "\n" : " ");
39+
buffer.write(innerString);
40+
return buffer.toString();
41+
}
42+
}

‎lib/src/loader.dart‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import 'package:path/path.dart' as p;
1212

1313
import 'dart.dart';
1414
import 'isolate_test.dart';
15+
import 'load_exception.dart';
16+
import 'remote_exception.dart';
1517
import 'suite.dart';
1618

1719
/// A class for finding test files and loading them into a runnable form.
@@ -49,17 +51,15 @@ class Loader {
4951

5052
/// Loads a test suite from the file at [path].
5153
///
52-
/// This wil throw a [FileSystemException] if there's no `packages/` directory
53-
/// available for [path]. Any other load error will cause an
54-
/// [IsolateSpawnException] or a [RemoteException].
54+
/// This will throw a [LoadException] if the file fails to load.
5555
Future<Suite> loadFile(String path) {
5656
// TODO(nweiz): Support browser tests.
5757
var packageRoot = _packageRoot == null
5858
? p.join(p.dirname(path), 'packages')
5959
: _packageRoot;
6060

6161
if (!new Directory(packageRoot).existsSync()) {
62-
throw new FileSystemException("Directory $packageRoot does not exist.");
62+
throw new LoadException(path, "Directory $packageRoot does not exist.");
6363
}
6464

6565
var receivePort = new ReceivePort();
@@ -70,15 +70,27 @@ import "${p.toUri(p.absolute(path))}" as test;
7070
7171
void main(_, Map message) {
7272
var sendPort = message['reply'];
73-
VmListener.start(sendPort, test.main);
73+
VmListener.start(sendPort, () => test.main);
7474
}
7575
''', {
7676
'reply': receivePort.sendPort
77-
}, packageRoot: packageRoot).then((isolate) {
77+
}, packageRoot: packageRoot).catchError((error, stackTrace) {
78+
receivePort.close();
79+
return new Future.error(new LoadException(path, error), stackTrace);
80+
}).then((isolate) {
7881
_isolates.add(isolate);
7982
return receivePort.first;
80-
}).then((tests) {
81-
return new Suite(path, tests.map((test) {
83+
}).then((response) {
84+
if (response["type"] == "loadException") {
85+
return new Future.error(new LoadException(path, response["message"]));
86+
} else if (response["type"] == "error") {
87+
var asyncError = RemoteException.deserialize(response["error"]);
88+
return new Future.error(
89+
new LoadException(path, asyncError.error),
90+
asyncError.stackTrace);
91+
}
92+
93+
return new Suite(path, response["tests"].map((test) {
8294
return new IsolateTest(test['name'], test['sendPort']);
8395
}));
8496
});

0 commit comments

Comments
 (0)