mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-19 21:57:51 +00:00
Implement fsUtils.isDirectory() helper
This commit is contained in:
parent
aef90a18e8
commit
86b3489a48
@ -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())
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user