Browse Source

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()

PR-URL: https://github.com/nodejs/node/pull/5456
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Roman Reiss <me@silverwind.io>
v5.x
Owen Smith 9 years ago
committed by Rod Vagg
parent
commit
871396ce8f
  1. 14
      lib/path.js
  2. 7
      test/parallel/test-path.js

14
lib/path.js

@ -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
@ -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
@ -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);
}
},

7
test/parallel/test-path.js

@ -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,

Loading…
Cancel
Save