<commit_before>// Copyright (c) 2013, 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.

library message_extraction_test;

import 'package:unittest/unittest.dart';
import 'dart:io';
import 'dart:async';
import 'package:pathos/path.dart' as path;
import '../data_directory.dart';

final dart = new Options().executable;

// TODO(alanknight): We have no way of knowing what the package-root is,
// so when we're running under the test framework, which sets the
// package-root, we use a horrible hack and infer it from the executable.
final packageDir = _findPackageDir(dart);

/**
 * Find our package directory from the executable. If we seem to be running
 * from out/Release<arch>/dart or the equivalent Debug, then use the packages
 * directory under Release<arch>. Otherwise return null, indicating to use
 * the normal pub packages directory.
 */
String _findPackageDir(executable) {
  var oneUp = path.dirname(executable);
  var tail = path.basename(oneUp);
  // If we're running from test.dart, we want e.g. out/ReleaseIA32/packages
  if (tail.contains('Release') || tail.contains('Debug')) {
      return path.join(oneUp, 'packages/');
  }
  // Check for the case where we're running Release<arch>/dart-sdk/bin/dart
  // (pub bots)
  var threeUp = path.dirname(path.dirname(oneUp));
  tail = path.basename(threeUp);
  if (tail.contains('Release') || tail.contains('Debug')) {
      return path.join(threeUp, 'packages/');
  }
  // Otherwise we will rely on the normal packages directory.
  return null;
}

/** If our package root directory is set, return it as a VM argument. */
final vmArgs = (packageDir == null) ? [] : ['--package-root=$packageDir'];

/**
 * Translate a file path into this test directory, regardless of the
 * working directory.
 */
String dir([String s]) {
  if (s != null && s.startsWith("--")) { // Don't touch command-line options.
    return s;
  } else {
   return path.join(intlDirectory, 'test', 'message_extraction', s);
  }
}

main() {
  test("Test round trip message extraction, translation, code generation, "
      "and printing", () {
    deleteGeneratedFiles();
    return extractMessages(null).then((result) {
      return generateTranslationFiles(result);
    }).then((result) {
      return generateCodeFromTranslation(result);
    }).then((result) {
      return runGeneratedCode(result);
    }).then(verifyResult)
    .whenComplete(deleteGeneratedFiles);
  });
}

void deleteGeneratedFiles() {
  var files = [dir('intl_messages.json'), dir('translation_fr.json'),
      dir('foo_messages_fr.dart'), dir('foo_messages_de_DE.dart'),
      dir('translation_de_DE.json'), dir('foo_messages_all.dart')];
  files.map((name) => new File(name)).forEach((x) {
    if (x.existsSync()) x.deleteSync();});
}

/**
 * Run the process with the given list of filenames, which we assume
 * are in dir() and need to be qualified in case that's not our working
 * directory.
 */
Future<ProcessResult> run(ProcessResult previousResult, List<String> filenames)
{
  // If there's a failure in one of the sub-programs, print its output.
  if (previousResult != null) {
    if (previousResult.exitCode != 0) {
      print("Error running sub-program:");
    }
    print(previousResult.stdout);
    print(previousResult.stderr);
    print("exitCode=${previousResult.exitCode}");
  }
  var filesInTheRightDirectory = filenames.map((x) => dir(x)).toList();
  // Inject the script argument --output-dir in between the script and its
  // arguments.
  var args = []
      ..addAll(vmArgs)
      ..add(filesInTheRightDirectory.first)
      ..addAll(["--output-dir=${dir()}"])
      ..addAll(filesInTheRightDirectory.skip(1));
  var options = new ProcessOptions()
      ..stdoutEncoding=Encoding.UTF_8
      ..stderrEncoding=Encoding.UTF_8;
  var result = Process.run(dart, args);
  return result;
}

Future<ProcessResult> extractMessages(ProcessResult previousResult) => run(
    previousResult,
    ['extract_to_json.dart', '--suppress-warnings', 'sample_with_messages.dart',
        'part_of_sample_with_messages.dart']);

Future<ProcessResult> generateTranslationFiles(ProcessResult previousResult) =>
    run(
        previousResult,
        ['make_hardcoded_translation.dart', 'intl_messages.json']);

Future<ProcessResult> generateCodeFromTranslation(ProcessResult previousResult)<commit_msg>Remove (unused) ProcessOptions in test.<commit_after>// Copyright (c) 2013, 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.

