Skip to content

Commit 102cdfe

Browse files
authored
test: fix case-insensitive path matching on Windows
On Windows, file paths are case-insensitive but string comparison is case-sensitive. When the drive letter case differs between the computed project root and the actual output (e.g., 'C:/' vs 'c:/'), the path replacement in transformProjectRoot() would fail. This fix uses case-insensitive regex replacement on Windows to ensure paths are correctly normalized in snapshot tests regardless of drive letter casing. Refs: nodejs/reliability#1453 PR-URL: #61682 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Stefan Stojanovic <stefan.stojanovic@janeasystems.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
1 parent d8c00ad commit 102cdfe

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

test/common/assertSnapshot.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,18 @@ function transformProjectRoot(replacement = '<project-root>') {
6464
const winPath = replaceWindowsPaths(projectRoot);
6565
// Handles URL encoded project root in file URL strings as well.
6666
const urlEncoded = pathToFileURL(projectRoot).pathname;
67+
// On Windows, paths are case-insensitive, so we need to use case-insensitive
68+
// regex replacement to handle cases where the drive letter case differs.
69+
const flags = common.isWindows ? 'gi' : 'g';
70+
const urlEncodedRegex = new RegExp(RegExp.escape(urlEncoded), flags);
71+
const projectRootRegex = new RegExp(RegExp.escape(projectRoot), flags);
72+
const winPathRegex = new RegExp(RegExp.escape(winPath), flags);
6773
return (str) => {
6874
return str.replaceAll('\\\'', "'")
6975
// Replace fileUrl first as `winPath` could be a substring of the fileUrl.
70-
.replaceAll(urlEncoded, replacement)
71-
.replaceAll(projectRoot, replacement)
72-
.replaceAll(winPath, replacement);
76+
.replaceAll(urlEncodedRegex, replacement)
77+
.replaceAll(projectRootRegex, replacement)
78+
.replaceAll(winPathRegex, replacement);
7379
};
7480
}
7581

0 commit comments

Comments
 (0)