Skip to content

Commit

Permalink
Merge pull request #35 from UltCombo/clean-up
Browse files Browse the repository at this point in the history
Throw on invalid glob, clean up, 100% coverage, closes #34
  • Loading branch information
yocontra committed Jan 2, 2015
2 parents 87c9e02 + 0e0765f commit b8669e5
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 at index ' + index);
}

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 .* 0/);
gs.create.bind(gs, ['.', 42], {cwd: __dirname}).should.throw(/Invalid glob .* 1/);
});

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 b8669e5

Please sign in to comment.