From fa11503cdd6a2d59cd24e7cf0f5253cdeb5be16d Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 25 May 2024 12:11:20 +0200 Subject: [PATCH 1/2] test: do not assume cwd in snapshot tests --- test/common/assertSnapshot.js | 4 ++-- test/parallel/test-runner-output.mjs | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/test/common/assertSnapshot.js b/test/common/assertSnapshot.js index 88f40281e069b7..c4a30a5bae5db7 100644 --- a/test/common/assertSnapshot.js +++ b/test/common/assertSnapshot.js @@ -25,7 +25,7 @@ function replaceWindowsPaths(str) { } function replaceFullPaths(str) { - return str.replaceAll(process.cwd(), ''); + return str.replaceAll(path.resolve(__dirname, '../..'), ''); } function transform(...args) { @@ -78,7 +78,7 @@ async function spawnAndAssert(filename, transform = (x) => x, { tty = false, ... return; } const flags = common.parseTestFlags(filename); - const executable = tty ? 'tools/pseudo-tty.py' : process.execPath; + const executable = tty ? path.join(__dirname, '../..', 'tools/pseudo-tty.py') : process.execPath; const args = tty ? [process.execPath, ...flags, filename] : [...flags, filename]; const { stdout, stderr } = await common.spawnPromisified(executable, args, options); await assertSnapshot(transform(`${stdout}${stderr}`), filename); diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index a3f7354f24b8ab..b38f05c11a007d 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -3,6 +3,8 @@ import * as fixtures from '../common/fixtures.mjs'; import * as snapshot from '../common/assertSnapshot.js'; import { describe, it } from 'node:test'; import { hostname } from 'node:os'; +import { chdir } from 'node:process'; +import { fileURLToPath } from 'node:url'; const skipForceColors = process.config.variables.icu_gyp_path !== 'tools/icu/icu-generic.gyp' || @@ -14,8 +16,10 @@ function replaceTestDuration(str) { .replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *'); } +const root = fileURLToPath(new URL('../..', import.meta.url)); + const color = '(\\[\\d+m)'; -const stackTraceBasePath = new RegExp(`${color}\\(${process.cwd().replaceAll(/[\\^$*+?.()|[\]{}]/g, '\\$&')}/?${color}(.*)${color}\\)`, 'g'); +const stackTraceBasePath = new RegExp(`${color}\\(${root.replaceAll(/[\\^$*+?.()|[\]{}]/g, '\\$&')}/?${color}(.*)${color}\\)`, 'g'); function replaceSpecDuration(str) { return str @@ -151,6 +155,7 @@ const tests = [ }), })); +chdir(root); describe('test runner output', { concurrency: true }, () => { for (const { name, fn } of tests) { it(name, fn); From e0ca0aa74cc49b70bf36f56f3750984410a8b704 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 25 May 2024 23:39:16 +0200 Subject: [PATCH 2/2] fixup! test: do not assume cwd in snapshot tests --- test/parallel/test-runner-output.mjs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index b38f05c11a007d..676168e9df4e50 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -3,7 +3,7 @@ import * as fixtures from '../common/fixtures.mjs'; import * as snapshot from '../common/assertSnapshot.js'; import { describe, it } from 'node:test'; import { hostname } from 'node:os'; -import { chdir } from 'node:process'; +import { chdir, cwd } from 'node:process'; import { fileURLToPath } from 'node:url'; const skipForceColors = @@ -16,7 +16,7 @@ function replaceTestDuration(str) { .replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *'); } -const root = fileURLToPath(new URL('../..', import.meta.url)); +const root = fileURLToPath(new URL('../..', import.meta.url)).slice(0, -1); const color = '(\\[\\d+m)'; const stackTraceBasePath = new RegExp(`${color}\\(${root.replaceAll(/[\\^$*+?.()|[\]{}]/g, '\\$&')}/?${color}(.*)${color}\\)`, 'g'); @@ -155,7 +155,9 @@ const tests = [ }), })); -chdir(root); +if (cwd() !== root) { + chdir(root); +} describe('test runner output', { concurrency: true }, () => { for (const { name, fn } of tests) { it(name, fn);