blob: 4a49e8cbcb27474bccb2427f1a4f5714872d705f [file] [log] [blame]
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// 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.
// A regression test for issue 22667.
//
// Makes sure that we don't print a '\0' character for files that are not
// properly new-line terminated.
library zero_termination_test;
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:async_helper/async_helper.dart';
import 'package:compiler/src/commandline_options.dart';
import 'package:expect/expect.dart';
import 'package:path/path.dart' as path;
import 'end_to_end/launch_helper.dart' show launchDart2Js;
Uri pathOfData = Platform.script;
Directory tempDir;
String outFilePath;
_sendNotFound(HttpResponse response) {
response.statusCode = HttpStatus.NOT_FOUND;
response.close();
}
Future handleRequest(HttpRequest request) {
final String path = request.uri.path.substring(1);
final Uri requestPath = pathOfData.resolve(path);
final File file = new File(requestPath.toFilePath());
return file.exists().then((bool found) {
if (found) {
file.openRead().pipe(request.response).catchError((e) {
_sendNotFound(request.response);
});
} else {
_sendNotFound(request.response);
}
});
}
void cleanup() {
File outFile = new File(outFilePath);
if (outFile.existsSync()) {
outFile.deleteSync();
}
}
void check(ProcessResult result) {
Expect.notEquals(0, result.exitCode);
List<int> stdout = result.stdout;
String stdoutString = utf8.decode(stdout);
Expect.isTrue(stdoutString.contains("Error"));
// Make sure the "499" from the last line is in the output.
Expect.isTrue(stdoutString.contains("499"));
// Make sure that the output does not contain any 0 character.
Expect.isFalse(stdout.contains(0));
}
Future testFile({bool useKernel}) async {
String inFilePath =
pathOfData.resolve('data/one_line_dart_program.dart').path;
List<String> args = [inFilePath, "--out=" + outFilePath];
if (!useKernel) args.add(Flags.useOldFrontend);
await cleanup();
check(await launchDart2Js(args, noStdoutEncoding: true));
await cleanup();
}
Future serverRunning(HttpServer server, {bool useKernel}) async {
int port = server.port;
String inFilePath = "http://127.0.0.1:$port/data/one_line_dart_program.dart";
List<String> args = [inFilePath, "--out=" + outFilePath];
if (!useKernel) args.add(Flags.useOldFrontend);
server.listen(handleRequest);
try {
await cleanup();
check(await launchDart2Js(args, noStdoutEncoding: true));
} finally {
await server.close();
await cleanup();
}
}
Future testHttp({bool useKernel}) {
return HttpServer
.bind(InternetAddress.LOOPBACK_IP_V4, 0)
.then((HttpServer server) => serverRunning(server, useKernel: useKernel));
}
runTests({bool useKernel}) async {
tempDir = Directory.systemTemp.createTempSync('directory_test');
outFilePath = path.join(tempDir.path, "out.js");
try {
await testFile(useKernel: useKernel);
if (!useKernel) {
// TODO(johnniwinther): Handle this test for kernel.
await testHttp(useKernel: useKernel);
}
} finally {
await tempDir.delete(recursive: true);
}
}
main() {
asyncTest(() async {
print('--test from ast---------------------------------------------------');
await runTests(useKernel: false);
print('--test from kernel------------------------------------------------');
await runTests(useKernel: true);
});
}