From 4d1951b2be17eddc38a819bc76a07e02be639065 Mon Sep 17 00:00:00 2001 From: Joel Peterson Date: Tue, 1 Jan 2019 14:14:30 -0600 Subject: [PATCH 1/2] fixed several issues with readdirPromise. * it was emitting the end event prior to the promise completing so the emitted files was an empty array * excluded files were still getting returned in the resolved promise's value, so they are now filtered out prior to completing the promise --- lib/iterators.js | 5 ++- lib/readers.js | 6 ++- test/fixtures/a/c/d/e/f/g/h/h.js | 0 test/fixtures/a/c/d/e/f/i/i.js | 0 test/gitignore.js | 1 + test/promise.js | 70 ++++++++++++++++++++++++++++++++ test/slashes.js | 2 +- 7 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/a/c/d/e/f/g/h/h.js create mode 100644 test/fixtures/a/c/d/e/f/i/i.js create mode 100644 test/promise.js diff --git a/lib/iterators.js b/lib/iterators.js index f287210..51dd1ea 100644 --- a/lib/iterators.js +++ b/lib/iterators.js @@ -223,7 +223,7 @@ module.exports = function (app) { // emit excluded file if (file.exclude === true) { self.emit('exclude', file); - return file.path; + return null; } // emit included file @@ -238,6 +238,9 @@ module.exports = function (app) { return file.path; }); }) + .filter(function(file) { + return file !== null; + }) .reduce(function (acc, files) { return acc.concat(files); diff --git a/lib/readers.js b/lib/readers.js index 919fe9f..896637e 100644 --- a/lib/readers.js +++ b/lib/readers.js @@ -99,8 +99,10 @@ module.exports = function (app) { this.emit('read'); this.setPattern(pattern, options); var res = this.iteratorPromise(this.pattern.base); - this.emit('end', this.files); - return res; + return res.then(() => { + this.emit('end', this.files); + return this.files; + }); } }); }; diff --git a/test/fixtures/a/c/d/e/f/g/h/h.js b/test/fixtures/a/c/d/e/f/g/h/h.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/a/c/d/e/f/i/i.js b/test/fixtures/a/c/d/e/f/i/i.js new file mode 100644 index 0000000..e69de29 diff --git a/test/gitignore.js b/test/gitignore.js index f5e1d1e..3aa92a8 100644 --- a/test/gitignore.js +++ b/test/gitignore.js @@ -27,6 +27,7 @@ describe('gitignore', function () { }); it('should recurse into node_modules when it\'s specified in the glob pattern:', function () { + glob = new Glob({ gitignore: false }) glob.readdirSync('./node_modules/micromatch/*.js').should.containDeep(['node_modules/micromatch/index.js']); }); diff --git a/test/promise.js b/test/promise.js new file mode 100644 index 0000000..185a339 --- /dev/null +++ b/test/promise.js @@ -0,0 +1,70 @@ +'use strict'; + +var should = require('should'); +var Glob = require('..'); +var path = require('path'); +var orig = process.cwd(); +var glob; + +describe("promise", function () { + before(function () { + process.chdir(__dirname + '/fixtures'); + }); + + after(function () { + process.chdir(orig); + }); + var mm = require('micromatch'); + + beforeEach(function () { + glob = new Glob(); + + glob.on('file', function (file) { + // console.log(mm.isMatch(file.relative, 'a/*/*/*/**/')) + }); + + glob.on('read', function () { + glob.files = []; + }); + }); + + it('should only return directories when the pattern ends in a slash:', function () { + return glob.readdirPromise('a/*/').then(function(files) { + (files.length).should.eql(3); + (files.includes('a/b/')).should.eql(true); + (files.includes('a/c/')).should.eql(true); + (files.includes('a/bc/')).should.eql(true); + }); + }); + + it('should not resolve with excluded matches:', function () { + function exclude(file) { + file.exclude = (file.path.indexOf('bc') === -1); + return file; + } + + return glob.use(exclude).readdirPromise('a/*/').then(function(files) { + (files.length).should.eql(1); + (files.includes('a/bc/')).should.eql(true); + }); + }); + + it('should only return files that match and not the directories containing them:', function () { + return glob.readdirPromise('a/bc/e/f/*').then(function(files) { + (files.length).should.eql(1); + (files.includes('a/bc/e/f/fff.js')).should.eql(true); + }); + }); + + it('should emit the correct values when completed:', function () { + var emittedFiles = []; + glob.on('end', function(files) { + emittedFiles = files; + }); + + return glob.readdirPromise('a/bc/e/f/*').then(function(files) { + (emittedFiles.length).should.eql(1); + (emittedFiles.includes('a/bc/e/f/fff.js')).should.eql(true); + }); + }); +}); diff --git a/test/slashes.js b/test/slashes.js index 599ff3b..97f49fc 100644 --- a/test/slashes.js +++ b/test/slashes.js @@ -6,7 +6,7 @@ var path = require('path'); var orig = process.cwd(); var glob; -describe("root", function () { +describe("slashes", function () { before(function () { process.chdir(__dirname + '/fixtures'); }); From ae3d2e2e00074b4f1931505491ee80f222f0d30b Mon Sep 17 00:00:00 2001 From: Joel Peterson Date: Tue, 1 Jan 2019 14:16:54 -0600 Subject: [PATCH 2/2] fixed ordering of filter and reduce so that reduce can flatten the arrays prior to filtering --- lib/iterators.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/iterators.js b/lib/iterators.js index 51dd1ea..5e4b60f 100644 --- a/lib/iterators.js +++ b/lib/iterators.js @@ -238,13 +238,14 @@ module.exports = function (app) { return file.path; }); }) - .filter(function(file) { - return file !== null; - }) + .reduce(function (acc, files) { return acc.concat(files); - }, []); + }, []) + .filter(function(file) { + return file !== null; + }); }, });