|
| 1 | +// Copyright 2019 Workiva Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +@TestOn('vm') |
| 16 | +import 'dart:async'; |
| 17 | +import 'dart:convert'; |
| 18 | +import 'dart:io'; |
| 19 | + |
| 20 | +import 'package:http/http.dart' as http; |
| 21 | +import 'package:test/test.dart'; |
| 22 | +import 'package:webdev_proxy/src/port_utils.dart'; |
| 23 | +import 'package:webdev_proxy/src/webdev_proc_utils.dart'; |
| 24 | +import 'package:webdev_proxy/src/webdev_server.dart'; |
| 25 | + |
| 26 | +import 'util.dart'; |
| 27 | + |
| 28 | +// Matches the build-complete patterns that build_runner emits to webdev stdout. |
| 29 | +final _buildCompletePattern = |
| 30 | + RegExp(r'Built with build_runner|Failed to build with build_runner'); |
| 31 | + |
| 32 | +// Strips ANSI escape sequences so output is readable in test logs. |
| 33 | +String _stripAnsi(String s) => s.replaceAll(RegExp(r'\x1b\[[0-9;]*[a-zA-Z]'), ''); |
| 34 | + |
| 35 | +void main() { |
| 36 | + late WebdevServer webdevServer; |
| 37 | + |
| 38 | + setUpAll(() async { |
| 39 | + await activateWebdev(webdevCompatibility.toString()); |
| 40 | + }); |
| 41 | + |
| 42 | + tearDown(() async { |
| 43 | + await webdevServer.close(); |
| 44 | + }); |
| 45 | + |
| 46 | + test( |
| 47 | + 'WebdevServer.stdoutLines emits build-complete signal before HTTP server ' |
| 48 | + 'responds', () async { |
| 49 | + final port = await findUnusedPort(); |
| 50 | + webdevServer = await WebdevServer.start(['test:$port']); |
| 51 | + |
| 52 | + final allLines = <String>[]; |
| 53 | + webdevServer.stdoutLines.listen((line) => allLines.add(line)); |
| 54 | + |
| 55 | + String? matchedLine; |
| 56 | + try { |
| 57 | + matchedLine = await webdevServer.stdoutLines |
| 58 | + .firstWhere(_buildCompletePattern.hasMatch) |
| 59 | + .timeout(const Duration(seconds: 90)); |
| 60 | + } on TimeoutException { |
| 61 | + fail('Timed out waiting for build-complete signal.\n' |
| 62 | + 'Lines seen:\n${allLines.map(_stripAnsi).map((l) => ' $l').join('\n')}'); |
| 63 | + } on StateError { |
| 64 | + fail('Build-complete signal never appeared — webdev exited early.\n' |
| 65 | + 'Lines seen:\n${allLines.map(_stripAnsi).map((l) => ' $l').join('\n')}'); |
| 66 | + } |
| 67 | + |
| 68 | + expect(_stripAnsi(matchedLine), matches(_buildCompletePattern), |
| 69 | + reason: 'Matched line should contain the build-complete pattern'); |
| 70 | + |
| 71 | + // Verify the server is also HTTP-responsive at this point. |
| 72 | + final response = await http |
| 73 | + .get(Uri.parse('http://localhost:$port/')) |
| 74 | + .timeout(const Duration(seconds: 10)); |
| 75 | + expect(response.statusCode, isNot(equals(0))); |
| 76 | + }); |
| 77 | +} |
0 commit comments