Skip to content

Commit

Permalink
test: do not assume cwd in snapshot tests
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
aduh95 authored and marco-ippolito committed Aug 19, 2024
1 parent 0312065 commit 662bf52
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions test/common/assertSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function replaceWindowsPaths(str) {
}

function replaceFullPaths(str) {
return str.replaceAll(process.cwd(), '');
return str.replaceAll(path.resolve(__dirname, '../..'), '');
}

function transform(...args) {
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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, cwd } from 'node:process';
import { fileURLToPath } from 'node:url';

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

const root = fileURLToPath(new URL('../..', import.meta.url)).slice(0, -1);

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
Expand Down Expand Up @@ -149,6 +153,9 @@ const tests = [
}),
}));

if (cwd() !== root) {
chdir(root);
}
describe('test runner output', { concurrency: true }, () => {
for (const { name, fn } of tests) {
it(name, fn);
Expand Down

0 comments on commit 662bf52

Please sign in to comment.