Implement fsUtils.isDirectory() helper

This commit is contained in:
Juan Cruz Viotti 2014-11-14 12:58:05 -04:00
parent aef90a18e8
commit 86b3489a48
2 changed files with 44 additions and 0 deletions

View File

@ -1,5 +1,11 @@
fs = require('fs')
_ = require('lodash')
# TODO: There should be more complex checks here
exports.isValidPath = (p) ->
return _.isString(p)
exports.isDirectory = (directory, callback) ->
fs.stat directory, (error, stats) ->
return callback?(error) if error?
return callback?(null, stats.isDirectory())

View File

@ -1,6 +1,15 @@
expect = require('chai').expect
mockFs = require('mock-fs')
fsUtils = require('./fs-utils')
FILESYSTEM =
text:
name: '/tmp/text'
contents: 'Hello World'
directory:
name: '/tmp/directory'
contents: mockFs.directory()
describe 'FsUtils:', ->
describe '#isValidPath()', ->
@ -25,3 +34,32 @@ describe 'FsUtils:', ->
'./file/../file2'
]
expect(fsUtils.isValidPath(validPath)).to.be.true
describe '#isDirectory()', ->
beforeEach ->
mockFsOptions = {}
for key, value of FILESYSTEM
mockFsOptions[value.name] = value.contents
mockFs(mockFsOptions)
afterEach ->
mockFs.restore()
it 'should return true if directory', (done) ->
fsUtils.isDirectory FILESYSTEM.directory.name, (error, isDirectory) ->
expect(error).to.not.exist
expect(isDirectory).to.be.true
done()
it 'should return false if not a directory', (done) ->
fsUtils.isDirectory FILESYSTEM.text.name, (error, isDirectory) ->
expect(error).to.not.exist
expect(isDirectory).to.be.false
done()
it 'should return an error if the path doesn\'t exists', (done) ->
fsUtils.isDirectory '/nonexistantpath', (error, isDirectory) ->
expect(error).to.exist
expect(isDirectory).to.be.undefined
done()