@@ -4,13 +4,17 @@ const path = require('node:path');
44const test = require ( 'node:test' ) ;
55const fs = require ( 'node:fs/promises' ) ;
66const assert = require ( 'node:assert/strict' ) ;
7+ const { pathToFileURL } = require ( 'node:url' ) ;
78const { hostname } = require ( 'node:os' ) ;
89
910const stackFramesRegexp = / (?< = \n ) ( \s + ) ( ( .+ ?) \s + \( ) ? (?: \( ? ( .+ ?) : ( \d + ) (?: : ( \d + ) ) ? ) \) ? ( \s + \{ ) ? ( \[ \d + m ) ? ( \n | $ ) / g;
1011const windowNewlineRegexp = / \r / g;
1112
13+ // Replaces the current Node.js executable version strings with a
14+ // placeholder. This could commonly present in an unhandled exception
15+ // output.
1216function replaceNodeVersion ( str ) {
13- return str . replaceAll ( process . version , '* ' ) ;
17+ return str . replaceAll ( process . version , '<node-version> ' ) ;
1418}
1519
1620function replaceStackTrace ( str , replacement = '$1*$7$8\n' ) {
@@ -23,18 +27,49 @@ function replaceInternalStackTrace(str) {
2327 return str . replaceAll ( / ( \W + ) .* [ ( \s ] n o d e : .* / g, '$1*' ) ;
2428}
2529
30+ // Replaces Windows line endings with posix line endings for unified snapshots
31+ // across platforms.
2632function replaceWindowsLineEndings ( str ) {
2733 return str . replace ( windowNewlineRegexp , '' ) ;
2834}
2935
36+ // Replaces all Windows path separators with posix separators for unified snapshots
37+ // across platforms.
3038function replaceWindowsPaths ( str ) {
3139 return common . isWindows ? str . replaceAll ( path . win32 . sep , path . posix . sep ) : str ;
3240}
3341
34- function transformProjectRoot ( replacement = '' ) {
42+ // Removes line trailing white spaces.
43+ function replaceTrailingSpaces ( str ) {
44+ return str . replaceAll ( / [ \t ] + \n / g, '\n' ) ;
45+ }
46+
47+ // Replaces customized or platform specific executable names to be `<node-exe>`.
48+ function generalizeExeName ( str ) {
49+ const baseName = path . basename ( process . argv0 || 'node' , '.exe' ) ;
50+ return str . replaceAll ( `${ baseName } --` , '<node-exe> --' ) ;
51+ }
52+
53+ // Replaces the pids in warning messages with a placeholder.
54+ function replaceWarningPid ( str ) {
55+ return str . replaceAll ( / \( n o d e : \d + \) / g, '(node:<pi
B2EA
d>)' ) ;
56+ }
57+
58+ // Replaces path strings representing the nodejs/node repo full project root with
59+ // `<project-root>`. Also replaces file URLs containing the full project root path.
60+ // The project root path may contain unicode characters.
61+ function transformProjectRoot ( replacement = '<project-root>' ) {
3562 const projectRoot = path . resolve ( __dirname , '../..' ) ;
63+ // Handles output already processed by `replaceWindowsPaths`.
64+ const winPath = replaceWindowsPaths ( projectRoot ) ;
65+ // Handles URL encoded project root in file URL strings as well.
66+ const urlEncoded = pathToFileURL ( projectRoot ) . pathname ;
3667 return ( str ) => {
37- return str . replaceAll ( '\\\'' , "'" ) . replaceAll ( projectRoot , replacement ) ;
68+ return str . replaceAll ( '\\\'' , "'" )
69+ // Replace fileUrl first as `winPath` could be a substring of the fileUrl.
70+ . replaceAll ( urlEncoded , replacement )
71+ . replaceAll ( projectRoot , replacement )
72+ . replaceAll ( winPath , replacement ) ;
3873 } ;
3974}
4075
@@ -152,32 +187,41 @@ function pickTestFileFromLcov(str) {
152187 ) ;
153188}
154189
155- const defaultTransform = transform (
190+ // Transforms basic patterns like:
191+ // - platform specific path and line endings,
192+ // - line trailing spaces,
193+ // - executable specific path and versions.
194+ const basicTransform = transform (
156195 replaceWindowsLineEndings ,
157- replaceStackTrace ,
196+ replaceTrailingSpaces ,
158197 removeWindowsPathEscaping ,
159- transformProjectRoot ( ) ,
160198 replaceWindowsPaths ,
199+ replaceNodeVersion ,
200+ generalizeExeName ,
201+ replaceWarningPid ,
202+ ) ;
203+
204+ const defaultTransform = transform (
205+ basicTransform ,
206+ replaceStackTrace ,
207+ transformProjectRoot ( ) ,
161208 replaceTestDuration ,
162209 replaceTestLocationLine ,
163210) ;
164211const specTransform = transform (
165212 replaceSpecDuration ,
166- replaceWindowsLineEndings ,
213+ basicTransform ,
167214 replaceStackTrace ,
168- replaceWindowsPaths ,
169215) ;
170216const junitTransform = transform (
171217 replaceJunitDuration ,
172- replaceWindowsLineEndings ,
218+ basicTransform ,
173219 replaceStackTrace ,
174- replaceWindowsPaths ,
175220) ;
176221const lcovTransform = transform (
177- replaceWindowsLineEndings ,
222+ basicTransform ,
178223 replaceStackTrace ,
179224 transformProjectRoot ( ) ,
180- replaceWindowsPaths ,
181225 pickTestFileFromLcov ,
182226) ;
183227
@@ -204,6 +248,7 @@ module.exports = {
204248 transform,
205249 transformProjectRoot,
206250 replaceTestDuration,
251+ basicTransform,
207252 defaultTransform,
208253 specTransform,
209254 junitTransform,
0 commit comments