<commit_before>// 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:expect/expect.dart';
import 'package:path/path.dart' as path;

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();
  }
}

Future launchDart2Js(args) {
  String executable = Platform.executable;
  String command = path.join(path.dirname(executable), 'dart2js');
  String dart2jsPath = path.normalize(
      path.join(path.fromUri(Platform.script),
        '../../../../pkg/compiler/lib/src/dart2js.dart'));
  List allArgs = ['--package-root=${Platform.packageRoot}',
       dart2jsPath]..addAll(args);
  return Process.run(Platform.executable, allArgs, stdoutEncoding: null);
}

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() async {
  String inFilePath =
      pathOfData.resolve('data/one_line_dart_program.dart').path;
  List<String> args = [inFilePath, "--out=" + outFilePath];

  await cleanup();
  check(await launchDart2Js(args));
  await cleanup();
}

Future serverRunning(HttpServer server) 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];<commit_msg>Additional fixes to zero_termination_test<commit_after>// 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:expect/expect.dart';
import 'package:path/path.dart' as path;

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();
  }
}

Future launchDart2Js(args) {
  String dart2jsPath = path.normalize(
      path.join(path.fromUri(Platform.script),
        '../../../../pkg/compiler/lib/src/dart2js.dart'));
  List allArgs = [];
  if (Platform.packageRoot != null) {
    allArgs.add('--package-root=${Platform.packageRoot}');
  } else if (Platform.packageConfig != null) {
    allArgs.add('--packages=${Platform.packageConfig}');
  }
  allArgs.add(dart2jsPath);
  allArgs.addAll(args);
  return Process.run(Platform.executable, allArgs, stdoutEncoding: null);
}

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() async {
  String inFilePath =
      pathOfData.resolve('data/one_line_dart_program.dart').path;
  List<String> args = [inFilePath, "--out=" + outFilePath];

  await cleanup();
  check(await launchDart2Js(args));
  await cleanup();
}

Future serverRunning(HttpServer server) 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];
{'fraction_non_alphanumeric': 0.1354336411370632, 'fraction_numerical': 0.011768966141589716, 'mean_word_length': 4.12430426716141, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5038248', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1716, 'n_words': 436}