Skip to content

Commit ac0421e

Browse files
authored
Add an integration test for serve (#37)
1 parent 3acb931 commit ac0421e

File tree

1 file changed

+101
-17
lines changed

1 file changed

+101
-17
lines changed

webdev/test/e2e_test.dart

Lines changed: 101 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
@Timeout(const Duration(minutes: 8))
5+
@Timeout(const Duration(minutes: 5))
66

7+
import 'dart:async';
78
import 'dart:io';
89

910
import 'package:path/path.dart' as p;
@@ -14,26 +15,39 @@ import 'package:webdev/src/util.dart';
1415

1516
import 'test_utils.dart';
1617

18+
/// Key: name of file in web directory
19+
/// Value: `null` - exists in both modes
20+
/// `true` - DDC only
21+
/// `false` - dart2js only
22+
const _testItems = const <String, bool>{
23+
'main.dart.js': null,
24+
'main.dart.bootstrap.js': true,
25+
'main.ddc.js': true
26+
};
27+
1728
void main() {
18-
group('should succeed with valid configuration', () {
19-
for (var withDDC in [true, false]) {
20-
test(withDDC ? 'DDC' : 'dart2js', () async {
21-
var exampleDirectory = p.absolute(p.join(p.current, '..', 'example'));
22-
var process = await TestProcess.start(pubPath, ['get'],
23-
workingDirectory: exampleDirectory,
24-
environment: _getPubEnvironment());
29+
String exampleDirectory;
30+
setUpAll(() async {
31+
exampleDirectory = p.absolute(p.join(p.current, '..', 'example'));
2532

26-
await process.shouldExit(0);
33+
var process = await TestProcess.start(pubPath, ['get'],
34+
workingDirectory: exampleDirectory, environment: _getPubEnvironment());
2735

28-
await d.file('.packages', isNotEmpty).validate(exampleDirectory);
29-
await d.file('pubspec.lock', isNotEmpty).validate(exampleDirectory);
36+
await process.shouldExit(0);
3037

38+
await d.file('.packages', isNotEmpty).validate(exampleDirectory);
39+
await d.file('pubspec.lock', isNotEmpty).validate(exampleDirectory);
40+
});
41+
42+
group('should build with valid configuration', () {
43+
for (var withDDC in [true, false]) {
44+
test(withDDC ? 'DDC' : 'dart2js', () async {
3145
var args = ['build', '-o', 'web:${d.sandbox}'];
3246
if (withDDC) {
3347
args.add('--no-release');
3448
}
3549

36-
process = await runWebDev(args, workingDirectory: exampleDirectory);
50+
var process = await runWebDev(args, workingDirectory: exampleDirectory);
3751

3852
var expectedItems = <Object>['[INFO] Succeeded'];
3953
if (!withDDC) {
@@ -44,18 +58,69 @@ void main() {
4458
await checkProcessStdout(process, expectedItems);
4559
await process.shouldExit(0);
4660

47-
await d.file('main.dart.js', isNotEmpty).validate();
61+
for (var entry in _testItems.entries) {
62+
var shouldExist = (entry.value ?? withDDC) == withDDC;
4863

49-
for (var ddcFile in ['main.dart.bootstrap.js', 'main.ddc.js']) {
50-
if (withDDC) {
51-
await d.file(ddcFile, isNotEmpty).validate();
64+
if (shouldExist) {
65+
await d.file(entry.key, isNotEmpty).validate();
5266
} else {
53-
await d.nothing(ddcFile).validate();
67+
await d.nothing(entry.key).validate();
5468
}
5569
}
5670
});
5771
}
5872
});
73+
74+
group('should serve with valid configuration', () {
75+
for (var withDDC in [true, false]) {
76+
test(withDDC ? 'DDC' : 'dart2js', () async {
77+
var openPort = await _getOpenPort();
78+
var args = ['serve', 'web:$openPort'];
79+
if (!withDDC) {
80+
args.add('--release');
81+
}
82+
83+
var process = await runWebDev(args, workingDirectory: exampleDirectory);
84+
85+
var hostUrl = 'http://localhost:$openPort';
86+
87+
await expectLater(
88+
process.stdout, emitsThrough('Serving `web` on $hostUrl'));
89+
90+
var client = new HttpClient();
91+
92+
try {
93+
for (var entry in _testItems.entries) {
94+
var url = Uri.parse('$hostUrl/${entry.key}');
95+
96+
var request = await client.getUrl(url);
97+
var response = await request.close();
98+
99+
var shouldExist = (entry.value ?? withDDC) == withDDC;
100+
101+
if (entry.key == 'main.ddc.js') {
102+
// This file SHOULD NOT be output in dart2js mode
103+
// But there is an issue here
104+
// https://github.com/dart-lang/build/issues/1033
105+
shouldExist = true;
106+
}
107+
108+
expect(response.statusCode, shouldExist ? 200 : 404);
109+
}
110+
} finally {
111+
client.close(force: true);
112+
}
113+
114+
if (Platform.isWindows) {
115+
await process.kill();
116+
await process.shouldExit(-1);
117+
} else {
118+
process.signal(ProcessSignal.SIGINT);
119+
await process.shouldExit(0);
120+
}
121+
});
122+
}
123+
});
59124
}
60125

61126
/// Returns an environment map that includes `PUB_ENVIRONMENT`.
@@ -74,3 +139,22 @@ Map<String, String> _getPubEnvironment() {
74139

75140
return environment;
76141
}
142+
143+
/// Returns an open port by creating a temporary Socket
144+
Future<int> _getOpenPort() async {
145+
ServerSocket socket;
146+
147+
try {
148+
socket = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0);
149+
} catch (_) {
150+
// try again v/ V6 only. Slight possibility that V4 is disabled
151+
socket = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V6, 0,
152+
v6Only: true);
153+
}
154+
155+
try {
156+
return socket.port;
157+
} finally {
158+
await socket.close();
159+
}
160+
}

0 commit comments

Comments
 (0)