From 0b71d70af9f418b9d1e70b50a3528990002fa553 Mon Sep 17 00:00:00 2001 From: Brian Woodward Date: Tue, 23 Aug 2016 18:13:14 -0400 Subject: [PATCH] Fix: Ensure basePath is resolved first (fixes gulpjs/gulp#1744) (#67) --- index.js | 15 ++++++---- test/fixtures/has (parens)/test.dmc | 1 + test/main.js | 45 ++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/has (parens)/test.dmc diff --git a/index.js b/index.js index 3662f57..f795959 100644 --- a/index.js +++ b/index.js @@ -15,17 +15,18 @@ var gs = { // Creates a stream for a single glob or filter createStream: function(ourGlob, negatives, opt) { - // Remove path relativity to make globs make sense - ourGlob = resolveGlob(ourGlob, opt); var ourOpt = extend({}, opt); delete ourOpt.root; + // Extract base path from glob + var basePath = ourOpt.base || getBasePath(ourGlob, opt); + + // Remove path relativity to make globs make sense + ourGlob = resolveGlob(ourGlob, opt); + // Create globbing stuff var globber = new glob.Glob(ourGlob, ourOpt); - // Extract base path from glob - var basePath = opt.base || globParent(ourGlob) + path.sep; - // Create stream and map events from globber to it var stream = through2.obj(opt, negatives.length ? filterNegatives : undefined); @@ -188,4 +189,8 @@ function globIsSingular(glob) { }); } +function getBasePath(ourGlob, opt) { + return resolveGlob(globParent(ourGlob) + path.sep, opt); +} + module.exports = gs; diff --git a/test/fixtures/has (parens)/test.dmc b/test/fixtures/has (parens)/test.dmc new file mode 100644 index 0000000..a8a9406 --- /dev/null +++ b/test/fixtures/has (parens)/test.dmc @@ -0,0 +1 @@ +this is a test \ No newline at end of file diff --git a/test/main.js b/test/main.js index e1b58a1..468c43a 100644 --- a/test/main.js +++ b/test/main.js @@ -61,6 +61,46 @@ describe('glob-stream', function() { }); }); + it('should handle ( ) in directory paths', function(done) { + var cwd = join(__dirname, './fixtures/has (parens)'); + var stream = gs.create('*.dmc', { cwd: cwd }); + should.exist(stream); + stream.on('error', function(err) { + throw err; + }); + stream.on('data', function(file) { + should.exist(file); + should.exist(file.path); + should.exist(file.base); + should.exist(file.cwd); + String(file.cwd).should.equal(cwd); + String(file.base).should.equal(cwd + sep); + String(join(file.path,'')).should.equal(join(cwd, 'test.dmc')); + done(); + }); + }); + + it('should find files in paths that contain ( )', function(done) { + var stream = gs.create('./fixtures/**/*.dmc', { cwd: __dirname }); + 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(3); + path.basename(files[0].path).should.equal('test.dmc'); + files[0].path.should.equal(join(__dirname, 'fixtures/has (parens)/test.dmc')); + path.basename(files[1].path).should.equal('run.dmc'); + files[1].path.should.equal(join(__dirname, 'fixtures/stuff/run.dmc')); + path.basename(files[2].path).should.equal('test.dmc'); + files[2].path.should.equal(join(__dirname, 'fixtures/stuff/test.dmc')); + done(); + }); + }); + it('should return a file name stream from a glob and respect state', function(done) { var stream = gs.create('./fixtures/stuff/*.dmc', { cwd: __dirname }); var wrapper = stream.pipe(through2.obj(function(data, enc, cb) { @@ -274,6 +314,7 @@ describe('glob-stream', function() { join(__dirname, './fixtures/**/test.txt'), join(__dirname, './fixtures/**/test.coffee'), join(__dirname, './fixtures/**/test.js'), + join(__dirname, './fixtures/**/test.dmc'), ]; var stream = gs.create(globArray, { cwd: __dirname }); @@ -285,10 +326,12 @@ describe('glob-stream', function() { files.push(file); }); stream.on('end', function() { - files.length.should.equal(3); + files.length.should.equal(5); path.basename(files[0].path).should.equal('test.txt'); path.basename(files[1].path).should.equal('test.coffee'); path.basename(files[2].path).should.equal('test.js'); + path.basename(files[3].path).should.equal('test.dmc'); + path.basename(files[4].path).should.equal('test.dmc'); done(); }); });