|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a
|
3 | 3 | // BSD-style license that can be found in the LICENSE file.
|
4 | 4 |
|
| 5 | +@Timeout(Duration(minutes: 3)) |
| 6 | + |
5 | 7 | import 'dart:async';
|
| 8 | +import 'dart:convert'; |
| 9 | +import 'dart:io'; |
6 | 10 |
|
| 11 | +import 'package:path/path.dart' as p; |
7 | 12 | import 'package:test/test.dart';
|
8 | 13 | import 'package:test_descriptor/test_descriptor.dart' as d;
|
9 | 14 |
|
10 | 15 | import 'test_utils.dart';
|
11 | 16 |
|
| 17 | +enum StreamType { |
| 18 | + stdout, |
| 19 | + stderr, |
| 20 | +} |
| 21 | + |
| 22 | +const processTimeout = Duration(minutes: 1); |
| 23 | + |
12 | 24 | void main() {
|
13 | 25 | final testRunner = TestRunner();
|
14 | 26 | setUpAll(testRunner.setUpAll);
|
15 | 27 | tearDownAll(testRunner.tearDownAll);
|
16 | 28 |
|
| 29 | + Future<void> expectStdoutAndCleanExit(Process process, |
| 30 | + {required String expectedStdout}) async { |
| 31 | + final stdoutCompleter = _captureOutput( |
| 32 | + process, |
| 33 | + streamType: StreamType.stdout, |
| 34 | + stopCaptureFuture: process.exitCode, |
| 35 | + ); |
| 36 | + final stderrCompleter = _captureOutput( |
| 37 | + process, |
| 38 | + streamType: StreamType.stderr, |
| 39 | + stopCaptureFuture: process.exitCode, |
| 40 | + ); |
| 41 | + final exitCode = await _waitForExitOrTimeout(process); |
| 42 | + final stderrLogs = await stderrCompleter.future; |
| 43 | + final stdoutLogs = await stdoutCompleter.future; |
| 44 | + expect( |
| 45 | + exitCode, |
| 46 | + equals(0), |
| 47 | + // Include the stderr and stdout logs if the process does not terminate |
| 48 | + // cleanly: |
| 49 | + reason: 'stderr: $stderrLogs, stdout: $stdoutLogs', |
| 50 | + ); |
| 51 | + expect( |
| 52 | + stderrLogs, |
| 53 | + isEmpty, |
| 54 | + ); |
| 55 | + expect( |
| 56 | + stdoutLogs, |
| 57 | + contains(expectedStdout), |
| 58 | + ); |
| 59 | + } |
| 60 | + |
17 | 61 | test('non-existent commands create errors', () async {
|
18 | 62 | final process = await testRunner.runWebDev(['monkey']);
|
19 | 63 |
|
@@ -214,6 +258,26 @@ dependencies:
|
214 | 258 | await checkProcessStdout(process, ['webdev could not run']);
|
215 | 259 | await process.shouldExit(78);
|
216 | 260 | });
|
| 261 | + |
| 262 | + if (command != 'daemon') { |
| 263 | + test('failure with offline and unresolved dependencies', () async { |
| 264 | + final createProcess = await Process.start( |
| 265 | + 'dart', |
| 266 | + ['create', '--no-pub', '--template', 'web', 'temp_app'], |
| 267 | + workingDirectory: d.sandbox, |
| 268 | + ); |
| 269 | + await expectStdoutAndCleanExit(createProcess, |
| 270 | + expectedStdout: 'Created project temp_app'); |
| 271 | + |
| 272 | + final appPath = p.join(d.sandbox, 'temp_app'); |
| 273 | + |
| 274 | + final process = await testRunner |
| 275 | + .runWebDev([command, '--offline'], workingDirectory: appPath); |
| 276 | + |
| 277 | + await checkProcessStdout(process, ['webdev could not run']); |
| 278 | + await process.shouldExit(78); |
| 279 | + }); |
| 280 | + } |
217 | 281 | });
|
218 | 282 | }
|
219 | 283 | }
|
@@ -286,3 +350,36 @@ packages:
|
286 | 350 |
|
287 | 351 | return buffer.toString();
|
288 | 352 | }
|
| 353 | + |
| 354 | +Future<int> _waitForExitOrTimeout(Process process) { |
| 355 | + Timer(processTimeout, () { |
| 356 | + process.kill(ProcessSignal.sigint); |
| 357 | + }); |
| 358 | + return process.exitCode; |
| 359 | +} |
| 360 | + |
| 361 | +Completer<String> _captureOutput( |
| 362 | + Process process, { |
| 363 | + required StreamType streamType, |
| 364 | + required Future stopCaptureFuture, |
| 365 | +}) { |
| 366 | + final stream = |
| 367 | + streamType == StreamType.stdout ? process.stdout : process.stderr; |
| 368 | + final completer = Completer<String>(); |
| 369 | + var output = ''; |
| 370 | + stream.transform(utf8.decoder).listen((line) { |
| 371 | + output += line; |
| 372 | + if (line.contains('[SEVERE]')) { |
| 373 | + process.kill(ProcessSignal.sigint); |
| 374 | + if (!completer.isCompleted) { |
| 375 | + completer.complete(output); |
| 376 | + } |
| 377 | + } |
| 378 | + }); |
| 379 | + unawaited(stopCaptureFuture.then((_) { |
| 380 | + if (!completer.isCompleted) { |
| 381 | + completer.complete(output); |
| 382 | + } |
| 383 | + })); |
| 384 | + return completer; |
| 385 | +} |
0 commit comments