Skip to content

Comments

remove some auto-generated error messages in "assert.throws" tests#4024

Merged
Ms2ger merged 1 commit intomainfrom
remove-autogen-error-message
Mar 25, 2024
Merged

remove some auto-generated error messages in "assert.throws" tests#4024
Ms2ger merged 1 commit intomainfrom
remove-autogen-error-message

Conversation

@bakkot
Copy link
Member

@bakkot bakkot commented Mar 23, 2024

Per #4017 (comment), this (automatically) rewrites a bunch of tests to remove auto-generated error messages in various assert.throws tests.

There's still a handful left but this deals with most of them.

rewrite-test262.mjs
import fs from 'node:fs';
import path from 'node:path';

if (process.argv.length !== 3) {
  console.log('Usage: node rewrite-test262.mjs path-to-test262');
  process.exit(1);
}

let dir = process.argv[2];

let matches = 0;
let files = [...fs.readdirSync(dir, { recursive: true, withFileTypes: true })];
for (let i = 0; i < files.length; ++i) {
  let file = files[i];
  if (!file.isFile()) continue;
  let fname = path.join(file.parentPath, file.name);
  let content = fs.readFileSync(fname, 'utf8');

  let regexpSrc = String.raw`
(?<main>assert.throws\( (?<kind>[A-Za-z0-9]+), (function \(\)|\(\) =>) \{
  (?<code>[^}]*|.*)
\})\, "\`(?<code2>[^\`]+)\` throws \k<kind>" \);
`
    .trim()
    .replace(/\\`/g, '`')
    .replace(/\s+/g, '\\s*')
    .replace(/"/g, '["\']');
  let regexp = new RegExp(regexpSrc);

  let match = regexp.exec(content);
  if (match) {
    let { groups } = match;
    let { code, code2} = groups;
    code = code.trim().replace(/\s+/g, '');
    code2 = code2.trim().replace(/\\'/g, "'").replace(/\s+/g, '');
    if (code.endsWith(';')) code = code.slice(0, -1);
    if (code2 !== code) continue;

    ++matches;

    let replacement = `${groups.main});`
    content = content.slice(0, match.index) + replacement + content.slice(match.index + match[0].length);

    fs.writeFileSync(fname, content, 'utf8');
    --i; // re-run on this file in case there was more than one
  }
}
console.log(`rewrote ${matches} instances`);

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

certainly we don't need an error message that just replicates the code that throws :-)

@Ms2ger Ms2ger force-pushed the remove-autogen-error-message branch from 2d73c0f to 7b5e31a Compare March 25, 2024 11:05
@Ms2ger Ms2ger merged commit 961480a into main Mar 25, 2024
@Ms2ger Ms2ger deleted the remove-autogen-error-message branch March 25, 2024 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants