Skip to content

Commit

Permalink
Add support for relative parent paths (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
cb1kenobi authored and sindresorhus committed Jan 10, 2017
1 parent 2e3dd05 commit f148c7b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}, {
Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit f148c7b

Please sign in to comment.