Skip to content

Commit

Permalink
Merge 9e67c2a into 33db74d
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Mar 22, 2015
2 parents 33db74d + 9e67c2a commit 3aee5bc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 39 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -24,3 +25,4 @@ function read(root, filter, files, prefix) {
function noDotFiles(x) {
return x[0] !== '.'
}

104 changes: 65 additions & 39 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
Expand All @@ -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)
}
})
})

0 comments on commit 3aee5bc

Please sign in to comment.