diff --git a/index.js b/index.js index f50540d..cf91acb 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ function read(root, filter, files, prefix) { filter = filter || noDotFiles var dir = path.join(root, prefix) + if (!fs.existsSync(dir)) return if (fs.statSync(dir).isDirectory()) fs.readdirSync(dir) .filter(filter) @@ -24,3 +25,4 @@ function read(root, filter, files, prefix) { function noDotFiles(x) { return x[0] !== '.' } + diff --git a/test/test.js b/test/test.js index 1e344c8..77edd42 100644 --- a/test/test.js +++ b/test/test.js @@ -5,16 +5,15 @@ var should = require('should') var read = require('../') describe('fs.readdirSyncRecursive()', function () { - it('should work in the folder', function (done) { + it('should work in the folder', function () { var files = read(__dirname) files.length.should.equal(1) files[0].should.equal('test.js') - done() }) - it('should work at the root with a filter', function (done) { + it('should work at the root with a filter', function () { var files = read(path.join(__dirname, '..'), function (name) { return name[0] !== '.' && name !== 'node_modules' && name !== 'coverage' }) @@ -28,54 +27,81 @@ describe('fs.readdirSyncRecursive()', function () { 'README.md' ].sort()) - done() }) - it('should work with the symlinked file', function (done) { - var linkname = __filename + '-link' - fs.symlinkSync(__filename, linkname, 'file') + it('should work with the symlinked file', function () { + try { + var linkname = __filename + '-link' + fs.symlinkSync(__filename, linkname, 'file') - var files = read(__dirname).sort(); + var files = read(__dirname).sort() - files.length.should.equal(2) - files.should.eql(['test.js', 'test.js-link']) + files.length.should.equal(2) + files.should.eql(['test.js', 'test.js-link']) - fs.unlinkSync(linkname) - done() + } catch (err) { + throw err + } finally { + fs.unlinkSync(linkname) + } }) - it('should work in the symlinked directory', function (done) { - var linkname = __dirname + '-link' - fs.symlinkSync(__dirname, linkname, 'dir') + it('should work in the symlinked directory', function () { + try { + var linkname = __dirname + '-link' + fs.symlinkSync(__dirname, linkname, 'dir') - var files = read(linkname) + var files = read(linkname) - files.length.should.equal(1) - files[0].should.equal('test.js') + files.length.should.equal(1) + files[0].should.equal('test.js') - fs.unlinkSync(linkname) - done() + } catch (err) { + throw err + } finally { + fs.unlinkSync(linkname) + } }) - it('should work in the symlinked directory with a filter', function (done) { - var linkname = path.join(__dirname, '..') + '-link' - fs.symlinkSync(path.join(__dirname, '..'), linkname, 'dir') - - var files = read(linkname, function (name) { - return name[0] !== '.' && name !== 'node_modules' && name !== 'coverage' - }) - - files.length.should.equal(5) - files.sort().should.eql([ - 'test/test.js', - 'index.js', - 'LICENSE', - 'package.json', - 'README.md' - ].sort()) - - fs.unlinkSync(linkname) + it('should work in the symlinked directory with a filter', function () { + try { + var linkname = path.join(__dirname, '..') + '-link' + fs.symlinkSync(path.join(__dirname, '..'), linkname, 'dir') + + var files = read(linkname, function (name) { + return name[0] !== '.' && name !== 'node_modules' && name !== 'coverage' + }) + + files.length.should.equal(5) + files.sort().should.eql([ + 'test/test.js', + 'index.js', + 'LICENSE', + 'package.json', + 'README.md' + ].sort()) + } catch (err) { + throw err + } finally { + fs.unlinkSync(linkname) + } + }) - done() + it('should ignore non-exist symlinked inside', function () { + try { + var linkname = __filename + '-link' + var emptyname = __filename + '-empty' + fs.writeFileSync(emptyname, 'empty') + fs.symlinkSync(emptyname, linkname, 'dir') + fs.unlinkSync(emptyname) + + var files = read(__dirname) + + files.should.eql(['test.js']) + } catch (err) { + throw err + } finally { + fs.unlinkSync(linkname) + } }) })