Skip to content

Commit

Permalink
path: fix win32 relative() for UNC paths
Browse files Browse the repository at this point in the history
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()

PR-URL: #5456
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Roman Reiss <me@silverwind.io>
  • Loading branch information
omsmith authored and silverwind committed Feb 27, 2016
1 parent b33879d commit e326950
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
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

0 comments on commit e326950

Please sign in to comment.