From f148c7b42019a511207fe7b2ec5003025a3b37ef Mon Sep 17 00:00:00 2001 From: Chris Barber Date: Tue, 10 Jan 2017 00:54:04 -0600 Subject: [PATCH] Add support for relative parent paths (#78) --- index.js | 15 +++++++++++++-- test.js | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d55dc57..9e8e241 100644 --- a/index.js +++ b/index.js @@ -13,8 +13,19 @@ module.exports = function (pattern, options) { } return streamfilter(function (file, enc, cb) { - var match = typeof pattern === 'function' ? pattern(file) : - multimatch(path.relative(file.cwd, file.path), pattern, options).length > 0; + var match; + if (typeof pattern === 'function') { + match = pattern(file); + } else { + var relPath = path.relative(file.cwd, file.path); + // if the path leaves the current working directory, then we need to + // resolve the absolute path so that the path can be properly matched + // by minimatch (via multimatch) + if (relPath.indexOf('../') === 0) { + relPath = path.resolve(relPath); + } + match = multimatch(relPath, pattern, options).length > 0; + } cb(!match); }, { diff --git a/test.js b/test.js index 28c4f3a..56807fd 100644 --- a/test.js +++ b/test.js @@ -160,6 +160,28 @@ describe('filter()', function () { stream.end(); }); + + it('should filter relative paths that leave current directory tree', function (cb) { + var stream = filter('**/test/**/*.js'); + var buffer = []; + var gfile = path.join('..', '..', 'test', 'included.js'); + + stream.on('data', function (file) { + buffer.push(file); + }); + + stream.on('end', function () { + assert.equal(buffer.length, 1); + assert.equal(buffer[0].relative, gfile); + cb(); + }); + + stream.write(new gutil.File({ + path: gfile + })); + + stream.end(); + }); }); describe('filter.restore', function () {