<commit_before>const { installForTests } = require('./esbuild');
const childProcess = require('child_process');
const assert = require('assert');
const path = require('path');
const fs = require('fs');

const repoDir = path.dirname(__dirname);
const testDir = path.join(repoDir, 'scripts', '.uglify-tests');
const uglifyDir = path.join(repoDir, 'demo', 'uglify');
let U;

main().catch(e => setTimeout(() => { throw e }));

async function main() {
  // // Terser's stdout comparisons fail if this is true since stdout contains
  // // terminal color escape codes
  // process.stdout.isTTY = false;

  // Make sure the tests are installed
  console.log('Downloading uglify...');
  childProcess.execSync('make demo/uglify', { cwd: repoDir, stdio: 'pipe' });
  U = require(path.join(uglifyDir, 'test', 'node'));

  // Start the esbuild service
  const esbuild = installForTests(testDir);
  const service = await esbuild.startService();

  // Find test files
  const compressDir = path.join(uglifyDir, 'test', 'compress');
  const files = fs.readdirSync(compressDir).filter(name => name.endsWith('.js'));

  // Run all tests concurrently
  let passedTotal = 0;
  let failedTotal = 0;
  const runTest = file => test_file(service, path.join(compressDir, file))
    .then(({ passed, failed }) => {
      passedTotal += passed;
      failedTotal += failed;
    });
  await Promise.all(files.map(runTest));

  // Clean up test output
  service.stop();
  childProcess.execSync(`rm -fr "${testDir}"`);

  console.log(`${failedTotal} failed out of ${passedTotal + failedTotal}`);
  if (failedTotal) {
    process.exit(1);
  }
}

async function test_file(service, file) {
  let passed = 0;
  let failed = 0;
  const tests = parse_test(file);
  const runTest = name => test_case(service, tests[name])
    .then(() => passed++)
    .catch(e => {
      failed++;
      console.error(`❌ ${file}: ${name}: ${(e && e.message || e).trim()}\n`);
      pass = false;
    });
  await Promise.all(Object.keys(tests).map(runTest));
  return { passed, failed };
}

