Skip to content

Commit 4b94f49

Browse files
committed
fixup! test: unify assertSnapshot stacktrace transform
1 parent d72d40b commit 4b94f49

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

test/common/assertSnapshot.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const common = require('.');
33
const path = require('node:path');
44
const test = require('node:test');
55
const fs = require('node:fs/promises');
6+
const { realpathSync } = require('node:fs');
67
const assert = require('node:assert/strict');
78
const { pathToFileURL } = require('node:url');
89
const { hostname } = require('node:os');
@@ -92,24 +93,42 @@ function replaceWarningPid(str) {
9293
return str.replaceAll(/\(node:\d+\)/g, '(node:<pid>)');
9394
}
9495

95-
// Replaces path strings representing the nodejs/node repo full project root with
96-
// `<project-root>`. Also replaces file URLs containing the full project root path.
97-
// The project root path may contain unicode characters.
98-
function transformProjectRoot(replacement = '<project-root>') {
99-
const projectRoot = path.resolve(__dirname, '../..');
96+
// Replaces a path with a placeholder. The path can be a platform specific path
97+
// or a file URL.
98+
function transformPath(dirname, replacement) {
10099
// Handles output already processed by `replaceWindowsPaths`.
101-
const winPath = replaceWindowsPaths(projectRoot);
100+
const winPath = replaceWindowsPaths(dirname);
102101
// Handles URL encoded project root in file URL strings as well.
103-
const urlEncoded = pathToFileURL(projectRoot).pathname;
102+
const urlEncoded = pathToFileURL(dirname).pathname;
104103
return (str) => {
105104
return str.replaceAll('\\\'', "'")
106105
// Replace fileUrl first as `winPath` could be a substring of the fileUrl.
107106
.replaceAll(urlEncoded, replacement)
108-
.replaceAll(projectRoot, replacement)
107+
.replaceAll(dirname, replacement)
109108
.replaceAll(winPath, replacement);
110109
};
111110
}
112111

112+
// Replaces path strings representing the nodejs/node repo full project root with
113+
// `<project-root>`. Also replaces file URLs containing the full project root path.
114+
// The project root path may contain unicode characters.
115+
const kProjectRoot = '<project-root>';
116+
function transformProjectRoot() {
117+
const projectRoot = path.resolve(__dirname, '../..');
118+
if (process.env.NODE_TEST_DIR) {
119+
const testDir = realpathSync(process.env.NODE_TEST_DIR);
120+
// On Jenkins CI, the test dir may be overridden by `NODE_TEST_DIR`.
121+
return transform(
122+
transformPath(projectRoot, kProjectRoot),
123+
transformPath(testDir, `${kProjectRoot}/test`),
124+
// TODO(legendecas): test-runner may print relative paths to the test relative to cwd.
125+
// It will be better if we could distinguish them from the project root.
126+
transformPath(path.relative(projectRoot, testDir), '')
127+
);
128+
}
129+
return transformPath(projectRoot, kProjectRoot);
130+
}
131+
113132
// Replaces tmpdirs created by `test/common/tmpdir.js`.
114133
function transformTmpDir(str) {
115134
return str.replaceAll(/\/\.tmp\.\d+\//g, '/<tmpdir>/');

0 commit comments

Comments
 (0)