mirror of
https://github.com/nodejs/node.git
synced 2025-12-28 07:50:41 +00:00
test: use RegExp.escape to improve test reliability
PR-URL: https://github.com/nodejs/node/pull/60803 Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
d255fc7bae
commit
8dc2bddbd5
@ -109,7 +109,7 @@ function replaceTestDuration(str) {
|
||||
|
||||
const root = path.resolve(__dirname, '..', '..');
|
||||
const color = '(\\[\\d+m)';
|
||||
const stackTraceBasePath = new RegExp(`${color}\\(${root.replaceAll(/[\\^$*+?.()|[\]{}]/g, '\\$&')}/?${color}(.*)${color}\\)`, 'g');
|
||||
const stackTraceBasePath = new RegExp(`${color}\\(${RegExp.escape(root)}/?${color}(.*)${color}\\)`, 'g');
|
||||
|
||||
function replaceSpecDuration(str) {
|
||||
return str
|
||||
|
||||
@ -112,7 +112,7 @@ for (const [algorithm, overrides, expected] of good) {
|
||||
for (const [algorithm, overrides, param] of bad) {
|
||||
const expected = {
|
||||
code: 'ERR_OUT_OF_RANGE',
|
||||
message: new RegExp(`The value of "${param}" is out of range`),
|
||||
message: new RegExp(`The value of "${RegExp.escape(param)}" is out of range`),
|
||||
};
|
||||
const parameters = { ...defaults, ...overrides };
|
||||
assert.throws(() => crypto.argon2(algorithm, parameters, () => {}), expected);
|
||||
@ -122,7 +122,7 @@ for (const [algorithm, overrides, param] of bad) {
|
||||
for (const key of Object.keys(defaults)) {
|
||||
const expected = {
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
message: new RegExp(`"parameters\\.${key}"`),
|
||||
message: new RegExp(`"parameters\\.${RegExp.escape(key)}"`),
|
||||
};
|
||||
const parameters = { ...defaults };
|
||||
delete parameters[key];
|
||||
|
||||
@ -296,7 +296,7 @@ oans248kpal88CGqsN2so/wZKxVnpiXlPHMdiNL7hRSUqlHkUi07FrP2Htg8kjI=
|
||||
'OCSP - URI': ['http://ocsp.nodejs.org/'],
|
||||
'CA Issuers - URI': ['http://ca.nodejs.org/ca.cert']
|
||||
}),
|
||||
modulusPattern: new RegExp(`^${modulusOSSL}$`, 'i'),
|
||||
modulusPattern: new RegExp(`^${RegExp.escape(modulusOSSL)}$`, 'i'),
|
||||
bits: 2048,
|
||||
exponent: '0x10001',
|
||||
valid_from: 'Sep 3 21:40:37 2022 GMT',
|
||||
|
||||
@ -54,7 +54,7 @@ if (process.argv[2] === 'child') {
|
||||
child_process.execFileSync(testExecPath, [ __filename, 'child' ],
|
||||
{ encoding: 'utf8', env: env });
|
||||
},
|
||||
new RegExp(`Cannot find module '${pkgName}'`));
|
||||
new RegExp(`Cannot find module '${RegExp.escape(pkgName)}'`));
|
||||
|
||||
// Test module in $HOME/.node_modules.
|
||||
const modHomeDir = path.join(testFixturesDir, 'home-pkg-in-node_modules');
|
||||
|
||||
@ -21,6 +21,6 @@ for (const flag of warnFlags) {
|
||||
]
|
||||
);
|
||||
|
||||
assert.match(stderr.toString(), new RegExp(`SecurityWarning: The flag ${flag} must be used with extreme caution`));
|
||||
assert.match(stderr.toString(), new RegExp(`SecurityWarning: The flag ${RegExp.escape(flag)} must be used with extreme caution`));
|
||||
assert.strictEqual(status, 0);
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@ const cliMd = path.join(rootDir, 'doc', 'api', 'cli.md');
|
||||
const cliText = fs.readFileSync(cliMd, { encoding: 'utf8' });
|
||||
|
||||
const parseSection = (text, startMarker, endMarker) => {
|
||||
const regExp = new RegExp(`${startMarker}\r?\n([^]*)\r?\n${endMarker}`);
|
||||
const regExp = new RegExp(`${RegExp.escape(startMarker)}\r?\n([^]*)\r?\n${RegExp.escape(endMarker)}`);
|
||||
const match = text.match(regExp);
|
||||
assert(match,
|
||||
`Unable to locate text between '${startMarker}' and '${endMarker}'.`);
|
||||
|
||||
@ -148,7 +148,7 @@ for (const { key, valid, invalid } of cases) {
|
||||
const options = {};
|
||||
options[key] = value;
|
||||
throws(() => new QuicEndpoint(options), {
|
||||
message: new RegExp(`${key}`),
|
||||
message: new RegExp(`${RegExp.escape(key)}`),
|
||||
}, value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const getDefine = (text, name) => {
|
||||
const regexp = new RegExp(`#define\\s+${name}\\s+(.*)`);
|
||||
const regexp = new RegExp(`#define\\s+${RegExp.escape(name)}\\s+(.*)`);
|
||||
const match = regexp.exec(text);
|
||||
assert.notStrictEqual(match, null);
|
||||
return match[1];
|
||||
@ -27,7 +27,7 @@ if (!release) {
|
||||
const major = getDefine(versionText, 'NODE_MAJOR_VERSION');
|
||||
const minor = getDefine(versionText, 'NODE_MINOR_VERSION');
|
||||
const patch = getDefine(versionText, 'NODE_PATCH_VERSION');
|
||||
const versionForRegex = `${major}\\.${minor}\\.${patch}`;
|
||||
const versionForRegex = RegExp.escape(`${major}.${minor}.${patch}`);
|
||||
|
||||
const lts = getDefine(versionText, 'NODE_VERSION_IS_LTS') !== '0';
|
||||
const codename = getDefine(versionText, 'NODE_VERSION_LTS_CODENAME').slice(1, -1);
|
||||
@ -45,7 +45,7 @@ const changelogPath = `doc/changelogs/CHANGELOG_V${major}.md`;
|
||||
// Check table header
|
||||
let tableHeader;
|
||||
if (lts) {
|
||||
tableHeader = new RegExp(`<th>LTS '${codename}'</th>`);
|
||||
tableHeader = new RegExp(`<th>LTS '${RegExp.escape(codename)}'</th>`);
|
||||
} else {
|
||||
tableHeader = /<th>Current<\/th>/;
|
||||
}
|
||||
@ -57,7 +57,7 @@ const changelogPath = `doc/changelogs/CHANGELOG_V${major}.md`;
|
||||
// Check title for changelog entry.
|
||||
let title;
|
||||
if (lts) {
|
||||
title = new RegExp(`## \\d{4}-\\d{2}-\\d{2}, Version ${versionForRegex} '${codename}' \\(LTS\\), @\\S+`);
|
||||
title = new RegExp(`## \\d{4}-\\d{2}-\\d{2}, Version ${versionForRegex} '${RegExp.escape(codename)}' \\(LTS\\), @\\S+`);
|
||||
} else {
|
||||
title = new RegExp(`## \\d{4}-\\d{2}-\\d{2}, Version ${versionForRegex} \\(Current\\), @\\S+`);
|
||||
}
|
||||
@ -70,20 +70,20 @@ const changelogPath = `doc/changelogs/CHANGELOG_V${major}.md`;
|
||||
// Check for the link to the appropriate CHANGELOG_V*.md file.
|
||||
let linkToChangelog;
|
||||
if (lts) {
|
||||
linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${changelogPath}\\) \\*\\*Long Term Support\\*\\*`);
|
||||
linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${RegExp.escape(changelogPath)}\\) \\*\\*Long Term Support\\*\\*`);
|
||||
} else {
|
||||
linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${changelogPath}\\) \\*\\*Current\\*\\*`);
|
||||
linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${RegExp.escape(changelogPath)}\\) \\*\\*Current\\*\\*`);
|
||||
}
|
||||
assert.match(mainChangelog, linkToChangelog);
|
||||
// Check table header.
|
||||
let tableHeader;
|
||||
if (lts) {
|
||||
tableHeader = new RegExp(`<th title="LTS Until \\d{4}-\\d{2}"><a href="${changelogPath}">${major}</a> \\(LTS\\)</th>`);
|
||||
tableHeader = new RegExp(`<th title="LTS Until \\d{4}-\\d{2}"><a href="${RegExp.escape(changelogPath)}">${major}</a> \\(LTS\\)</th>`);
|
||||
} else {
|
||||
tableHeader = new RegExp(`<th title="Current"><a href="${changelogPath}">${major}</a> \\(Current\\)</th>`);
|
||||
tableHeader = new RegExp(`<th title="Current"><a href="${RegExp.escape(changelogPath)}">${major}</a> \\(Current\\)</th>`);
|
||||
}
|
||||
assert.match(mainChangelog, tableHeader);
|
||||
// Check the table contains a link to the release in the appropriate CHANGELOG_V*.md file.
|
||||
const linkToVersion = new RegExp(`<b><a href="${changelogPath}#${versionForRegex}">${versionForRegex}</a></b><br/>`);
|
||||
const linkToVersion = new RegExp(`<b><a href="${RegExp.escape(changelogPath)}#${versionForRegex}">${versionForRegex}</a></b><br/>`);
|
||||
assert.match(mainChangelog, linkToVersion);
|
||||
}
|
||||
|
||||
@ -45,7 +45,7 @@ describe('with previews', () => {
|
||||
);
|
||||
const lines = getSingleCommandLines(output);
|
||||
assert.match(lines.command, /^'Hello custom' \+ ' eval World!'/);
|
||||
assert.match(lines.prompt, new RegExp(`${testingReplPrompt}$`));
|
||||
assert.match(lines.prompt, new RegExp(`${RegExp.escape(testingReplPrompt)}$`));
|
||||
assert.strictEqual(lines.result, "'Hello custom eval World!'");
|
||||
assert.strictEqual(lines.preview, undefined);
|
||||
});
|
||||
@ -62,7 +62,7 @@ describe('with previews', () => {
|
||||
);
|
||||
const lines = getSingleCommandLines(output);
|
||||
assert.match(lines.command, /^'Hello custom' \+ ' eval World!'/);
|
||||
assert.match(lines.prompt, new RegExp(`${testingReplPrompt}$`));
|
||||
assert.match(lines.prompt, new RegExp(`${RegExp.escape(testingReplPrompt)}$`));
|
||||
assert.strictEqual(lines.result, "'Hello custom eval World!'");
|
||||
assert.match(lines.preview, /'Hello custom eval World!'/);
|
||||
});
|
||||
|
||||
@ -2912,11 +2912,10 @@ assert.strictEqual(
|
||||
frame.replaceAll('/', '\\'))
|
||||
).join('\n');
|
||||
}
|
||||
const escapedCWD = util.inspect(process.cwd()).slice(1, -1);
|
||||
util.inspect(err, { colors: true }).split('\n').forEach(common.mustCallAtLeast((line, i) => {
|
||||
let expected = stack[i].replace(/node_modules\/(@[^/]+\/[^/]+|[^/]+)/gi, (_, m) => {
|
||||
return `node_modules/\u001b[4m${m}\u001b[24m`;
|
||||
}).replaceAll(new RegExp(`(\\(?${escapedCWD}(\\\\|/))`, 'gi'), (_, m) => {
|
||||
}).replaceAll(new RegExp(`(\\(?${RegExp.escape(process.cwd())}(\\\\|/))`, 'gi'), (_, m) => {
|
||||
return `\x1B[90m${m}\x1B[39m`;
|
||||
});
|
||||
if (expected.includes(process.cwd()) && expected.endsWith(')')) {
|
||||
|
||||
@ -63,5 +63,5 @@ child.on('message', (msg) => {
|
||||
|
||||
await once(child, 'exit');
|
||||
|
||||
assert.match(stdout, new RegExp(`__SIGTERM received__ ${firstGrandchildPid}`));
|
||||
assert.doesNotMatch(stdout, new RegExp(`__SIGINT received__ ${firstGrandchildPid}`));
|
||||
assert.match(stdout, new RegExp(`__SIGTERM received__ ${RegExp.escape(firstGrandchildPid)}`));
|
||||
assert.doesNotMatch(stdout, new RegExp(`__SIGINT received__ ${RegExp.escape(firstGrandchildPid)}`));
|
||||
|
||||
@ -67,5 +67,5 @@ await once(child, 'exit');
|
||||
// The second grandchild, if there is one, could receive SIGTERM if it's killed as a
|
||||
// consequence of the parent being killed in this process instead of being killed by the
|
||||
// parent for file changes. Here we only care about the first grandchild.
|
||||
assert.match(stdout, new RegExp(`__SIGINT received__ ${firstGrandchildPid}`));
|
||||
assert.doesNotMatch(stdout, new RegExp(`__SIGTERM received__ ${firstGrandchildPid}`));
|
||||
assert.match(stdout, new RegExp(`__SIGINT received__ ${RegExp.escape(firstGrandchildPid)}`));
|
||||
assert.doesNotMatch(stdout, new RegExp(`__SIGTERM received__ ${RegExp.escape(firstGrandchildPid)}`));
|
||||
|
||||
@ -50,7 +50,7 @@ const kEuro = Buffer.from([0xe2, 0x82, 0xac]).toString();
|
||||
() => Reflect.get(TextDecoderStream.prototype, getter, {}), {
|
||||
name: 'TypeError',
|
||||
message: /Cannot read private member/,
|
||||
stack: new RegExp(`at get ${getter}`)
|
||||
stack: new RegExp(`at get ${RegExp.escape(getter)}`)
|
||||
}
|
||||
);
|
||||
});
|
||||
@ -79,7 +79,7 @@ const kEuro = Buffer.from([0xe2, 0x82, 0xac]).toString();
|
||||
() => Reflect.get(TextDecoderStream.prototype, getter, {}), {
|
||||
name: 'TypeError',
|
||||
message: /Cannot read private member/,
|
||||
stack: new RegExp(`at get ${getter}`)
|
||||
stack: new RegExp(`at get ${RegExp.escape(getter)}`)
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user