// Modified from "uglify/demo/test/compress.js"
async function test_case(service, test) {
  const sandbox = require(path.join(uglifyDir, 'test', 'sandbox'));
  const log = (format, args) => { throw new Error(tmpl(format, args)); };

  var semver = require(path.join(uglifyDir, 'node_modules', 'semver'));

  // Generate the input code
  var input = to_toplevel(test.input, test.mangle);
  var input_code = make_code(input);
  var input_formatted = make_code(test.input, {
    beautify: true,
    comments: "all",
    keep_quoted_props: true,
    quote_style: 3,
  });

  // Make sure it's valid
  try {
    U.parse(input_code);
  } catch (ex) {
    log([
      "!!! Cannot parse input",
      "---INPUT---",
      "{input}",
      "--PARSE ERROR--",
      "{error}",
      "",
      "",
    ].join("\n"), {
      input: input_formatted,
      error: ex,
    });
  }

  // Run esbuild as a minifier
  try {
    var { js: output } = await service.transform(input_code, {
      minify: true,
      target: 'es5',
    });
  } catch (e) {
    const formatError = ({ text, location }) => {
      if (!location) return `\nerror: ${text}`;
      const { file, line, column } = location;
      return `\n${file}:${line}:${column}: error: ${text}`;
    }
    log("!!! esbuild failed\n---INPUT---\n{input}\n---ERROR---\n{error}\n", {<commit_msg>disable uglify tests that aren't applicable<commit_after>const { installForTests } = require('./esbuild');
const childProcess = require('child_process');
const assert = require('assert');
const path = require('path');
const fs = require('fs');

const repoDir = path.dirname(__dirname);
const testDir = path.join(repoDir, 'scripts', '.uglify-tests');
const uglifyDir = path.join(repoDir, 'demo', 'uglify');
let U;

main().catch(e => setTimeout(() => { throw e }));

async function main() {
  // // Terser's stdout comparisons fail if this is true since stdout contains
  // // terminal color escape codes
  // process.stdout.isTTY = false;

  // Make sure the tests are installed
  childProcess.execSync('make demo/uglify', { cwd: repoDir, stdio: 'pipe' });
  U = require(path.join(uglifyDir, 'test', 'node'));

  // Start the esbuild service
  const esbuild = installForTests(testDir);
  const service = await esbuild.startService();

  // Find test files
  const compressDir = path.join(uglifyDir, 'test', 'compress');
  const files = fs.readdirSync(compressDir).filter(name => name.endsWith('.js'));

  // Run all tests concurrently
  let passedTotal = 0;
  let failedTotal = 0;
  const runTest = file => test_file(service, path.join(compressDir, file))
    .then(({ passed, failed }) => {
      passedTotal += passed;
      failedTotal += failed;
    });
  await Promise.all(files.map(runTest));

  // Clean up test output
  service.stop();
  childProcess.execSync(`rm -fr "${testDir}"`);

  console.log(`${failedTotal} failed out of ${passedTotal + failedTotal}`);
  if (failedTotal) {
    process.exit(1);
  }
}

async function test_file(service, file) {
  let passed = 0;
  let failed = 0;
  const tests = parse_test(file);
  const runTest = name => test_case(service, tests[name])
    .then(() => passed++)
    .catch(e => {
      failed++;
      console.error(`❌ ${file}: ${name}: ${(e && e.message || e).trim()}\n`);
      pass = false;
    });
  await Promise.all(Object.keys(tests).map(runTest));
  return { passed, failed };
}

// Modified from "uglify/demo/test/compress.js"
async function test_case(service, test) {
  const sandbox = require(path.join(uglifyDir, 'test', 'sandbox'));
  const log = (format, args) => { throw new Error(tmpl(format, args)); };

  var semver = require(path.join(uglifyDir, 'node_modules', 'semver'));

  // Generate the input code
  var input = to_toplevel(test.input, test.mangle);
  var input_code = make_code(input);
  var input_formatted = make_code(test.input, {
    beautify: true,
    comments: "all",
    keep_quoted_props: true,
    quote_style: 3,
  });

  // Make sure it's valid
  try {
    U.parse(input_code);
  } catch (ex) {
    log([
      "!!! Cannot parse input",
      "---INPUT---",
      "{input}",
      "--PARSE ERROR--",
      "{error}",
      "",
      "",
    ].join("\n"), {
      input: input_formatted,
      error: ex,
    });
  }

  // Ignore tests that no longer pass in modern versions of node. These tests
  // contain code that is now considered a syntax error. The relevant code is
  // this:
  //
  //   try{throw 42}catch(a){console.log(a);function a(){}}
  //
  if (test.node_version && !semver.satisfies(process.version, test.node_version)) {
    console.error("*** skipping test %j with node_version %j", test.name, test.node_version);
    return;
  }

  // Run esbuild as a minifier
  try {
    var { js: output } = await service.transform(input_code, {
      minify: true,
      target: 'es5',
    });
  } catch (e) {
    // These two tests fail because they contain setters without arguments,
    // which is a syntax error. These test failures do not indicate anything
    // wrong with esbuild so the failures are ignored. Here is one of the
    // tests:
    //
    //   function f(){var a={get b(){},set b(){}};return{a:a}}
    //
    if (test.name === 'unsafe_object_accessor' || test.name === 'keep_name_of_setter') {
      console.error("*** skipping test with known syntax error:", test.name);
      return;
    }

    const formatError = ({ text, location }) => {
      if (!location) return `\nerror: ${text}`;
      const { file, line, column } = location;
      return `\n${file}:${line}:${column}: error: ${text}`;
    }
    log("!!! esbuild failed\n---INPUT---\n{input}\n---ERROR---\n{error}\n", {
{'fraction_non_alphanumeric': 0.17231491136600627, 'fraction_numerical': 0.0020855057351407717, 'mean_word_length': 3.215934065934066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2230346', 'n_tokens_mistral': 2507, 'n_tokens_neox': 2482, 'n_words': 662}