From 871396ce8fe75d74fd71ecd2011305fb91358d09 Mon Sep 17 00:00:00 2001 From: Owen Smith Date: Fri, 26 Feb 2016 15:53:20 -0500 Subject: [PATCH] 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 Reviewed-By: Roman Reiss --- lib/path.js | 14 ++++++++++++-- test/parallel/test-path.js | 7 ++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/path.js b/lib/path.js index fa84485cc3228e..7ac3c8980d94af 100644 --- a/lib/path.js +++ b/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); } }, diff --git a/test/parallel/test-path.js b/test/parallel/test-path.js index 92b0358d8e2323..3ec2e905f5841d 100644 --- a/test/parallel/test-path.js +++ b/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,