-
Notifications
You must be signed in to change notification settings - Fork 211
/
executable.dart
194 lines (167 loc) · 5.67 KB
/
executable.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:io';
import 'package:async/async.dart';
import 'package:path/path.dart' as p;
import 'package:source_span/source_span.dart';
import 'package:stack_trace/stack_trace.dart';
import 'package:test_api/src/utils.dart'; // ignore: implementation_imports
import 'runner/application_exception.dart';
import 'runner/configuration.dart';
import 'runner/version.dart';
import 'runner.dart';
import 'util/exit_codes.dart' as exit_codes;
import 'util/io.dart';
StreamSubscription signalSubscription;
bool isShutdown = false;
/// Returns the path to the global test configuration file.
final String _globalConfigPath = () {
if (Platform.environment.containsKey('DART_TEST_CONFIG')) {
return Platform.environment['DART_TEST_CONFIG'];
} else if (Platform.operatingSystem == 'windows') {
return p.join(Platform.environment['LOCALAPPDATA'], 'DartTest.yaml');
} else {
return '${Platform.environment['HOME']}/.dart_test.yaml';
}
}();
Future<void> main(List<String> args) async {
await _execute(args);
completeShutdown();
}
Future<void> runTests(List<String> args) async {
await _execute(args);
}
void completeShutdown() {
if (isShutdown) return;
if (signalSubscription != null) {
signalSubscription.cancel();
signalSubscription = null;
}
isShutdown = true;
stdinLines.cancel(immediate: true);
}
Future<void> _execute(List<String> args) async {
/// A merged stream of all signals that tell the test runner to shut down
/// gracefully.
///
/// Signals will only be captured as long as this has an active subscription.
/// Otherwise, they'll be handled by Dart's default signal handler, which
/// terminates the program immediately.
final _signals = Platform.isWindows
? ProcessSignal.sigint.watch()
: StreamGroup.merge(
[ProcessSignal.sigterm.watch(), ProcessSignal.sigint.watch()]);
Configuration configuration;
try {
configuration = Configuration.parse(args);
} on FormatException catch (error) {
_printUsage(error.message);
exitCode = exit_codes.usage;
return;
}
if (configuration.help) {
_printUsage();
return;
}
if (configuration.version) {
var version = testVersion;
if (version == null) {
stderr.writeln("Couldn't find version number.");
exitCode = exit_codes.data;
} else {
print(version);
}
return;
}
try {
var fileConfiguration = Configuration.empty;
if (File(_globalConfigPath).existsSync()) {
fileConfiguration = fileConfiguration
.merge(Configuration.load(_globalConfigPath, global: true));
}
if (File(configuration.configurationPath).existsSync()) {
fileConfiguration = fileConfiguration
.merge(Configuration.load(configuration.configurationPath));
}
configuration = fileConfiguration.merge(configuration);
} on SourceSpanFormatException catch (error) {
stderr.writeln(error.toString(color: configuration.color));
exitCode = exit_codes.data;
return;
} on FormatException catch (error) {
stderr.writeln(error.message);
exitCode = exit_codes.data;
return;
} on IOException catch (error) {
stderr.writeln(error.toString());
exitCode = exit_codes.noInput;
return;
}
var undefinedPresets = configuration.chosenPresets
.where((preset) => !configuration.knownPresets.contains(preset))
.toList();
if (undefinedPresets.isNotEmpty) {
_printUsage("Undefined ${pluralize('preset', undefinedPresets.length)} "
"${toSentence(undefinedPresets.map((preset) => '"$preset"'))}.");
exitCode = exit_codes.usage;
return;
}
if (!configuration.explicitPaths &&
!Directory(configuration.paths.single).existsSync()) {
_printUsage('No test files were passed and the default "test/" '
"directory doesn't exist.");
exitCode = exit_codes.data;
return;
}
Runner runner;
signalSubscription ??= _signals.listen((signal) async {
completeShutdown();
await runner?.close();
});
try {
runner = Runner(configuration);
exitCode = (await runner.run()) ? 0 : 1;
} on ApplicationException catch (error) {
stderr.writeln(error.message);
exitCode = exit_codes.data;
} on SourceSpanFormatException catch (error) {
stderr.writeln(error.toString(color: configuration.color));
exitCode = exit_codes.data;
} on FormatException catch (error) {
stderr.writeln(error.message);
exitCode = exit_codes.data;
} catch (error, stackTrace) {
stderr.writeln(getErrorMessage(error));
stderr.writeln(Trace.from(stackTrace).terse);
stderr.writeln('This is an unexpected error. Please file an issue at '
'http://github.com/dart-lang/test\n'
'with the stack trace and instructions for reproducing the error.');
exitCode = exit_codes.software;
} finally {
await runner?.close();
}
// TODO(grouma) - figure out why the executable can hang in the travis
// environment. https://github.com/dart-lang/test/issues/599
if (Platform.environment['FORCE_TEST_EXIT'] == 'true') {
exit(exitCode);
}
return;
}
/// Print usage information for this command.
///
/// If [error] is passed, it's used in place of the usage message and the whole
/// thing is printed to stderr instead of stdout.
void _printUsage([String error]) {
var output = stdout;
var message = 'Runs tests in this package.';
if (error != null) {
message = error;
output = stderr;
}
output.write('''${wordWrap(message)}
Usage: pub run test [files or directories...]
${Configuration.usage}
''');
}