Skip to content

Commit 662bf52

Browse files
aduh95marco-ippolito
authored andcommitted
test: do not assume cwd in snapshot tests
PR-URL: #53146 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 0312065 commit 662bf52

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

test/common/assertSnapshot.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function replaceWindowsPaths(str) {
2525
}
2626

2727
function replaceFullPaths(str) {
28-
return str.replaceAll(process.cwd(), '');
28+
return str.replaceAll(path.resolve(__dirname, '../..'), '');
2929
}
3030

3131
function transform(...args) {
@@ -78,7 +78,7 @@ async function spawnAndAssert(filename, transform = (x) => x, { tty = false, ...
7878
return;
7979
}
8080
const flags = common.parseTestFlags(filename);
81-
const executable = tty ? 'tools/pseudo-tty.py' : process.execPath;
81+
const executable = tty ? path.join(__dirname, '../..', 'tools/pseudo-tty.py') : process.execPath;
8282
const args = tty ? [process.execPath, ...flags, filename] : [...flags, filename];
8383
const { stdout, stderr } = await common.spawnPromisified(executable, args, options);
8484
await assertSnapshot(transform(`${stdout}${stderr}`), filename);

test/parallel/test-runner-output.mjs

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as fixtures from '../common/fixtures.mjs';
33
import * as snapshot from '../common/assertSnapshot.js';
44
import { describe, it } from 'node:test';
55
import { hostname } from 'node:os';
6+
import { chdir, cwd } from 'node:process';
7+
import { fileURLToPath } from 'node:url';
68

79
const skipForceColors =
810
process.config.variables.icu_gyp_path !== 'tools/icu/icu-generic.gyp' ||
@@ -14,8 +16,10 @@ function replaceTestDuration(str) {
1416
.replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *');
1517
}
1618

19+
const root = fileURLToPath(new URL('../..', import.meta.url)).slice(0, -1);
20+
1721
const color = '(\\[\\d+m)';
18-
const stackTraceBasePath = new RegExp(`${color}\\(${process.cwd().replaceAll(/[\\^$*+?.()|[\]{}]/g, '\\$&')}/?${color}(.*)${color}\\)`, 'g');
22+
const stackTraceBasePath = new RegExp(`${color}\\(${root.replaceAll(/[\\^$*+?.()|[\]{}]/g, '\\$&')}/?${color}(.*)${color}\\)`, 'g');
1923

2024
function replaceSpecDuration(str) {
2125
return str
@@ -149,6 +153,9 @@ const tests = [
149153
}),
150154
}));
151155

156+
if (cwd() !== root) {
157+
chdir(root);
158+
}
152159
describe('test runner output', { concurrency: true }, () => {
153160
for (const { name, fn } of tests) {
154161
it(name, fn);

0 commit comments

Comments
 (0)