/
daemon_test.dart
381 lines (351 loc) · 12.3 KB
/
daemon_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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright (c) 2019, 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:build_daemon/client.dart';
import 'package:build_daemon/constants.dart';
import 'package:build_daemon/data/build_status.dart';
import 'package:build_daemon/data/build_target.dart';
import 'package:build_daemon/data/shutdown_notification.dart';
import 'package:build_runner/src/daemon/constants.dart';
import 'package:build_runner_core/src/util/constants.dart' show dartBinary;
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;
void main() {
Process? daemonProcess;
Stream<String>? stdoutLines;
String workspace() => p.join(d.sandbox, 'a');
final webTarget = DefaultBuildTarget((b) => b..target = 'web');
final testTarget = DefaultBuildTarget((b) => b..target = 'test');
var clients = <BuildDaemonClient>[];
setUp(() 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.dart', '''
main() {
// Don't actually use test package to speed up tests
print('hello');
}'''),
]),
d.dir('lib', [
d.file('message.dart', '''
const message = 'hello world';
'''),
]),
d.dir('web', [
d.file('main.dart', '''
import 'package:a/message.dart';
main() {
print(message);
}'''),
]),
]).create();
await pubGet('a', offline: false);
// We use `addTearDown` to ensure this runs before the temp dir gets
// cleaned up, otherwise there is a race condition that causes flaky tests.
addTearDown(() async {
for (var client in clients) {
await client.close();
}
clients.clear();
stdoutLines = null;
daemonProcess?.kill(ProcessSignal.sigkill);
await daemonProcess?.exitCode;
});
});
Future<BuildDaemonClient> _startClient(
{BuildMode buildMode = BuildMode.Auto, List<String> options = const []}) {
var args = ['run', 'build_runner', 'daemon', ...options];
printOnFailure('Starting client in: ${workspace()}');
return BuildDaemonClient.connect(
workspace(),
[
dartBinary,
...args,
],
logHandler: (log) => printOnFailure('Client: ${log.message}'),
buildMode: buildMode);
}
Future<void> _startDaemon(
{BuildMode buildMode = BuildMode.Auto,
List<String> options = const []}) async {
var args = [
'build_runner',
'daemon',
'--$buildModeFlag=$buildMode',
...options
];
printOnFailure('Starting daemon in: ${workspace()}');
var daemon = daemonProcess = await startPub('a', 'run', args: args);
stdoutLines = daemon.stdout
.transform(Utf8Decoder())
.transform(LineSplitter())
.asBroadcastStream()
..listen((line) {
printOnFailure('Daemon: $line');
});
daemon.stderr
.transform(Utf8Decoder())
.transform(LineSplitter())
.listen((line) {
printOnFailure('Daemon Error: $line');
});
unawaited(daemon.exitCode.then((exitCode) {
printOnFailure('GOT EXIT CODE: $exitCode');
}));
expect(await stdoutLines!.contains(readyToConnectLog), isTrue);
}
group('Build Daemon', () {
test('successfully starts', () async {
await _startDaemon();
});
test('shuts down on build script change', () async {
await _startDaemon();
var client = await _startClient()
..registerBuildTarget(webTarget)
..startBuild();
// We need to add a listener otherwise we won't get the event.
unawaited(expectLater(client.shutdownNotifications.first, isNotNull));
// Force a build script change.
await d.dir('a', [
d.dir('.dart_tool', [
d.dir('build', [
d.dir('entrypoint', [d.file('build.dart', '\n')])
])
])
]).create();
});
test('supports --enable-experiment option', () async {
await _startDaemon(options: ['--enable-experiment=fake-experiment']);
var client =
await _startClient(options: ['--enable-experiment=fake-experiment'])
..registerBuildTarget(webTarget)
..startBuild();
clients.add(client);
await expectLater(
client.buildResults,
// TODO: Check for specific message about a bad experiment
emitsThrough((BuildResults b) =>
b.results.first.status == BuildStatus.failed));
});
test('does not shut down down on build script change when configured',
() async {
await _startDaemon(options: ['--skip-build-script-check']);
var client = await _startClient(options: ['--skip-build-script-check'])
..registerBuildTarget(webTarget)
..startBuild();
clients.add(client);
ShutdownNotification? notification;
// We need to add a listener otherwise we won't get the event.
unawaited(client.shutdownNotifications.first
.then((value) => notification = value));
// Force a build script change.
await d.dir('a', [
d.dir('.dart_tool', [
d.dir('build', [
d.dir('entrypoint', [d.file('build.dart', '\n')])
])
])
]).create();
// Give time for the notification to propogate if there was one.
await Future<void>.delayed(Duration(seconds: 4));
expect(notification, isNull);
});
test('errors if build modes conflict', () async {
await _startDaemon();
expect(_startClient(buildMode: BuildMode.Manual),
throwsA(TypeMatcher<OptionsSkew>()));
});
test('can build in manual mode', () async {
await _startDaemon(buildMode: BuildMode.Manual);
var client = await _startClient(buildMode: BuildMode.Manual)
..registerBuildTarget(webTarget)
..startBuild();
clients.add(client);
expect(
client.buildResults,
emitsThrough((BuildResults b) =>
b.results.first.status == BuildStatus.succeeded));
});
test('auto build mode automatically builds on file change', () async {
await _startDaemon();
var client = await _startClient()
..registerBuildTarget(webTarget);
clients.add(client);
// Let the target request propagate.
await Future<void>.delayed(Duration(seconds: 2));
// Trigger a file change.
await d.dir('a', [
d.dir('web', [
d.file('main.dart', '''
main() {
print('goodbye world');
}'''),
])
]).create();
expect(
client.buildResults,
emitsThrough((BuildResults b) =>
b.results.first.status == BuildStatus.succeeded));
});
test('manual build mode does not automatically build on file change',
() async {
await _startDaemon(buildMode: BuildMode.Manual);
var client = await _startClient(buildMode: BuildMode.Manual)
..registerBuildTarget(webTarget);
clients.add(client);
// Let the target request propagate.
await Future<void>.delayed(Duration(seconds: 2));
// Trigger a file change.
await d.dir('a', [
d.dir('web', [
d.file('main.dart', '''
// @dart=2.12
main() {
print('goodbye world');
}'''),
])
]).create();
// There shouldn't be any build results.
var buildResults = await client.buildResults.first
.then<BuildResults?>((r) => r)
.timeout(Duration(seconds: 2), onTimeout: () => null);
expect(buildResults, isNull);
client.startBuild();
var startedResult = await client.buildResults.first;
expect(startedResult.results.first.status, BuildStatus.started,
reason: 'Should do a build once requested');
var succeededResult = await client.buildResults.first;
expect(succeededResult.results.first.status, BuildStatus.succeeded);
var ddcContent = await File(p.join(d.sandbox, 'a', '.dart_tool', 'build',
'generated', 'a', 'web', 'main.sound.ddc.js'))
.readAsString();
expect(ddcContent, contains('goodbye world'));
});
test('can build to outputs', () async {
var outputDir = Directory(p.join(d.sandbox, 'a', 'deploy'));
expect(outputDir.existsSync(), isFalse);
await _startDaemon();
// Start the client with the same options to prevent OptionSkew.
// In the future this should be an option on the target.
var client = await _startClient()
..registerBuildTarget(DefaultBuildTarget((b) => b
..target = 'web'
..outputLocation = OutputLocation((b) => b
..output = 'deploy'
..hoist = true
..useSymlinks = false).toBuilder()))
..startBuild();
clients.add(client);
await client.buildResults
.firstWhere((b) => b.results.first.status == BuildStatus.succeeded);
expect(outputDir.existsSync(), isTrue);
});
test('writes the asset server port', () async {
await _startDaemon();
expect(File(assetServerPortFilePath(workspace())).existsSync(), isTrue);
});
test('notifies upon build start', () async {
await _startDaemon();
var client = await _startClient()
..registerBuildTarget(webTarget)
..startBuild();
clients.add(client);
expect(
client.buildResults,
emitsThrough((BuildResults b) =>
b.results.first.status == BuildStatus.started));
// Wait for the build to finish before exiting to prevent flakiness.
expect(
client.buildResults,
emitsThrough((BuildResults b) =>
b.results.first.status == BuildStatus.succeeded));
});
test('can complete builds', () async {
await _startDaemon();
var client = await _startClient()
..registerBuildTarget(webTarget)
..startBuild();
clients.add(client);
expect(
client.buildResults,
emitsThrough((BuildResults b) =>
b.results.first.status == BuildStatus.succeeded));
});
test('allows multiple clients to connect and build', () async {
await _startDaemon();
var clientA = await _startClient();
clientA.registerBuildTarget(webTarget);
clients.add(clientA);
var clientB = await _startClient()
..registerBuildTarget(testTarget)
..startBuild();
clients.add(clientB);
// Both clients should be notified.
await clientA.buildResults.first;
var buildResultsB = await clientB.buildResults
.firstWhere((b) => b.results.first.status != BuildStatus.started);
expect(buildResultsB.results.first.status, BuildStatus.succeeded);
expect(buildResultsB.results.length, equals(2));
});
group('build filters', () {
setUp(() async {
// Adds an additional entrypoint.
await d.dir('a', [
d.dir('web', [
d.file('other.dart', '''
main() {
print('goodbye world');
}'''),
])
]).create();
});
test('can build specific outputs', () async {
await _startDaemon(buildMode: BuildMode.Manual);
var client = await _startClient(buildMode: BuildMode.Manual)
..registerBuildTarget(DefaultBuildTarget((b) => b
..target = 'web'
..buildFilters.add('web/other.dart.js')))
..startBuild();
clients.add(client);
await client.buildResults
.firstWhere((b) => b.results.first.status == BuildStatus.succeeded);
await d.dir('a', [
d.dir('.dart_tool', [
d.dir('build', [
d.dir('generated', [
d.dir('a', [
d.dir('web', [
d.file('other.dart.js', isNotEmpty),
d.nothing('main.dart.js'),
]),
])
])
])
])
]).validate();
});
});
});
}