<commit_before>const fs = require('fs');
const path = require('path');
const unidiff = require('unidiff')

function check(file, updateGolden = false) {
  // Strip comments from generated file for comparison to golden
  // to make comparison less brittle
  const actual = require.resolve(path.posix.join('fine_grained_goldens', file));
  const actualContents =
      fs.readFileSync(actual, {encoding: 'utf-8'})
          .replace(/\r\n/g, '\n')
          .split('\n')
          // Remove all comments for the comparison
          .filter(l => !l.trimLeft().startsWith('#'))
          // Remove .cmd files for the comparison since they only exist on Windows
          .filter(l => !l.endsWith('.cmd",'))
          .join('\n')
          .replace(/[\n]+/g, '\n');

  // Load the golden file for comparison
  const golden = path.posix.join(path.dirname(__filename), 'golden', file + '.golden');

  if (updateGolden) {
    // Write to golden file
    // TODO(kyliau): Consider calling mkdirp() here, otherwise write operation
    // would fail if directory does not already exist.
    fs.writeFileSync(golden, actualContents);
    console.error(`Replaced ${path.join(process.cwd(), golden)}`);
  } else {
    const goldenContents = fs.readFileSync(golden, {encoding: 'utf-8'}).replace(/\r\n/g, '\n');
    // Check if actualContents matches golden file
    if (actualContents !== goldenContents) {
      // Generated does not match golden
      const diff = unidiff.diffLines(goldenContents, actualContents);
      const prettyDiff = unidiff.formatLines(diff);
      throw new Error(`Actual output in ${file} doesn't match golden file ${golden}.

Diff:
${prettyDiff}

Update the golden file:

      bazel run ${process.env['BAZEL_TARGET']}.accept`);
    }
  }
}

module.exports = {
  check,
  files: [
    'BUILD.bazel',
    'install_bazel_dependencies.bzl',
    'manual_build_file_contents',<commit_msg>test: npm_install golden test does not work on windows<commit_after>const fs = require('fs');
const path = require('path');
const unidiff = require('unidiff')

function check(file, updateGolden = false) {
  // Strip comments from generated file for comparison to golden
  // to make comparison less brittle
  const actual = require.resolve(path.posix.join('fine_grained_goldens', file));
  const actualContents =
      fs.readFileSync(actual, {encoding: 'utf-8'})
          .replace(/\r\n/g, '\n')
          .split('\n')
          // Remove all comments for the comparison
          .filter(l => !l.trimLeft().startsWith('#'))
          // Remove .cmd files for the comparison since they only exist on Windows
          .filter(l => !l.endsWith('.cmd",'))
          .join('\n')
          .replace(/[\n]+/g, '\n');

  // Load the golden file for comparison
  const golden = require.resolve('./golden/' + file + '.golden');

  if (updateGolden) {
    // Write to golden file
    // TODO(kyliau): Consider calling mkdirp() here, otherwise write operation
    // would fail if directory does not already exist.
    fs.writeFileSync(golden, actualContents);
    console.error(`Replaced ${path.join(golden)}`);
  } else {
    const goldenContents = fs.readFileSync(golden, {encoding: 'utf-8'}).replace(/\r\n/g, '\n');
    // Check if actualContents matches golden file
    if (actualContents !== goldenContents) {
      // Generated does not match golden
      const diff = unidiff.diffLines(goldenContents, actualContents);
      const prettyDiff = unidiff.formatLines(diff);
      throw new Error(`Actual output in ${file} doesn't match golden file ${golden}.

Diff:
${prettyDiff}

Update the golden file:

      bazel run ${process.env['BAZEL_TARGET']}.accept`);
    }
  }
}

module.exports = {
  check,
  files: [
    'BUILD.bazel',
    'install_bazel_dependencies.bzl',
    'manual_build_file_contents',
{'fraction_non_alphanumeric': 0.1418421052631579, 'fraction_numerical': 0.0010526315789473684, 'mean_word_length': 3.131521739130435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2536197', 'n_tokens_mistral': 1150, 'n_tokens_neox': 1130, 'n_words': 319}