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