Skip to content

Commit

Permalink
fix: Traverse symlink folders (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshhunt authored Mar 24, 2024
1 parent 8450d16 commit d49d9bd
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ function walkdir() {

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);
}
});
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/symlinks/file-a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file a
1 change: 1 addition & 0 deletions test/fixtures/symlinks/symlink-dest
69 changes: 68 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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);
Expand Down Expand Up @@ -736,6 +750,37 @@ function suite(moduleName) {
done
);
});

it('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(2);
expect(pathObjs).toContainEqual(expected[0]);
expect(pathObjs).toContainEqual(expected[1]);
}

stream.pipeline(
[
globStream(['./fixtures/symlinks/**/*.txt'], { cwd: dir }),
concat(assert),
],
done
);
});
});

describe('options', function () {
Expand Down Expand Up @@ -1039,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 });

Expand Down

0 comments on commit d49d9bd

Please sign in to comment.