Skip to content

Add an integration test for serve #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 101 additions & 17 deletions webdev/test/e2e_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

@Timeout(const Duration(minutes: 8))
@Timeout(const Duration(minutes: 5))

import 'dart:async';
import 'dart:io';

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

import 'test_utils.dart';

/// Key: name of file in web directory
/// Value: `null` - exists in both modes
/// `true` - DDC only
/// `false` - dart2js only
const _testItems = const <String, bool>{
'main.dart.js': null,
'main.dart.bootstrap.js': true,
'main.ddc.js': true
};

void main() {
group('should succeed with valid configuration', () {
for (var withDDC in [true, false]) {
test(withDDC ? 'DDC' : 'dart2js', () async {
var exampleDirectory = p.absolute(p.join(p.current, '..', 'example'));
var process = await TestProcess.start(pubPath, ['get'],
workingDirectory: exampleDirectory,
environment: _getPubEnvironment());
String exampleDirectory;
setUpAll(() async {
exampleDirectory = p.absolute(p.join(p.current, '..', 'example'));

await process.shouldExit(0);
var process = await TestProcess.start(pubPath, ['get'],
workingDirectory: exampleDirectory, environment: _getPubEnvironment());

await d.file('.packages', isNotEmpty).validate(exampleDirectory);
await d.file('pubspec.lock', isNotEmpty).validate(exampleDirectory);
await process.shouldExit(0);

await d.file('.packages', isNotEmpty).validate(exampleDirectory);
await d.file('pubspec.lock', isNotEmpty).validate(exampleDirectory);
});

group('should build with valid configuration', () {
for (var withDDC in [true, false]) {
test(withDDC ? 'DDC' : 'dart2js', () async {
var args = ['build', '-o', 'web:${d.sandbox}'];
if (withDDC) {
args.add('--no-release');
}

process = await runWebDev(args, workingDirectory: exampleDirectory);
var process = await runWebDev(args, workingDirectory: exampleDirectory);

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

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

for (var ddcFile in ['main.dart.bootstrap.js', 'main.ddc.js']) {
if (withDDC) {
await d.file(ddcFile, isNotEmpty).validate();
if (shouldExist) {
await d.file(entry.key, isNotEmpty).validate();
} else {
await d.nothing(ddcFile).validate();
await d.nothing(entry.key).validate();
}
}
});
}
});

group('should serve with valid configuration', () {
for (var withDDC in [true, false]) {
test(withDDC ? 'DDC' : 'dart2js', () async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd argue that this kind of logic will make it harder to know where to look if one of these tests starts failing.

var openPort = await _getOpenPort();
var args = ['serve', 'web:$openPort'];
if (!withDDC) {
args.add('--release');
}

var process = await runWebDev(args, workingDirectory: exampleDirectory);

var hostUrl = 'http://localhost:$openPort';

await expectLater(
process.stdout, emitsThrough('Serving `web` on $hostUrl'));

var client = new HttpClient();

try {
for (var entry in _testItems.entries) {
var url = Uri.parse('$hostUrl/${entry.key}');

var request = await client.getUrl(url);
var response = await request.close();

var shouldExist = (entry.value ?? withDDC) == withDDC;

if (entry.key == 'main.ddc.js') {
// This file SHOULD NOT be output in dart2js mode
// But there is an issue here
// https://github.com/dart-lang/build/issues/1033
shouldExist = true;
}

expect(response.statusCode, shouldExist ? 200 : 404);
}
} finally {
client.close(force: true);
}

if (Platform.isWindows) {
await process.kill();
await process.shouldExit(-1);
} else {
process.signal(ProcessSignal.SIGINT);
await process.shouldExit(0);
}
});
}
});
}

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

return environment;
}

/// Returns an open port by creating a temporary Socket
Future<int> _getOpenPort() async {
ServerSocket socket;

try {
socket = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0);
} catch (_) {
// try again v/ V6 only. Slight possibility that V4 is disabled
socket = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V6, 0,
v6Only: true);
}

try {
return socket.port;
} finally {
await socket.close();
}
}