Skip to content

Commit

Permalink
Merge pull request #41 from callumacrae/empty-error
Browse files Browse the repository at this point in the history
Emit error on file not found with singular globs
  • Loading branch information
contra committed Mar 16, 2015
2 parents e1747cd + 1df43bf commit 1895396
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ You can pass any combination of globs. One caveat is that you can not only pass
- cwdbase
- Default is `false`
- When true it is the same as saying opt.base = opt.cwd
- allowEmpty
- Default is `false`
- If true, won't emit an error when a glob pointing at a single file fails to match

This argument is passed directly to [node-glob](https://github.com/isaacs/node-glob) so check there for more options

Expand Down
29 changes: 27 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ var gs = {
// create stream and map events from globber to it
var stream = through2.obj(negatives.length ? filterNegatives : undefined);

var found = false;

globber.on('error', stream.emit.bind(stream, 'error'));
globber.on('end', function(/* some args here so can't use bind directly */){
globber.on('end', function(){
if (opt.allowEmpty !== true && !found && globIsSingular(globber)) {
stream.emit('error', new Error('File not found with singular glob'));
}

stream.end();
});
globber.on('match', function(filename) {
found = true;

stream.write({
cwd: opt.cwd,
base: basePath,
Expand Down Expand Up @@ -95,8 +103,13 @@ var gs = {
// then just pipe them to a single unique stream and return it
var aggregate = new Combine(streams);
var uniqueStream = unique('path');
var returnStream = aggregate.pipe(uniqueStream);

aggregate.on('error', function (err) {
returnStream.emit('error', err);
});

return aggregate.pipe(uniqueStream);
return returnStream;

function streamFromPositive(positive) {
var negativeGlobs = negatives.filter(indexGreaterThan(positive.index)).map(toGlob);
Expand Down Expand Up @@ -134,4 +147,16 @@ function toGlob(obj) {
return obj.glob;
}

function globIsSingular(glob) {
var globSet = glob.minimatch.set;

if (globSet.length !== 1) {
return false;
}

return globSet[0].every(function isString(value) {
return typeof value === 'string';
});
}

module.exports = gs;
46 changes: 43 additions & 3 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('glob-stream', function() {
var baseDir = join(__dirname, './fixtures');

var globArray = [
'./whatsgoingon/key/isaidhey/whatsgoingon/test.txt',
'./whatsgoingon/hey/isaidhey/whatsgoingon/test.txt',
'./test.coffee',
'./whatsgoingon/test.js'
];
Expand All @@ -97,7 +97,7 @@ describe('glob-stream', function() {
var baseDir = join(__dirname, './fixtures');

var globArray = [
'./whatsgoingon/key/isaidhey/whatsgoingon/test.txt',
'./whatsgoingon/hey/isaidhey/whatsgoingon/test.txt',
'./test.coffee',
'./whatsgoingon/test.js'
];
Expand Down Expand Up @@ -307,7 +307,7 @@ describe('glob-stream', function() {
var baseDir = join(__dirname, './fixtures');

var globArray = [
'./whatsgoingon/key/isaidhey/whatsgoingon/test.txt',
'./whatsgoingon/hey/isaidhey/whatsgoingon/test.txt',
'./test.coffee',
'./whatsgoingon/test.js'
];
Expand Down Expand Up @@ -484,5 +484,45 @@ describe('glob-stream', function() {
gs.create.bind(gs, ['!a', '!b'], {cwd: __dirname}).should.throw(/Missing positive glob/);
});

it('should emit error on singular glob when file not found', function(done) {
var stream = gs.create('notfound');
should.exist(stream);
stream.on('error', function(err) {
err.should.match(/File not found with singular glob/);
done();
});
});

it('should emit error when a glob in multiple globs not found', function(done) {
var stream = gs.create(['notfound', './fixtures/whatsgoingon'], {cwd: __dirname});
should.exist(stream);
stream.on('error', function(err) {
err.should.match(/File not found with singular glob/);
done();
});
});

it('should not emit error on glob containing {} when not found', function(done) {
var stream = gs.create('notfound{a,b}');
should.exist(stream);
stream.on('error', function() {
throw new Error('Error was emitted');
});

stream.resume();
stream.once('end', done);
});

it('should not emit error on singular glob when allowEmpty is true', function(done) {
var stream = gs.create('notfound', { allowEmpty: true });
should.exist(stream);
stream.on('error', function() {
throw new Error('Error was emitted');
});

stream.resume();
stream.once('end', done);
});

});
});

0 comments on commit 1895396

Please sign in to comment.