|
| 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) => |
| 34 | + s.replaceAll(RegExp(r'\x1b\[[0-9;]*[a-zA-Z]'), ''); |
| 35 | + |
| 36 | +void main() { |
| 37 | + setUpAll(() async { |
| 38 | + await activateWebdev(webdevCompatibility.toString()); |
| 39 | + }); |
| 40 | + |
| 41 | + group('WebdevServer', () { |
| 42 | + late WebdevServer webdevServer; |
| 43 | + |
| 44 | + tearDown(() async { |
| 45 | + await webdevServer.close(); |
| 46 | + }); |
| 47 | + |
| 48 | + test('stdoutLines emits build-complete signal before HTTP server responds', |
| 49 | + () async { |
| 50 | + final port = await findUnusedPort(); |
| 51 | + webdevServer = await WebdevServer.start(['test:$port']); |
| 52 | + |
| 53 | + final allLines = <String>[]; |
| 54 | + webdevServer.stdoutLines.listen((line) => allLines.add(line)); |
| 55 | + |
| 56 | + String? matchedLine; |
| 57 | + try { |
| 58 | + matchedLine = await webdevServer.stdoutLines |
| 59 | + .firstWhere(_buildCompletePattern.hasMatch) |
| 60 | + .timeout(const Duration(seconds: 90)); |
| 61 | + } on TimeoutException { |
| 62 | + fail('Timed out waiting for build-complete signal.\n' |
| 63 | + 'Lines seen:\n' |
| 64 | + '${allLines.map(_stripAnsi).map((l) => ' $l').join('\n')}'); |
| 65 | + } on StateError { |
| 66 | + fail('Build-complete signal never appeared — webdev exited early.\n' |
| 67 | + 'Lines seen:\n' |
| 68 | + '${allLines.map(_stripAnsi).map((l) => ' $l').join('\n')}'); |
| 69 | + } |
| 70 | + |
| 71 | + expect(_stripAnsi(matchedLine), matches(_buildCompletePattern)); |
| 72 | + |
| 73 | + // Verify the server is also HTTP-responsive at this point. |
| 74 | + final response = await http |
| 75 | + .get(Uri.parse('http://localhost:$port/')) |
| 76 | + .timeout(const Duration(seconds: 10)); |
| 77 | + expect(response.statusCode, isNot(equals(0))); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + group('ServeCommand', () { |
| 82 | + late Process proxyProcess; |
| 83 | + |
| 84 | + tearDown(() async { |
| 85 | + proxyProcess.kill(); |
| 86 | + await proxyProcess.exitCode; |
| 87 | + }); |
| 88 | + |
| 89 | + test('outputs "Succeeded after" once the initial build completes', |
| 90 | + () async { |
| 91 | + final port = await findUnusedPort(); |
| 92 | + proxyProcess = await Process.start( |
| 93 | + 'dart', |
| 94 | + ['bin/webdev_proxy.dart', 'serve', '--', 'test:$port'], |
| 95 | + mode: ProcessStartMode.normal, |
| 96 | + ); |
| 97 | + |
| 98 | + final stdoutLines = proxyProcess.stdout |
| 99 | + .transform(utf8.decoder) |
| 100 | + .transform(const LineSplitter()) |
| 101 | + .asBroadcastStream(); |
| 102 | + |
| 103 | + // Drain stderr so the process doesn't block. |
| 104 | + proxyProcess.stderr.drain<void>(); |
| 105 | + |
| 106 | + final allLines = <String>[]; |
| 107 | + stdoutLines.listen((line) => allLines.add(line)); |
| 108 | + |
| 109 | + String? matchedLine; |
| 110 | + try { |
| 111 | + matchedLine = await stdoutLines |
| 112 | + .firstWhere((line) => line.contains('Succeeded after')) |
| 113 | + .timeout(const Duration(seconds: 90)); |
| 114 | + } on TimeoutException { |
| 115 | + fail('Timed out waiting for "Succeeded after" in proxy output.\n' |
| 116 | + 'Lines seen:\n' |
| 117 | + '${allLines.map(_stripAnsi).map((l) => ' $l').join('\n')}'); |
| 118 | + } on StateError { |
| 119 | + fail('"Succeeded after" never appeared — proxy exited early.\n' |
| 120 | + 'Lines seen:\n' |
| 121 | + '${allLines.map(_stripAnsi).map((l) => ' $l').join('\n')}'); |
| 122 | + } |
| 123 | + |
| 124 | + expect(_stripAnsi(matchedLine), contains('Succeeded after')); |
| 125 | + }); |
| 126 | + }); |
| 127 | +} |
0 commit comments