From 15de356a883bbfaec5252c009875fda3781d460b Mon Sep 17 00:00:00 2001 From: Kael Zhang Date: Sat, 3 Nov 2018 09:49:45 +0800 Subject: [PATCH] 5.0.4: fixes #47: fixes isPathValid in windows --- index.js | 29 ++++++++++++++++++----------- package.json | 2 +- test/others.js | 31 +++++++++++++++++++++++-------- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 8e31cec..b77e5eb 100755 --- a/index.js +++ b/index.js @@ -532,6 +532,18 @@ class Ignore { } } +const factory = options => new Ignore(options) + +const isPathValid = path => checkPath(path, returnFalse) + + +factory.isPathValid = isPathValid + +// Fixes typescript +factory.default = factory + +module.exports = factory + // Windows // -------------------------------------------------------------- /* istanbul ignore if */ @@ -546,22 +558,17 @@ if ( const test = Ignore.prototype._test /* eslint no-control-regex: "off" */ - const make_posix = str => /^\\\\\?\\/.test(str) + const makePosix = str => /^\\\\\?\\/.test(str) || /["<>|\u0000-\u001F]+/u.test(str) ? str : str.replace(/\\/g, '/') Ignore.prototype._test = function testWin32 (path, ...args) { - path = make_posix(path) + path = makePosix(path) return test.call(this, path, ...args) } -} - -const factory = options => new Ignore(options) -factory.isPathValid = path => checkPath(path, returnFalse) - -// Fixes typescript -factory.default = factory - -module.exports = factory + factory.isPathValid = path => path + ? isPathValid(makePosix(path)) + : isPathValid(path) +} diff --git a/package.json b/package.json index 3ef3516..c8b2d4c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ignore", - "version": "5.0.3", + "version": "5.0.4", "description": "Ignore is a manager and filter for .gitignore rules, the one used by eslint, gitbook and many others.", "files": [ "legacy.js", diff --git a/test/others.js b/test/others.js index a774d7f..02c851a 100644 --- a/test/others.js +++ b/test/others.js @@ -3,6 +3,10 @@ const ignore = require('..') const {isPathValid} = ignore +const IS_WINDOWS = process.platform === 'win32' +const SHOULD_TEST_WINDOWS = process.env.IGNORE_TEST_WIN32 + || IS_WINDOWS + test('.add()', t => { const a = ignore().add(['.abc/*', '!.abc/d/']) const b = ignore().add(a).add('!.abc/e/') @@ -97,15 +101,26 @@ test('special case: invalid paths, throw', t => { }) test('isPathValid', t => { + const paths = [ + '.', + './foo', + '../foo', + '/foo', + false, + 'foo' + ] + + if (SHOULD_TEST_WINDOWS) { + paths.push( + '..\\foo', + '.\\foo', + '\\foo', + '\\\\foo' + ) + } + t.deepEqual( - [ - '.', - './foo', - '../foo', - '/foo', - false, - 'foo' - ].filter(isPathValid), + paths.filter(isPathValid), [ 'foo' ]