Skip to content

Commit d49d9bd

Browse files
authored
fix: Traverse symlink folders (#122)
1 parent 8450d16 commit d49d9bd

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ function walkdir() {
7474

7575
if (dirent.isDirectory()) {
7676
queue.push(nextpath);
77+
} else if (dirent.isSymbolicLink()) {
78+
// If it's a symlink, check if the symlink points to a directory
79+
fs.stat(nextpath, function (err, stats) {
80+
if (err) {
81+
return cb(err);
82+
}
83+
84+
if (stats.isDirectory()) {
85+
queue.push(nextpath);
86+
}
87+
});
7788
}
7889
}
7990
}

test/fixtures/symlinks/file-a.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file a
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../whatsgoingon

test/index.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,18 @@ function suite(moduleName) {
334334
base: dir + '/fixtures',
335335
path: dir + '/fixtures/stuff/test.dmc',
336336
},
337+
{
338+
cwd: dir,
339+
base: dir + '/fixtures',
340+
path: dir + '/fixtures/symlinks/symlink-dest/test.js',
341+
},
342+
{
343+
cwd: dir,
344+
base: dir + '/fixtures',
345+
path:
346+
dir +
347+
'/fixtures/symlinks/symlink-dest/hey/isaidhey/whatsgoingon/test.txt',
348+
},
337349
];
338350

339351
var globs = [
@@ -344,12 +356,14 @@ function suite(moduleName) {
344356
];
345357

346358
function assert(pathObjs) {
347-
expect(pathObjs.length).toEqual(5);
359+
expect(pathObjs.length).toEqual(7);
348360
expect(pathObjs).toContainEqual(expected[0]);
349361
expect(pathObjs).toContainEqual(expected[1]);
350362
expect(pathObjs).toContainEqual(expected[2]);
351363
expect(pathObjs).toContainEqual(expected[3]);
352364
expect(pathObjs).toContainEqual(expected[4]);
365+
expect(pathObjs).toContainEqual(expected[5]);
366+
expect(pathObjs).toContainEqual(expected[6]);
353367
}
354368

355369
stream.pipeline([globStream(globs, { cwd: dir }), concat(assert)], done);
@@ -736,6 +750,37 @@ function suite(moduleName) {
736750
done
737751
);
738752
});
753+
754+
it('traverses symlinked directories', function (done) {
755+
var expected = [
756+
{
757+
cwd: dir,
758+
base: dir + '/fixtures/symlinks',
759+
path: dir + '/fixtures/symlinks/file-a.txt',
760+
},
761+
{
762+
cwd: dir,
763+
base: dir + '/fixtures/symlinks',
764+
path:
765+
dir +
766+
'/fixtures/symlinks/symlink-dest/hey/isaidhey/whatsgoingon/test.txt',
767+
},
768+
];
769+
770+
function assert(pathObjs) {
771+
expect(pathObjs.length).toBe(2);
772+
expect(pathObjs).toContainEqual(expected[0]);
773+
expect(pathObjs).toContainEqual(expected[1]);
774+
}
775+
776+
stream.pipeline(
777+
[
778+
globStream(['./fixtures/symlinks/**/*.txt'], { cwd: dir }),
779+
concat(assert),
780+
],
781+
done
782+
);
783+
});
739784
});
740785

741786
describe('options', function () {
@@ -1039,6 +1084,28 @@ function suite(moduleName) {
10391084
stream.pipeline([gs, concat()], assert);
10401085
});
10411086

1087+
it('destroys the stream if walker errors when following symlink', function (done) {
1088+
var expectedError = new Error('Stubbed error');
1089+
1090+
var gs = globStream('./fixtures/**/*.dmc', { cwd: dir });
1091+
1092+
function stubError(dirpath, cb) {
1093+
cb(expectedError);
1094+
}
1095+
1096+
var spy = sinon.spy(gs, 'destroy');
1097+
sinon.stub(fs, 'stat').callsFake(stubError);
1098+
1099+
function assert(err) {
1100+
sinon.restore();
1101+
expect(spy.called).toEqual(true);
1102+
expect(err).toBe(expectedError);
1103+
done();
1104+
}
1105+
1106+
stream.pipeline([gs, concat()], assert);
1107+
});
1108+
10421109
it('does not emit an error if stream is destroyed without an error', function (done) {
10431110
var gs = globStream('./fixtures/**/*.dmc', { cwd: dir });
10441111

0 commit comments

Comments
 (0)