Skip to content

Commit

Permalink
Fix: Avoid mutating the options passed in (closes #75)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Feb 21, 2017
1 parent 9911598 commit f369978
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 52 deletions.
29 changes: 16 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,26 @@ function globStream(globs, opt) {
if (!opt) {
opt = {};
}
if (typeof opt.cwd !== 'string') {
opt.cwd = process.cwd();

var ourOpt = extend({}, opt);

if (typeof ourOpt.cwd !== 'string') {
ourOpt.cwd = process.cwd();
}
if (typeof opt.dot !== 'boolean') {
opt.dot = false;
if (typeof ourOpt.dot !== 'boolean') {
ourOpt.dot = false;
}
if (typeof opt.silent !== 'boolean') {
opt.silent = true;
if (typeof ourOpt.silent !== 'boolean') {
ourOpt.silent = true;
}
if (typeof opt.nonull !== 'boolean') {
opt.nonull = false;
if (typeof ourOpt.nonull !== 'boolean') {
ourOpt.nonull = false;
}
if (typeof opt.cwdbase !== 'boolean') {
opt.cwdbase = false;
if (typeof ourOpt.cwdbase !== 'boolean') {
ourOpt.cwdbase = false;
}
if (opt.cwdbase) {
opt.base = opt.cwd;
if (ourOpt.cwdbase) {
ourOpt.base = ourOpt.cwd;
}

// Only one glob no need to aggregate
Expand Down Expand Up @@ -79,7 +82,7 @@ function globStream(globs, opt) {
function streamFromPositive(positive) {
var negativeGlobs = negatives.filter(indexGreaterThan(positive.index))
.map(toGlob);
return createStream(positive.glob, negativeGlobs, opt);
return createStream(positive.glob, negativeGlobs, ourOpt);
}
}

Expand Down
100 changes: 61 additions & 39 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,52 +625,74 @@ describe('glob-stream', function() {
});
});

describe('ignore option', function() {
describe('options', function() {

it('should support the ignore option instead of negation', function(done) {
var expectedPath = join(__dirname, './fixtures/stuff/run.dmc');
var glob = join(__dirname, './fixtures/stuff/*.dmc');
var stream = globStream(glob, { cwd: __dirname, ignore: ['./fixtures/stuff/test.dmc'] });
it('avoids mutation of options', function(done) {

var files = [];
stream.on('error', done);
stream.on('data', function(file) {
should.exist(file);
should.exist(file.path);
files.push(file);
});
stream.on('end', function() {
files.length.should.equal(1);
files[0].path.should.equal(expectedPath);
done();
});
var defaultedOpts = {
cwd: process.cwd(),
dot: false,
silent: true,
nonull: false,
cwdbase: false,
};

var opts = {};

var stream = globStream(join(__dirname, './fixtures/stuff/run.dmc'), opts);
Object.keys(opts).length.should.equal(0);
opts.should.not.eql(defaultedOpts);
stream.on('data', function() {});
stream.on('end', done);
});

it('should support the ignore option with dot option', function(done) {
var stream = globStream('./fixtures/*swag', { cwd: __dirname, dot: true, ignore: ['./fixtures/**'] });
should.exist(stream);
stream.on('error', function(err) {
throw err;
});
stream.once('data', function(file) {
throw new Error('file ' + file.path + ' should have been negated');
describe('ignore', function() {

it('should support the ignore option instead of negation', function(done) {
var expectedPath = join(__dirname, './fixtures/stuff/run.dmc');
var glob = join(__dirname, './fixtures/stuff/*.dmc');
var stream = globStream(glob, { cwd: __dirname, ignore: ['./fixtures/stuff/test.dmc'] });

var files = [];
stream.on('error', done);
stream.on('data', function(file) {
should.exist(file);
should.exist(file.path);
files.push(file);
});
stream.on('end', function() {
files.length.should.equal(1);
files[0].path.should.equal(expectedPath);
done();
});
});
stream.once('end', done);
});

it('should merge ignore option and negative globs', function(done) {
var globArray = [
'./fixtures/stuff/*.dmc',
'!./fixtures/stuff/test.dmc',
];
var stream = globStream(globArray, { cwd: __dirname, ignore: ['./fixtures/stuff/run.dmc'] });
should.exist(stream);
stream.on('error', function(err) {
throw err;
it('should support the ignore option with dot option', function(done) {
var stream = globStream('./fixtures/*swag', { cwd: __dirname, dot: true, ignore: ['./fixtures/**'] });
should.exist(stream);
stream.on('error', function(err) {
throw err;
});
stream.once('data', function(file) {
throw new Error('file ' + file.path + ' should have been negated');
});
stream.once('end', done);
});
stream.once('data', function(file) {
throw new Error('file ' + file.path + ' should have been negated');

it('should merge ignore option and negative globs', function(done) {
var globArray = [
'./fixtures/stuff/*.dmc',
'!./fixtures/stuff/test.dmc',
];
var stream = globStream(globArray, { cwd: __dirname, ignore: ['./fixtures/stuff/run.dmc'] });
should.exist(stream);
stream.on('error', function(err) {
throw err;
});
stream.once('data', function(file) {
throw new Error('file ' + file.path + ' should have been negated');
});
stream.once('end', done);
});
stream.once('end', done);
});
});

0 comments on commit f369978

Please sign in to comment.