From 0c0585987d57244c016bbedcfbcdaaa041b3f082 Mon Sep 17 00:00:00 2001 From: joshhunt Date: Fri, 22 Mar 2024 15:38:51 +0000 Subject: [PATCH 1/4] fix: Traverse symlink folders --- index.js | 12 +++++++++++ test/fixtures/symlinks/file-a.txt | 1 + test/fixtures/symlinks/symlink-dest | 1 + test/index.js | 31 +++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 test/fixtures/symlinks/file-a.txt create mode 120000 test/fixtures/symlinks/symlink-dest diff --git a/index.js b/index.js index cc3abdd..43aaf6d 100644 --- a/index.js +++ b/index.js @@ -70,10 +70,22 @@ function walkdir() { function processDirent(dirent) { var nextpath = path.join(filepath, dirent.name); + ee.emit('path', nextpath, dirent); if (dirent.isDirectory()) { queue.push(nextpath); + } else if (dirent.isSymbolicLink()) { + // If it's a symlink, check if the symlink points to a directory + fs.stat(nextpath, function (err, stats) { + if (err) { + return cb(err); + } + + if (stats.isDirectory()) { + queue.push(nextpath); + } + }); } } } diff --git a/test/fixtures/symlinks/file-a.txt b/test/fixtures/symlinks/file-a.txt new file mode 100644 index 0000000..4ef30bb --- /dev/null +++ b/test/fixtures/symlinks/file-a.txt @@ -0,0 +1 @@ +file a diff --git a/test/fixtures/symlinks/symlink-dest b/test/fixtures/symlinks/symlink-dest new file mode 120000 index 0000000..f2e2d0e --- /dev/null +++ b/test/fixtures/symlinks/symlink-dest @@ -0,0 +1 @@ +../whatsgoingon \ No newline at end of file diff --git a/test/index.js b/test/index.js index d3194e3..273f2b6 100644 --- a/test/index.js +++ b/test/index.js @@ -736,6 +736,37 @@ function suite(moduleName) { done ); }); + + it.only('traverses symlinked directories', function (done) { + var expected = [ + { + cwd: dir, + base: dir + '/fixtures/symlinks', + path: dir + '/fixtures/symlinks/file-a.txt', + }, + { + cwd: dir, + base: dir + '/fixtures/symlinks', + path: + dir + + '/fixtures/symlinks/symlink-dest/hey/isaidhey/whatsgoingon/test.txt', + }, + ]; + + function assert(pathObjs) { + expect(pathObjs.length).toBe(3); + expect(pathObjs).toContainEqual(expected[0]); + expect(pathObjs).toContainEqual(expected[1]); + } + + stream.pipeline( + [ + globStream(['./fixtures/symlinks/**/*.txt'], { cwd: dir }), + concat(assert), + ], + done + ); + }); }); describe('options', function () { From e6c022d967bd58afdb718d6d0d7f1a0bdb27f490 Mon Sep 17 00:00:00 2001 From: joshhunt Date: Fri, 22 Mar 2024 15:42:05 +0000 Subject: [PATCH 2/4] less newline --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index 43aaf6d..773b784 100644 --- a/index.js +++ b/index.js @@ -70,7 +70,6 @@ function walkdir() { function processDirent(dirent) { var nextpath = path.join(filepath, dirent.name); - ee.emit('path', nextpath, dirent); if (dirent.isDirectory()) { From 8b6146ba6c7749b6925ce342f1fef7dcf84939c2 Mon Sep 17 00:00:00 2001 From: joshhunt Date: Fri, 22 Mar 2024 16:00:19 +0000 Subject: [PATCH 3/4] fix the tests i missed --- test/index.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/test/index.js b/test/index.js index 273f2b6..11a7927 100644 --- a/test/index.js +++ b/test/index.js @@ -334,6 +334,18 @@ function suite(moduleName) { base: dir + '/fixtures', path: dir + '/fixtures/stuff/test.dmc', }, + { + cwd: dir, + base: dir + '/fixtures', + path: dir + '/fixtures/symlinks/symlink-dest/test.js', + }, + { + cwd: dir, + base: dir + '/fixtures', + path: + dir + + '/fixtures/symlinks/symlink-dest/hey/isaidhey/whatsgoingon/test.txt', + }, ]; var globs = [ @@ -344,12 +356,14 @@ function suite(moduleName) { ]; function assert(pathObjs) { - expect(pathObjs.length).toEqual(5); + expect(pathObjs.length).toEqual(7); expect(pathObjs).toContainEqual(expected[0]); expect(pathObjs).toContainEqual(expected[1]); expect(pathObjs).toContainEqual(expected[2]); expect(pathObjs).toContainEqual(expected[3]); expect(pathObjs).toContainEqual(expected[4]); + expect(pathObjs).toContainEqual(expected[5]); + expect(pathObjs).toContainEqual(expected[6]); } stream.pipeline([globStream(globs, { cwd: dir }), concat(assert)], done); @@ -737,7 +751,7 @@ function suite(moduleName) { ); }); - it.only('traverses symlinked directories', function (done) { + it('traverses symlinked directories', function (done) { var expected = [ { cwd: dir, @@ -754,7 +768,7 @@ function suite(moduleName) { ]; function assert(pathObjs) { - expect(pathObjs.length).toBe(3); + expect(pathObjs.length).toBe(2); expect(pathObjs).toContainEqual(expected[0]); expect(pathObjs).toContainEqual(expected[1]); } From 619084d9205a4985b56edeb7975a95fcf5d10a92 Mon Sep 17 00:00:00 2001 From: joshhunt Date: Fri, 22 Mar 2024 16:06:20 +0000 Subject: [PATCH 4/4] test when fs.stat errors --- test/index.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/index.js b/test/index.js index 11a7927..c87c057 100644 --- a/test/index.js +++ b/test/index.js @@ -1084,6 +1084,28 @@ function suite(moduleName) { stream.pipeline([gs, concat()], assert); }); + it('destroys the stream if walker errors when following symlink', function (done) { + var expectedError = new Error('Stubbed error'); + + var gs = globStream('./fixtures/**/*.dmc', { cwd: dir }); + + function stubError(dirpath, cb) { + cb(expectedError); + } + + var spy = sinon.spy(gs, 'destroy'); + sinon.stub(fs, 'stat').callsFake(stubError); + + function assert(err) { + sinon.restore(); + expect(spy.called).toEqual(true); + expect(err).toBe(expectedError); + done(); + } + + stream.pipeline([gs, concat()], assert); + }); + it('does not emit an error if stream is destroyed without an error', function (done) { var gs = globStream('./fixtures/**/*.dmc', { cwd: dir });