Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
path: fix win32 relative() for UNC paths
win32 normalize() will output a trailing '\' for some UNC paths. trim
them before processing

Change by @mscdex

Add basic UNC path tests to win32 relative()
  • Loading branch information
omsmith committed Feb 27, 2016
commit 80fd741ff002abea716dd2df80367e2694291c7a
14 changes: 12 additions & 2 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,12 @@ const win32 = {
if (from.charCodeAt(fromStart) !== 92/*\*/)
break;
}
// Trim trailing backslashes (applicable to UNC paths only)
var fromEnd = from.length;
for (; fromEnd - 1 > fromStart; --fromEnd) {
if (from.charCodeAt(fromEnd - 1) !== 92/*\*/)
break;
}
var fromLen = (fromEnd - fromStart);

// Trim any leading backslashes
Expand All @@ -594,7 +599,12 @@ const win32 = {
if (to.charCodeAt(toStart) !== 92/*\*/)
break;
}
// Trim trailing backslashes (applicable to UNC paths only)
var toEnd = to.length;
for (; toEnd - 1 > toStart; --toEnd) {
if (to.charCodeAt(toEnd - 1) !== 92/*\*/)
break;
}
var toLen = (toEnd - toStart);

// Compare paths to find the longest common path from root
Expand Down Expand Up @@ -662,12 +672,12 @@ const win32 = {
// Lastly, append the rest of the destination (`to`) path that comes after
// the common path parts
if (out.length > 0)
return out + toOrig.slice(toStart + lastCommonSep);
return out + toOrig.slice(toStart + lastCommonSep, toEnd);
else {
toStart += lastCommonSep;
if (toOrig.charCodeAt(toStart) === 92/*\*/)
++toStart;
return toOrig.slice(toStart);
return toOrig.slice(toStart, toEnd);
}
},

Expand Down
7 changes: 6 additions & 1 deletion test/parallel/test-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,12 @@ const relativeTests = [
['c:/aaaaa/', 'c:/aaaa/cccc', '..\\aaaa\\cccc'],
['C:\\foo\\bar\\baz\\quux', 'C:\\', '..\\..\\..\\..'],
['C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json', 'bar\\package.json'],
['C:\\foo\\bar\\baz-quux', 'C:\\foo\\bar\\baz', '..\\baz']
['C:\\foo\\bar\\baz-quux', 'C:\\foo\\bar\\baz', '..\\baz'],
['C:\\foo\\bar\\baz', 'C:\\foo\\bar\\baz-quux', '..\\baz-quux'],
['\\\\foo\\bar', '\\\\foo\\bar\\baz', 'baz'],
['\\\\foo\\bar\\baz', '\\\\foo\\bar', '..'],
['\\\\foo\\bar\\baz-quux', '\\\\foo\\bar\\baz', '..\\baz'],
['\\\\foo\\bar\\baz', '\\\\foo\\bar\\baz-quux', '..\\baz-quux']
]
],
[ path.posix.relative,
Expand Down