From e291c5c1e347c4f4f6db0d11208f8c65750416a7 Mon Sep 17 00:00:00 2001 From: drtootsie Date: Mon, 9 Mar 2026 21:45:33 -0500 Subject: [PATCH] test: make snapshot path matching CWD-agnostic --- test/common/assertSnapshot.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/test/common/assertSnapshot.js b/test/common/assertSnapshot.js index 9a7482020e098a..fa1ba67d1939fd 100644 --- a/test/common/assertSnapshot.js +++ b/test/common/assertSnapshot.js @@ -108,9 +108,18 @@ function transformPath(dirname, replacement) { // 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 dirnameRegex = new RegExp(RegExp.escape(dirname), flags); - const winPathRegex = new RegExp(RegExp.escape(winPath), flags); + + // Escape and add word boundaries to prevent partial matches + // (e.g., /node shouldn't match /node_modules or nodejs.org) + const escapedUrlEncoded = RegExp.escape(urlEncoded); + const escapedDirname = RegExp.escape(dirname); + const escapedWinPath = RegExp.escape(winPath); + + // Use negative lookahead to prevent matching if followed by alphanumeric, underscore, or slash + const urlEncodedRegex = new RegExp(escapedUrlEncoded + '(?![\\w/])', flags); + const dirnameRegex = new RegExp(escapedDirname + '(?![\\w/])', flags); + const winPathRegex = new RegExp(escapedWinPath + '(?![\\w/])', flags); + return (str) => { return str.replaceAll('\\\'', "'") // Replace fileUrl first as `winPath` could be a substring of the fileUrl.