Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw on invalid glob, clean up, 100% coverage, closes #34 #35

Merged
merged 1 commit into from
Jan 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/);
});

});
});