GeezerVison is showing github.com — test: use `RegExp.escape` to improve test reliability · nodejs/node@edf90ce
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: test/common/assertSnapshot.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -109,7 +109,7 @@ function replaceTestDuration(str) { | 109 | 109 |
| 110 | 110 | const root = path.resolve(__dirname, '..', '..'); | 111 | 111 | const color = '(\\[\\d+m)'; | 112 | | -const stackTraceBasePath = new RegExp(`${color}\\(${root.replaceAll(/[\\^$*+?.()|[\]{}]/g, '\\$&')}/?${color}(.*)${color}\\)`, 'g'); | | 112 | +const stackTraceBasePath = new RegExp(`${color}\\(${RegExp.escape(root)}/?${color}(.*)${color}\\)`, 'g'); | 113 | 113 |
| 114 | 114 | function replaceSpecDuration(str) { | 115 | 115 | return str | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-argon2.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -112,7 +112,7 @@ for (const [algorithm, overrides, expected] of good) { | 112 | 112 | for (const [algorithm, overrides, param] of bad) { | 113 | 113 | const expected = { | 114 | 114 | code: 'ERR_OUT_OF_RANGE', | 115 | | - message: new RegExp(`The value of "${param}" is out of range`), | | 115 | + message: new RegExp(`The value of "${RegExp.escape(param)}" is out of range`), | 116 | 116 | }; | 117 | 117 | const parameters = { ...defaults, ...overrides }; | 118 | 118 | assert.throws(() => crypto.argon2(algorithm, parameters, () => {}), expected); | @@ -122,7 +122,7 @@ for (const [algorithm, overrides, param] of bad) { | 122 | 122 | for (const key of Object.keys(defaults)) { | 123 | 123 | const expected = { | 124 | 124 | code: 'ERR_INVALID_ARG_TYPE', | 125 | | - message: new RegExp(`"parameters\\.${key}"`), | | 125 | + message: new RegExp(`"parameters\\.${RegExp.escape(key)}"`), | 126 | 126 | }; | 127 | 127 | const parameters = { ...defaults }; | 128 | 128 | delete parameters[key]; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-x509.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -296,7 +296,7 @@ oans248kpal88CGqsN2so/wZKxVnpiXlPHMdiNL7hRSUqlHkUi07FrP2Htg8kjI= | 296 | 296 | 'OCSP - URI': ['http://ocsp.nodejs.org/'], | 297 | 297 | 'CA Issuers - URI': ['http://ca.nodejs.org/ca.cert'] | 298 | 298 | }), | 299 | | - modulusPattern: new RegExp(`^${modulusOSSL}$`, 'i'), | | 299 | + modulusPattern: new RegExp(`^${RegExp.escape(modulusOSSL)}$`, 'i'), | 300 | 300 | bits: 2048, | 301 | 301 | exponent: '0x10001', | 302 | 302 | valid_from: 'Sep 3 21:40:37 2022 GMT', | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-module-loading-globalpaths.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,7 +54,7 @@ if (process.argv[2] === 'child') { | 54 | 54 | child_process.execFileSync(testExecPath, [ __filename, 'child' ], | 55 | 55 | { encoding: 'utf8', env: env }); | 56 | 56 | }, | 57 | | - new RegExp(`Cannot find module '${pkgName}'`)); | | 57 | + new RegExp(`Cannot find module '${RegExp.escape(pkgName)}'`)); | 58 | 58 |
| 59 | 59 | // Test module in $HOME/.node_modules. | 60 | 60 | const modHomeDir = path.join(testFixturesDir, 'home-pkg-in-node_modules'); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-permission-warning-flags.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,6 @@ for (const flag of warnFlags) { | 21 | 21 | ] | 22 | 22 | ); | 23 | 23 |
| 24 | | - assert.match(stderr.toString(), new RegExp(`SecurityWarning: The flag ${flag} must be used with extreme caution`)); | | 24 | + assert.match(stderr.toString(), new RegExp(`SecurityWarning: The flag ${RegExp.escape(flag)} must be used with extreme caution`)); | 25 | 25 | assert.strictEqual(status, 0); | 26 | 26 | } |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-process-env-allowed-flags-are-documented.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,7 +12,7 @@ const cliMd = path.join(rootDir, 'doc', 'api', 'cli.md'); | 12 | 12 | const cliText = fs.readFileSync(cliMd, { encoding: 'utf8' }); | 13 | 13 |
| 14 | 14 | const parseSection = (text, startMarker, endMarker) => { | 15 | | - const regExp = new RegExp(`${startMarker}\r?\n([^]*)\r?\n${endMarker}`); | | 15 | + const regExp = new RegExp(`${RegExp.escape(startMarker)}\r?\n([^]*)\r?\n${RegExp.escape(endMarker)}`); | 16 | 16 | const match = text.match(regExp); | 17 | 17 | assert(match, | 18 | 18 | `Unable to locate text between '${startMarker}' and '${endMarker}'.`); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-quic-internal-endpoint-options.mjs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -148,7 +148,7 @@ for (const { key, valid, invalid } of cases) { | 148 | 148 | const options = {}; | 149 | 149 | options[key] = value; | 150 | 150 | throws(() => new QuicEndpoint(options), { | 151 | | - message: new RegExp(`${key}`), | | 151 | + message: new RegExp(`${RegExp.escape(key)}`), | 152 | 152 | }, value); | 153 | 153 | } | 154 | 154 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-release-changelog.js +9-9Lines changed: 9 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,7 +8,7 @@ const fs = require('fs'); | 8 | 8 | const path = require('path'); | 9 | 9 |
| 10 | 10 | const getDefine = (text, name) => { | 11 | | - const regexp = new RegExp(`#define\\s+${name}\\s+(.*)`); | | 11 | + const regexp = new RegExp(`#define\\s+${RegExp.escape(name)}\\s+(.*)`); | 12 | 12 | const match = regexp.exec(text); | 13 | 13 | assert.notStrictEqual(match, null); | 14 | 14 | return match[1]; | @@ -27,7 +27,7 @@ if (!release) { | 27 | 27 | const major = getDefine(versionText, 'NODE_MAJOR_VERSION'); | 28 | 28 | const minor = getDefine(versionText, 'NODE_MINOR_VERSION'); | 29 | 29 | const patch = getDefine(versionText, 'NODE_PATCH_VERSION'); | 30 | | -const versionForRegex = `${major}\\.${minor}\\.${patch}`; | | 30 | +const versionForRegex = RegExp.escape(`${major}.${minor}.${patch}`); | 31 | 31 |
| 32 | 32 | const lts = getDefine(versionText, 'NODE_VERSION_IS_LTS') !== '0'; | 33 | 33 | const codename = getDefine(versionText, 'NODE_VERSION_LTS_CODENAME').slice(1, -1); | @@ -45,7 +45,7 @@ const changelogPath = `doc/changelogs/CHANGELOG_V${major}.md`; | 45 | 45 | // Check table header | 46 | 46 | let tableHeader; | 47 | 47 | if (lts) { | 48 | | - tableHeader = new RegExp(`<th>LTS '${codename}'</th>`); | | 48 | + tableHeader = new RegExp(`<th>LTS '${RegExp.escape(codename)}'</th>`); | 49 | 49 | } else { | 50 | 50 | tableHeader = /<th>Current<\/th>/; | 51 | 51 | } | @@ -57,7 +57,7 @@ const changelogPath = `doc/changelogs/CHANGELOG_V${major}.md`; | 57 | 57 | // Check title for changelog entry. | 58 | 58 | let title; | 59 | 59 | if (lts) { | 60 | | - title = new RegExp(`## \\d{4}-\\d{2}-\\d{2}, Version ${versionForRegex} '${codename}' \\(LTS\\), @\\S+`); | | 60 | + title = new RegExp(`## \\d{4}-\\d{2}-\\d{2}, Version ${versionForRegex} '${RegExp.escape(codename)}' \\(LTS\\), @\\S+`); | 61 | 61 | } else { | 62 | 62 | title = new RegExp(`## \\d{4}-\\d{2}-\\d{2}, Version ${versionForRegex} \\(Current\\), @\\S+`); | 63 | 63 | } | @@ -70,20 +70,20 @@ const changelogPath = `doc/changelogs/CHANGELOG_V${major}.md`; | 70 | 70 | // Check for the link to the appropriate CHANGELOG_V*.md file. | 71 | 71 | let linkToChangelog; | 72 | 72 | if (lts) { | 73 | | - linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${changelogPath}\\) \\*\\*Long Term Support\\*\\*`); | | 73 | + linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${RegExp.escape(changelogPath)}\\) \\*\\*Long Term Support\\*\\*`); | 74 | 74 | } else { | 75 | | - linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${changelogPath}\\) \\*\\*Current\\*\\*`); | | 75 | + linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${RegExp.escape(changelogPath)}\\) \\*\\*Current\\*\\*`); | 76 | 76 | } | 77 | 77 | assert.match(mainChangelog, linkToChangelog); | 78 | 78 | // Check table header. | 79 | 79 | let tableHeader; | 80 | 80 | if (lts) { | 81 | | - tableHeader = new RegExp(`<th title="LTS Until \\d{4}-\\d{2}"><a href="${changelogPath}">${major}</a> \\(LTS\\)</th>`); | | 81 | + tableHeader = new RegExp(`<th title="LTS Until \\d{4}-\\d{2}"><a href="${RegExp.escape(changelogPath)}">${major}</a> \\(LTS\\)</th>`); | 82 | 82 | } else { | 83 | | - tableHeader = new RegExp(`<th title="Current"><a href="${changelogPath}">${major}</a> \\(Current\\)</th>`); | | 83 | + tableHeader = new RegExp(`<th title="Current"><a href="${RegExp.escape(changelogPath)}">${major}</a> \\(Current\\)</th>`); | 84 | 84 | } | 85 | 85 | assert.match(mainChangelog, tableHeader); | 86 | 86 | // Check the table contains a link to the release in the appropriate CHANGELOG_V*.md file. | 87 | | - const linkToVersion = new RegExp(`<b><a href="${changelogPath}#${versionForRegex}">${versionForRegex}</a></b><br/>`); | | 87 | + const linkToVersion = new RegExp(`<b><a href="${RegExp.escape(changelogPath)}#${versionForRegex}">${versionForRegex}</a></b><br/>`); | 88 | 88 | assert.match(mainChangelog, linkToVersion); | 89 | 89 | } |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-repl-custom-eval-previews.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,7 +45,7 @@ describe('with previews', () => { | 45 | 45 | ); | 46 | 46 | const lines = getSingleCommandLines(output); | 47 | 47 | assert.match(lines.command, /^'Hello custom' \+ ' eval World!'/); | 48 | | - assert.match(lines.prompt, new RegExp(`${testingReplPrompt}$`)); | | 48 | + assert.match(lines.prompt, new RegExp(`${RegExp.escape(testingReplPrompt)}$`)); | 49 | 49 | assert.strictEqual(lines.result, "'Hello custom eval World!'"); | 50 | 50 | assert.strictEqual(lines.preview, undefined); | 51 | 51 | }); | @@ -62,7 +62,7 @@ describe('with previews', () => { | 62 | 62 | ); | 63 | 63 | const lines = getSingleCommandLines(output); | 64 | 64 | assert.match(lines.command, /^'Hello custom' \+ ' eval World!'/); | 65 | | - assert.match(lines.prompt, new RegExp(`${testingReplPrompt}$`)); | | 65 | + assert.match(lines.prompt, new RegExp(`${RegExp.escape(testingReplPrompt)}$`)); | 66 | 66 | assert.strictEqual(lines.result, "'Hello custom eval World!'"); | 67 | 67 | assert.match(lines.preview, /'Hello custom eval World!'/); | 68 | 68 | }); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3190,11 +3190,10 @@ assert.strictEqual( | 3190 | 3190 | frame.replaceAll('/', '\\')) | 3191 | 3191 | ).join('\n'); | 3192 | 3192 | } | 3193 | | - const escapedCWD = util.inspect(process.cwd()).slice(1, -1); | 3194 | 3193 | util.inspect(err, { colors: true }).split('\n').forEach(common.mustCallAtLeast((line, i) => { | 3195 | 3194 | let expected = stack[i].replace(/node_modules\/(@[^/]+\/[^/]+|[^/]+)/gi, (_, m) => { | 3196 | 3195 | return `node_modules/\u001b[4m${m}\u001b[24m`; | 3197 | | - }).replaceAll(new RegExp(`(\\(?${escapedCWD}(\\\\|/))`, 'gi'), (_, m) => { | | 3196 | + }).replaceAll(new RegExp(`(\\(?${RegExp.escape(process.cwd())}(\\\\|/))`, 'gi'), (_, m) => { | 3198 | 3197 | return `\x1B[90m${m}\x1B[39m`; | 3199 | 3198 | }); | 3200 | 3199 | if (expected.includes(process.cwd()) && expected.endsWith(')')) { | |
|