-
Notifications
You must be signed in to change notification settings - Fork 204
/
run_test.dart
211 lines (190 loc) · 6.65 KB
/
run_test.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) 2018, 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.
@Tags(['integration'])
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:_test_common/common.dart';
import 'package:async/async.dart';
import 'package:io/io.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;
void main() {
setUpAll(() async {
await d.dir('a', [
await pubspec(
'a',
currentIsolateDependencies: [
'build',
'build_config',
'build_daemon',
'build_modules',
'build_resolvers',
'build_runner',
'build_runner_core',
'build_test',
'build_web_compilers',
'code_builder',
'test',
],
),
d.dir('test', [
d.file('hello_test.dart', '''
import 'package:test/test.dart';
main() {
test('hello', () {});
}'''),
]),
d.dir('web', [
d.file('main.dart', '''
main() {
print('hello world');
}'''),
]),
]).create();
await pubGet('a', offline: false);
});
tearDown(() async {
expect((await runPub('a', 'run', args: ['build_runner', 'clean'])).exitCode,
0);
});
void expectOutput(String path, {required bool exists}) {
path =
p.join(d.sandbox, 'a', '.dart_tool', 'build', 'generated', 'a', path);
expect(File(path).existsSync(), exists);
}
Future<int> runSingleBuild(String command, List<String> args,
{StreamSink<String>? stdoutSink}) async {
var process = await startPub('a', 'run', args: args);
var stdoutLines = process.stdout
.transform(Utf8Decoder())
.transform(LineSplitter())
.asBroadcastStream()
..listen((line) {
stdoutSink?.add(line);
printOnFailure(line);
});
var queue = StreamQueue(stdoutLines);
if (command == 'serve' || command == 'watch') {
while (await queue.hasNext) {
var nextLine = (await queue.next).toLowerCase();
if (nextLine.contains('succeeded after')) {
process.kill();
await process.exitCode;
return ExitCode.success.code;
} else if (nextLine.contains('failed after')) {
process.kill();
await process.exitCode;
return 1;
}
}
throw StateError('Build process exited without success or failure.');
}
var result = await process.exitCode;
return result;
}
group('Building explicit output directories', () {
void testBasicBuildCommand(String command) {
test('is supported by the $command command', () async {
var args = ['build_runner', command, 'web'];
expect(await runSingleBuild(command, args), ExitCode.success.code);
expectOutput('web/main.dart.js', exists: true);
expectOutput('test/hello_test.dart.browser_test.dart.js',
exists: false);
});
}
void testBuildCommandWithOutput(String command) {
test('works with -o and the $command command', () async {
var outputDirName = 'foo';
var args = [
'build_runner',
command,
'web',
'-o',
'test:$outputDirName'
];
expect(await runSingleBuild(command, args), ExitCode.success.code);
expectOutput('web/main.dart.js', exists: true);
expectOutput('test/hello_test.dart.browser_test.dart.js', exists: true);
var outputDir = Directory(p.join(d.sandbox, 'a', 'foo'));
await outputDir.delete(recursive: true);
});
}
for (var command in ['build', 'serve', 'watch']) {
testBasicBuildCommand(command);
testBuildCommandWithOutput(command);
}
test('is not supported for the test command', () async {
var command = 'test';
var args = ['build_runner', command, 'web'];
expect(await runSingleBuild(command, args), ExitCode.usage.code);
args.addAll(['--', '-p chrome']);
expect(await runSingleBuild(command, args), ExitCode.usage.code);
});
});
test('test builds only the test directory by default', () async {
var command = 'test';
var args = ['build_runner', command];
expect(await runSingleBuild(command, args), ExitCode.success.code);
expectOutput('web/main.dart.js', exists: false);
expectOutput('test/hello_test.dart.browser_test.dart.js', exists: true);
});
test('hoists output correctly even with --symlink', () async {
var command = 'build';
var outputDirName = 'foo';
var args = [
'build_runner',
command,
'-o',
'web:$outputDirName',
'--symlink'
];
expect(await runSingleBuild(command, args), ExitCode.success.code);
var outputDir = Directory(p.join(d.sandbox, 'a', 'foo'));
expect(File(p.join(outputDir.path, 'web', 'main.dart.js')).existsSync(),
isFalse);
expect(File(p.join(outputDir.path, 'main.dart.js')).existsSync(), isTrue);
await outputDir.delete(recursive: true);
});
test('Duplicate output directories give a nice error', () async {
var command = 'build';
var args = ['build_runner', command, '-o', 'web:build', '-o', 'test:build'];
var stdoutController = StreamController<String>();
expect(
await runSingleBuild(command, args, stdoutSink: stdoutController.sink),
ExitCode.usage.code);
expect(
stdoutController.stream,
emitsThrough(contains(
'Invalid argument (--output): Duplicate output directories are not '
'allowed, got: "web:build test:build"')));
await stdoutController.close();
});
test('Build directories have to be top level dirs', () async {
var command = 'build';
var args = ['build_runner', command, '-o', 'foo/bar:build'];
var stdoutController = StreamController<String>();
expect(
await runSingleBuild(command, args, stdoutSink: stdoutController.sink),
ExitCode.usage.code);
expect(
stdoutController.stream,
emitsThrough(contains(
'Invalid argument (--output): Input root can not be nested: '
'"foo/bar:build"')));
await stdoutController.close();
});
test('Handles socket errors gracefully', () async {
var server = await HttpServer.bind('localhost', 8080);
addTearDown(server.close);
var process =
await runPub('a', 'run', args: ['build_runner', 'serve', 'web:8080']);
expect(process.exitCode, ExitCode.osError.code);
expect(
process.stdout,
allOf(contains('Error starting server'), contains('8080'),
contains('address is already in use')));
});
}