diff --git a/index.js b/index.js index 09dde64..679b546 100644 --- a/index.js +++ b/index.js @@ -21,6 +21,8 @@ module.exports = function globParent(str, opts) { str = str.replace(backslash, slash); } + var win32Root = isWin32 && /^([a-zA-Z]:\/)/.exec(str); + // special case for strings ending in enclosure containing path separator if (isEnclosure(str)) { str += slash; @@ -34,8 +36,12 @@ module.exports = function globParent(str, opts) { str = pathPosixDirname(str); } while (isGlobby(str)); - // remove escape chars and return result - return str.replace(escaped, '$1'); + // remove escape chars and preserve a drive root used as the parent + str = str.replace(escaped, '$1'); + if (win32Root && str === win32Root[0].slice(0, -1)) { + return win32Root[0]; + } + return str; }; function isEnclosure(str) { diff --git a/test/index.test.js b/test/index.test.js index 5889412..13ff9c5 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -252,6 +252,23 @@ describe('glob2base test patterns', function () { if (isWin32) { describe('technically invalid windows globs', function () { + it('should preserve a drive root when it is the static parent', function (done) { + var options = { flipBackslashes: false }; + + expect(gp('C:/', options)).toEqual('C:/'); + expect(gp('C:/.', options)).toEqual('C:/'); + expect(gp('C:/*', options)).toEqual('C:/'); + expect(gp('C:/./*', options)).toEqual('C:/.'); + expect(gp('C://', options)).toEqual('C:/'); + expect(gp('C://*', options)).toEqual('C:/'); + expect(gp('C:/file.js', options)).toEqual('C:/'); + expect(gp('C:/dir/*.js', options)).toEqual('C:/dir'); + expect(gp('C:\\')).toEqual('C:/'); + expect(gp('C:\\*')).toEqual('C:/'); + + done(); + }); + it('should manage simple globs with backslash path separator', function (done) { expect(gp('C:\\path\\*.js')).toEqual('C:/path');