Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 87 additions & 38 deletions test/common/assertSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@ const common = require('.');
const path = require('node:path');
const test = require('node:test');
const fs = require('node:fs/promises');
const { realpathSync } = require('node:fs');
const assert = require('node:assert/strict');
const { pathToFileURL } = require('node:url');
const { hostname } = require('node:os');

const stackFramesRegexp = /(?<=\n)(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\[\d+m)?(\n|$)/g;
/* eslint-disable @stylistic/js/max-len,no-control-regex */
/**
* Group 1: Line start (including color codes and escapes)
* Group 2: Function name
* Group 3: Filename
* Group 4: Line number
* Group 5: Column number
* Group 6: Line end (including color codes and `{` which indicates the start of an error object details)
*/
// Mappings: (g1 ) (g2 ) (g3 ) (g4 ) (g5 ) (g6 )
const internalStackFramesRegexp = /(?<=\n)(\s*(?:\x1b?\[\d+m\s+)?(?:at\s)?)(?:(.+?)\s+\()?(?:(node:.+?):(\d+)(?::(\d+))?)\)?((?:\x1b?\[\d+m)?\s*{?\n|$)/g;
/**
* Group 1: Filename
* Group 2: Line number
* Group 3: Line end and source code line
*/
const internalErrorSourceLines = /(?<=\n|^)(node:.+?):(\d+)(\n.*\n\s*\^(?:\n|$))/g;
/* eslint-enable @stylistic/js/max-len,no-control-regex */

const windowNewlineRegexp = /\r/g;

// Replaces the current Node.js executable version strings with a
Expand All @@ -17,14 +36,33 @@ function replaceNodeVersion(str) {
return str.replaceAll(`Node.js ${process.version}`, 'Node.js <node-version>');
}

function replaceStackTrace(str, replacement = '$1*$7$8\n') {
return str.replace(stackFramesRegexp, replacement);
// Collapse consecutive identical lines containing the keyword into
// one single line. The `str` should have been processed by `replaceWindowsLineEndings`.
function foldIdenticalLines(str, keyword) {
const lines = str.split('\n');
const folded = lines.filter((line, idx) => {
if (idx === 0) {
return true;
}
if (line.includes(keyword) && line === lines[idx - 1]) {
return false;
}
return true;
});
return folded.join('\n');
}

const kInternalFrame = '<node-internal-frames>';
// Replace non-internal frame `at TracingChannel.traceSync (node:diagnostics_channel:328:14)`
// as well as `at node:internal/main/run_main_module:33:47` with `at <node-internal-frames>`.
// Also replaces error source line like:
// node:internal/mod.js:44
// throw err;
// ^
function replaceInternalStackTrace(str) {
// Replace non-internal frame `at TracingChannel.traceSync (node:diagnostics_channel:328:14)`
// as well as `at node:internal/main/run_main_module:33:47` with `*`.
return str.replaceAll(/(\W+).*[(\s]node:.*/g, '$1*');
const result = str.replaceAll(internalErrorSourceLines, `$1:<line>$3`)
.replaceAll(internalStackFramesRegexp, `$1${kInternalFrame}$6`);
return foldIdenticalLines(result, kInternalFrame);
}

// Replaces Windows line endings with posix line endings for unified snapshots
Expand Down Expand Up @@ -55,30 +93,53 @@ function replaceWarningPid(str) {
return str.replaceAll(/\(node:\d+\)/g, '(node:<pid>)');
}

// Replaces path strings representing the nodejs/node repo full project root with
// `<project-root>`. Also replaces file URLs containing the full project root path.
// The project root path may contain unicode characters.
function transformProjectRoot(replacement = '<project-root>') {
const projectRoot = path.resolve(__dirname, '../..');
// Replaces a path with a placeholder. The path can be a platform specific path
// or a file URL.
function transformPath(dirname, replacement) {
// Handles output already processed by `replaceWindowsPaths`.
const winPath = replaceWindowsPaths(projectRoot);
// Handles URL encoded project root in file URL strings as well.
const urlEncoded = pathToFileURL(projectRoot).pathname;
const winPath = replaceWindowsPaths(dirname);
// Handles URL encoded path in file URL strings as well.
const urlEncoded = pathToFileURL(dirname).pathname;
// On Windows, paths are case-insensitive, so we need to use case-insensitive
// regex replacement to handle cases where the drive letter case differs.
const flags = common.isWindows ? 'gi' : 'g';
const urlEncodedRegex = new RegExp(RegExp.escape(urlEncoded), flags);
const projectRootRegex = new RegExp(RegExp.escape(projectRoot), flags);
const dirnameRegex = new RegExp(RegExp.escape(dirname), flags);
const winPathRegex = new RegExp(RegExp.escape(winPath), flags);
return (str) => {
return str.replaceAll('\\\'', "'")
// Replace fileUrl first as `winPath` could be a substring of the fileUrl.
.replaceAll(urlEncodedRegex, replacement)
.replaceAll(projectRootRegex, replacement)
.replaceAll(dirnameRegex, replacement)
.replaceAll(winPathRegex, replacement);
};
}

// Replaces path strings representing the nodejs/node repo full project root with
// `<project-root>`. Also replaces file URLs containing the full project root path.
// The project root path may contain unicode characters.
const kProjectRoot = '<project-root>';
function transformProjectRoot() {
const projectRoot = path.resolve(__dirname, '../..');
if (process.env.NODE_TEST_DIR) {
const testDir = realpathSync(process.env.NODE_TEST_DIR);
// On Jenkins CI, the test dir may be overridden by `NODE_TEST_DIR`.
return transform(
transformPath(projectRoot, kProjectRoot),
transformPath(testDir, `${kProjectRoot}/test`),
// TODO(legendecas): test-runner may print relative paths to the test relative to cwd.
// It will be better if we could distinguish them from the project root.
transformPath(path.relative(projectRoot, testDir), 'test'),
);
}
return transformPath(projectRoot, kProjectRoot);
}

// Replaces tmpdirs created by `test/common/tmpdir.js`.
function transformTmpDir(str) {
return str.replaceAll(/\/\.tmp\.\d+\//g, '/<tmpdir>/');
}

function transform(...args) {
return (str) => args.reduce((acc, fn) => fn(acc), str);
}
Expand Down Expand Up @@ -149,33 +210,24 @@ function replaceTestDuration(str) {
}

const root = path.resolve(__dirname, '..', '..');
const color = '(\\[\\d+m)';
const stackTraceBasePath = new RegExp(`${color}\\(${RegExp.escape(root)}/?${color}(.*)${color}\\)`, 'g');

function replaceSpecDuration(str) {
return str
.replaceAll(/[0-9.]+ms/g, '*ms')
.replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *')
.replace(stackTraceBasePath, '$3');
.replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *');
}

function replaceJunitDuration(str) {
return str
.replaceAll(/time="[0-9.]+"/g, 'time="*"')
.replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *')
.replaceAll(`hostname="${hostname()}"`, 'hostname="HOSTNAME"')
.replaceAll(/file="[^"]*"/g, 'file="*"')
.replace(stackTraceBasePath, '$3');
.replaceAll(/file="[^"]*"/g, 'file="*"');
}

function removeWindowsPathEscaping(str) {
return common.isWindows ? str.replaceAll(/\\\\/g, '\\') : str;
}

function replaceTestLocationLine(str) {
return str.replaceAll(/(js:)(\d+)(:\d+)/g, '$1(LINE)$3');
}

// The Node test coverage returns results for all files called by the test. This
// will make the output file change if files like test/common/index.js change.
// This transform picks only the first line and then the lines from the test
Expand All @@ -194,9 +246,11 @@ function pickTestFileFromLcov(str) {
}

// Transforms basic patterns like:
// - platform specific path and line endings,
// - line trailing spaces,
// - executable specific path and versions.
// - platform specific path and line endings
// - line trailing spaces
// - executable specific path and versions
// - project root path and tmpdir
// - node internal stack frames
const basicTransform = transform(
replaceWindowsLineEndings,
replaceTrailingSpaces,
Expand All @@ -205,29 +259,25 @@ const basicTransform = transform(
replaceNodeVersion,
generalizeExeName,
replaceWarningPid,
transformProjectRoot(),
transformTmpDir,
replaceInternalStackTrace,
);

const defaultTransform = transform(
basicTransform,
replaceStackTrace,
transformProjectRoot(),
replaceTestDuration,
replaceTestLocationLine,
);
const specTransform = transform(
replaceSpecDuration,
basicTransform,
replaceStackTrace,
);
const junitTransform = transform(
replaceJunitDuration,
basicTransform,
replaceStackTrace,
);
const lcovTransform = transform(
basicTransform,
replaceStackTrace,
transformProjectRoot(),
pickTestFileFromLcov,
);

Expand All @@ -246,7 +296,6 @@ module.exports = {
assertSnapshot,
getSnapshotPath,
replaceNodeVersion,
replaceStackTrace,
replaceInternalStackTrace,
replaceWindowsLineEndings,
replaceWindowsPaths,
Expand Down
10 changes: 2 additions & 8 deletions test/fixtures/console/console.snapshot
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
Trace: foo
at *
at *
at *
at *
at *
at *
at *
at *
at Object.<anonymous> (<project-root>/test/fixtures/console/console.js:5:9)
at <node-internal-frames>
2 changes: 1 addition & 1 deletion test/fixtures/console/stack_overflow.snapshot
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
before
<project-root>/test/fixtures/console/stack_overflow.js:*
<project-root>/test/fixtures/console/stack_overflow.js:39
JSON.stringify(array);
^

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/errors/async_error_nexttick_main.snapshot
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Error: test
at one (<project-root>/test/fixtures/async-error.js:4:9)
at two (<project-root>/test/fixtures/async-error.js:17:9)
at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
at <node-internal-frames>
at async three (<project-root>/test/fixtures/async-error.js:20:3)
at async four (<project-root>/test/fixtures/async-error.js:24:3)
at async main (<project-root>/test/fixtures/errors/async_error_nexttick_main.js:7:5)
5 changes: 2 additions & 3 deletions test/fixtures/errors/core_line_numbers.snapshot
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
node:punycode:54
node:punycode:<line>
throw new RangeError(errors[type]);
^

RangeError: Invalid input
at error (node:punycode:54:8)
at Object.decode (node:punycode:247:5)
at <node-internal-frames>
at Object.<anonymous> (<project-root>/test/fixtures/errors/core_line_numbers.js:13:10)

Node.js <node-version>
8 changes: 4 additions & 4 deletions test/fixtures/errors/error_aggregateTwoErrors.snapshot
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:*
<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:15
throw aggregateTwoErrors(err, originalError);
^

AggregateError: original
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:*:*) {
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:15:7) {
code: 'ERR0',
[errors]: [
Error: original
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:*:*) {
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:9:23) {
code: 'ERR0'
},
Error: second error
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:*:*) {
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_aggregateTwoErrors.js:10:13) {
code: 'ERR1'
}
]
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/errors/error_exit.snapshot
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Exiting with code=1
node:internal/assert/utils:*
node:internal/assert/utils:<line>
throw error;
^

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

1 !== 2

at Object.<anonymous> (<project-root>/test/fixtures/errors/error_exit.js:*:*) {
at Object.<anonymous> (<project-root>/test/fixtures/errors/error_exit.js:32:8) {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: 1,
Expand Down
Binary file modified test/fixtures/errors/error_with_nul.snapshot
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
node:events:*
node:events:<line>
throw er; // Unhandled 'error' event
^

Error: foo:bar
at bar (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:*:*)
at foo (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:*:*)
at bar (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:9:12)
at foo (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:12:10)
Emitted 'error' event at:
at quux (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:*:*)
at quux (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:19:6)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_common_trace.js:22:1)

Node.js <node-version>
6 changes: 3 additions & 3 deletions test/fixtures/errors/events_unhandled_error_nexttick.snapshot
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
node:events:*
node:events:<line>
throw er; // Unhandled 'error' event
^

Error
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_nexttick.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_nexttick.js:6:12)
Emitted 'error' event at:
at <project-root>/test/fixtures/errors/events_unhandled_error_nexttick.js:*:*
at <project-root>/test/fixtures/errors/events_unhandled_error_nexttick.js:8:22

Node.js <node-version>
6 changes: 3 additions & 3 deletions test/fixtures/errors/events_unhandled_error_sameline.snapshot
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
node:events:*
node:events:<line>
throw er; // Unhandled 'error' event
^

Error
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_sameline.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_sameline.js:6:34)
Emitted 'error' event at:
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_sameline.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_sameline.js:6:20)

Node.js <node-version>
6 changes: 3 additions & 3 deletions test/fixtures/errors/events_unhandled_error_subclass.snapshot
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
node:events:*
node:events:<line>
throw er; // Unhandled 'error' event
^

Error
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_subclass.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_subclass.js:7:25)
Emitted 'error' event on Foo instance at:
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_subclass.js:*:*)
at Object.<anonymous> (<project-root>/test/fixtures/errors/events_unhandled_error_subclass.js:7:11)

Node.js <node-version>
8 changes: 1 addition & 7 deletions test/fixtures/errors/force_colors.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ throw new Error('Should include grayed stack trace');

Error: Should include grayed stack trace
at Object.<anonymous> (<project-root>/test/fixtures/errors/force_colors.js:2:7)
 at *
 at *
 at *
 at *
 at *
 at *
 at *
 at <node-internal-frames>

Node.js <node-version>
Loading
Loading