library message_extraction_test;

import 'package:unittest/unittest.dart';
import 'dart:io';
import 'dart:async';
import 'package:pathos/path.dart' as path;
import '../data_directory.dart';

final dart = new Options().executable;

// TODO(alanknight): We have no way of knowing what the package-root is,
// so when we're running under the test framework, which sets the
// package-root, we use a horrible hack and infer it from the executable.
final packageDir = _findPackageDir(dart);

/**
 * Find our package directory from the executable. If we seem to be running
 * from out/Release<arch>/dart or the equivalent Debug, then use the packages
 * directory under Release<arch>. Otherwise return null, indicating to use
 * the normal pub packages directory.
 */
String _findPackageDir(executable) {
  var oneUp = path.dirname(executable);
  var tail = path.basename(oneUp);
  // If we're running from test.dart, we want e.g. out/ReleaseIA32/packages
  if (tail.contains('Release') || tail.contains('Debug')) {
      return path.join(oneUp, 'packages/');
  }
  // Check for the case where we're running Release<arch>/dart-sdk/bin/dart
  // (pub bots)
  var threeUp = path.dirname(path.dirname(oneUp));
  tail = path.basename(threeUp);
  if (tail.contains('Release') || tail.contains('Debug')) {
      return path.join(threeUp, 'packages/');
  }
  // Otherwise we will rely on the normal packages directory.
  return null;
}

/** If our package root directory is set, return it as a VM argument. */
final vmArgs = (packageDir == null) ? [] : ['--package-root=$packageDir'];

/**
 * Translate a file path into this test directory, regardless of the
 * working directory.
 */
String dir([String s]) {
  if (s != null && s.startsWith("--")) { // Don't touch command-line options.
    return s;
  } else {
   return path.join(intlDirectory, 'test', 'message_extraction', s);
  }
}

main() {
  test("Test round trip message extraction, translation, code generation, "
      "and printing", () {
    deleteGeneratedFiles();
    return extractMessages(null).then((result) {
      return generateTranslationFiles(result);
    }).then((result) {
      return generateCodeFromTranslation(result);
    }).then((result) {
      return runGeneratedCode(result);
    }).then(verifyResult)
    .whenComplete(deleteGeneratedFiles);
  });
}

void deleteGeneratedFiles() {
  var files = [dir('intl_messages.json'), dir('translation_fr.json'),
      dir('foo_messages_fr.dart'), dir('foo_messages_de_DE.dart'),
      dir('translation_de_DE.json'), dir('foo_messages_all.dart')];
  files.map((name) => new File(name)).forEach((x) {
    if (x.existsSync()) x.deleteSync();});
}

/**
 * Run the process with the given list of filenames, which we assume
 * are in dir() and need to be qualified in case that's not our working
 * directory.
 */
Future<ProcessResult> run(ProcessResult previousResult, List<String> filenames)
{
  // If there's a failure in one of the sub-programs, print its output.
  if (previousResult != null) {
    if (previousResult.exitCode != 0) {
      print("Error running sub-program:");
    }
    print(previousResult.stdout);
    print(previousResult.stderr);
    print("exitCode=${previousResult.exitCode}");
  }
  var filesInTheRightDirectory = filenames.map((x) => dir(x)).toList();
  // Inject the script argument --output-dir in between the script and its
  // arguments.
  var args = []
      ..addAll(vmArgs)
      ..add(filesInTheRightDirectory.first)
      ..addAll(["--output-dir=${dir()}"])
      ..addAll(filesInTheRightDirectory.skip(1));
  var result = Process.run(dart, args);
  return result;
}

Future<ProcessResult> extractMessages(ProcessResult previousResult) => run(
    previousResult,
    ['extract_to_json.dart', '--suppress-warnings', 'sample_with_messages.dart',
        'part_of_sample_with_messages.dart']);

Future<ProcessResult> generateTranslationFiles(ProcessResult previousResult) =>
    run(
        previousResult,
        ['make_hardcoded_translation.dart', 'intl_messages.json']);

Future<ProcessResult> generateCodeFromTranslation(ProcessResult previousResult)
{'fraction_non_alphanumeric': 0.13309922972360672, 'fraction_numerical': 0.0020389669234254643, 'mean_word_length': 4.04802744425386, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '367786', 'n_tokens_mistral': 2624, 'n_tokens_neox': 2589, 'n_words': 763}