From ef7a08890648325270ad4b7ea816c965899119e0 Mon Sep 17 00:00:00 2001 From: Zheng Chaoping Date: Mon, 29 Feb 2016 15:03:37 +0800 Subject: [PATCH] path: fix win32 parse() Fix path.win32.parse("/foo/bar") retuns `{root: '' ...}`(v5.7.0), but not `{root: '/' ...}`(v5.6.0). PR-URL: https://github.com/nodejs/node/pull/5484 Reviewed-By: Brian White Reviewed-By: Roman Reiss Conflicts: test/parallel/test-path-parse-format.js --- lib/path.js | 2 +- test/parallel/test-path-parse-format.js | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/path.js b/lib/path.js index 7ac3c8980d94af..2c27d75fc17e73 100644 --- a/lib/path.js +++ b/lib/path.js @@ -1007,8 +1007,8 @@ const win32 = { isAbsolute = true; code = path.charCodeAt(1); + rootEnd = 1; if (code === 47/*/*/ || code === 92/*\*/) { - rootEnd = 1; // Matched double path separator at beginning var j = 2; var last = j; diff --git a/test/parallel/test-path-parse-format.js b/test/parallel/test-path-parse-format.js index aa707c0f0b218e..e8c550acfec2c2 100644 --- a/test/parallel/test-path-parse-format.js +++ b/test/parallel/test-path-parse-format.js @@ -19,7 +19,11 @@ var winPaths = [ '\\\\?\\UNC\\server\\share' ]; -var winSpecialCaseFormatTests = [ +const winSpecialCaseParseTests = [ + ['/foo/bar', {root: '/'}] +]; + +const winSpecialCaseFormatTests = [ [{dir: 'some\\dir'}, 'some\\dir\\'], [{base: 'index.html'}, 'index.html'], [{}, ''] @@ -77,6 +81,7 @@ var errors = [ checkParseFormat(path.win32, winPaths); checkParseFormat(path.posix, unixPaths); +checkSpecialCaseParseFormat(path.win32, winSpecialCaseParseTests); checkErrors(path.win32); checkErrors(path.posix); checkFormat(path.win32, winSpecialCaseFormatTests); @@ -175,6 +180,17 @@ function checkParseFormat(path, paths) { }); } +function checkSpecialCaseParseFormat(path, testCases) { + testCases.forEach(function(testCase) { + const element = testCase[0]; + const expect = testCase[1]; + const output = path.parse(element); + Object.keys(expect).forEach(function(key) { + assert.strictEqual(output[key], expect[key]); + }); + }); +} + function checkFormat(path, testCases) { testCases.forEach(function(testCase) { assert.strictEqual(path.format(testCase[0]), testCase[1]);