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

Emit error on file not found with singular globs #41

Merged
merged 8 commits into from
Mar 16, 2015
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errors should propagate downstream you shouldnt need to do this afaik

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seemed weird to me as well, but the tests fail without it: the error is just thrown instead of being emitted on the stream.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@callumacrae 🤷 as long as it works haha, we can revisit later

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);
});

});
});