Skip to content

Commit

Permalink
Throw on invalid glob, clean up, 100% coverage, closes #34
Browse files Browse the repository at this point in the history
- Better naming for negative matchers ( #33 (comment) )
- Throw on invalid glob argument ( #34 (comment) )
- Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
  • Loading branch information
UltCombo committed Jan 2, 2015
1 parent 2768041 commit 4e4be75
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ var gs = {
if (opt.cwdbase) opt.base = opt.cwd;

// only one glob no need to aggregate
if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);
if (!Array.isArray(globs)) globs = [globs];

var positives = [];
var negatives = [];

globs.forEach(function(glob, index) {
if (typeof glob !== 'string' && !(glob instanceof RegExp)) {
throw new Error('Invalid glob argument');
}

var globArray = isNegative(glob) ? negatives : positives;

// create Minimatch instances for negative glob patterns
Expand Down Expand Up @@ -104,13 +108,11 @@ var gs = {
function isMatch(file, matcher) {
if (matcher instanceof Minimatch) return matcher.match(file.path);
if (matcher instanceof RegExp) return matcher.test(file.path);
return true; // unknown glob type?
}

function isNegative(pattern) {
if (typeof pattern !== 'string') return true;
if (pattern[0] === '!') return true;
return false;
if (typeof pattern === 'string') return pattern[0] === '!';
if (pattern instanceof RegExp) return true;
}

function unrelative(cwd, glob) {
Expand Down
10 changes: 10 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,5 +474,15 @@ describe('glob-stream', function() {
});
});

it('should throw on invalid glob argument', function() {
gs.create.bind(gs, 42, {cwd: __dirname}).should.throw(/Invalid glob argument/);
gs.create.bind(gs, ['.', 42], {cwd: __dirname}).should.throw(/Invalid glob argument/);
});

it('should throw on missing positive glob', function() {
gs.create.bind(gs, '!c', {cwd: __dirname}).should.throw(/Missing positive glob/);
gs.create.bind(gs, ['!a', '!b'], {cwd: __dirname}).should.throw(/Missing positive glob/);
});

});
});

0 comments on commit 4e4be75

Please sign in to comment.