Skip to content

Commit ae18ec0

Browse files
committed
fix: file paths in snapshots by normalizing on windows
1 parent 44696a5 commit ae18ec0

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

rewatch/tests/compile.sh

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,36 @@ mv ./packages/main/src/Main2.res ./packages/main/src/Main.res
3939
# Rename a file with a dependent - this should trigger an error
4040
mv ./packages/main/src/InternalDep.res ./packages/main/src/InternalDep2.res
4141
rewatch build &> ../tests/snapshots/rename-file-internal-dep.txt
42-
# replace the absolute path so the snapshot is the same on all machines
43-
replace "s/$(pwd | sed "s/\//\\\\\//g")//g" ../tests/snapshots/rename-file-internal-dep.txt
42+
# normalize paths so the snapshot is the same on all machines
43+
normalize_paths ../tests/snapshots/rename-file-internal-dep.txt
4444
mv ./packages/main/src/InternalDep2.res ./packages/main/src/InternalDep.res
4545

4646
# Rename a file with a dependent in a namespaced package - this should trigger an error (regression)
4747
mv ./packages/new-namespace/src/Other_module.res ./packages/new-namespace/src/Other_module2.res
4848
rewatch build &> ../tests/snapshots/rename-file-internal-dep-namespace.txt
49-
# replace the absolute path so the snapshot is the same on all machines
50-
replace "s/$(pwd | sed "s/\//\\\\\//g")//g" ../tests/snapshots/rename-file-internal-dep-namespace.txt
49+
# normalize paths so the snapshot is the same on all machines
50+
normalize_paths ../tests/snapshots/rename-file-internal-dep-namespace.txt
5151
mv ./packages/new-namespace/src/Other_module2.res ./packages/new-namespace/src/Other_module.res
5252

5353
rewatch build &> /dev/null
5454
mv ./packages/main/src/ModuleWithInterface.resi ./packages/main/src/ModuleWithInterface2.resi
5555
rewatch build &> ../tests/snapshots/rename-interface-file.txt
56+
# normalize paths so the snapshot is the same on all machines
57+
normalize_paths ../tests/snapshots/rename-interface-file.txt
5658
mv ./packages/main/src/ModuleWithInterface2.resi ./packages/main/src/ModuleWithInterface.resi
5759
rewatch build &> /dev/null
5860
mv ./packages/main/src/ModuleWithInterface.res ./packages/main/src/ModuleWithInterface2.res
5961
rewatch build &> ../tests/snapshots/rename-file-with-interface.txt
62+
# normalize paths so the snapshot is the same on all machines
63+
normalize_paths ../tests/snapshots/rename-file-with-interface.txt
6064
mv ./packages/main/src/ModuleWithInterface2.res ./packages/main/src/ModuleWithInterface.res
6165
rewatch build &> /dev/null
6266

6367
# when deleting a file that other files depend on, the compile should fail
6468
rm packages/dep02/src/Dep02.res
6569
rewatch build &> ../tests/snapshots/remove-file.txt
66-
# replace the absolute path so the snapshot is the same on all machines
67-
replace "s/$(pwd | sed "s/\//\\\\\//g")//g" ../tests/snapshots/remove-file.txt
70+
# normalize paths so the snapshot is the same on all machines
71+
normalize_paths ../tests/snapshots/remove-file.txt
6872
git checkout -- packages/dep02/src/Dep02.res
6973
rewatch build &> /dev/null
7074

rewatch/tests/utils.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,42 @@ bold() { echo -e "\033[1m$1\033[0m"; }
66
rewatch() { RUST_BACKTRACE=1 $REWATCH_EXECUTABLE --no-timing=true $@; }
77
rewatch_bg() { RUST_BACKTRACE=1 nohup $REWATCH_EXECUTABLE --no-timing=true $@; }
88

9+
# Detect if running on Windows
10+
is_windows() {
11+
[[ $OSTYPE == 'msys'* || $OSTYPE == 'cygwin'* || $OSTYPE == 'win'* ]];
12+
}
13+
14+
# get pwd with forward slashes
15+
pwd_prefix() {
16+
if is_windows; then
17+
# On Windows, escape backslashes for sed and convert to forward slashes for consistent snapshots
18+
# This ensures paths like C:\a\b are replaced correctly
19+
# First get the Windows-style path with backslashes
20+
local win_path=$(pwd -W | sed "s#/#\\\\#g")
21+
# Then escape the backslashes for sed replacement
22+
echo $win_path | sed 's#\\#\\\\#g'
23+
else
24+
# On Unix-like systems, escape forward slashes for sed
25+
echo $(pwd | sed "s#/#\\/#g")
26+
fi
27+
}
28+
29+
# replace the absolute path so the snapshot is the same on all machines
30+
# then normalize the path separators
31+
normalize_paths() {
32+
if [[ $OSTYPE == 'darwin'* ]];
33+
then
34+
sed -i '' "s#$(pwd_prefix)##g" $1;
35+
else
36+
if is_windows; then
37+
sed -i "s#$(pwd_prefix)##g" $1
38+
sed -i "s#\\\\#/#g" $1
39+
else
40+
sed -i "s#$(pwd_prefix)##g" $1;
41+
fi
42+
fi
43+
}
44+
945
replace() {
1046
if [[ $OSTYPE == 'darwin'* ]];
1147
then

0 commit comments

Comments
 (0)