mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-18 21:27:51 +00:00
Implement fsUtils module
This commit is contained in:
parent
41d73eaf98
commit
89bb0d0b21
29
lib/fs-utils/fs-utils.coffee
Normal file
29
lib/fs-utils/fs-utils.coffee
Normal file
@ -0,0 +1,29 @@
|
||||
fs = require('fs')
|
||||
_ = require('lodash')
|
||||
async = require('async')
|
||||
|
||||
exports.isValidPath = (p) ->
|
||||
return _.isString(p)
|
||||
|
||||
exports.isFile = (file, callback) ->
|
||||
async.waterfall([
|
||||
|
||||
# Check if the file exists
|
||||
(callback) ->
|
||||
fs.exists file, (exists) ->
|
||||
if not exists
|
||||
|
||||
# Return false anyway to avoid confusions
|
||||
return callback(new Error('File doesn\'t exists'), false)
|
||||
|
||||
return callback(null, exists)
|
||||
|
||||
# Get file stats
|
||||
(exists, callback) ->
|
||||
fs.stat(file, callback)
|
||||
|
||||
# Check if it's a file
|
||||
(stats, callback) ->
|
||||
return callback(null, !!stats.isFile())
|
||||
|
||||
], callback)
|
67
lib/fs-utils/fs-utils.spec.coffee
Normal file
67
lib/fs-utils/fs-utils.spec.coffee
Normal file
@ -0,0 +1,67 @@
|
||||
expect = require('chai').expect
|
||||
_ = require('lodash')
|
||||
mockFs = require('mock-fs')
|
||||
|
||||
fsUtils = require('./fs-utils')
|
||||
|
||||
PATHS =
|
||||
file:
|
||||
name: '/tmp/file1'
|
||||
contents: 'File1 contents'
|
||||
directory:
|
||||
name: '/tmp/dir'
|
||||
contents: mockFs.directory()
|
||||
|
||||
describe 'FsUtils', ->
|
||||
|
||||
describe '#isValidPath()', ->
|
||||
|
||||
it 'should return false for invalid paths', ->
|
||||
|
||||
for invalidPath in [
|
||||
{ hello: 'world' }
|
||||
1234
|
||||
[ 1, 2, 3 ]
|
||||
undefined
|
||||
null
|
||||
]
|
||||
expect(fsUtils.isValidPath(invalidPath)).to.be.false
|
||||
|
||||
it 'should return true for valid paths', ->
|
||||
|
||||
for validPath in [
|
||||
'/Users/johndoe'
|
||||
'~/.resin'
|
||||
'../parent'
|
||||
'./file/../file2'
|
||||
]
|
||||
expect(fsUtils.isValidPath(validPath)).to.be.true
|
||||
|
||||
describe '#isFile()', ->
|
||||
|
||||
beforeEach ->
|
||||
mockFsOptions = {}
|
||||
for key, value of PATHS
|
||||
mockFsOptions[value.name] = value.contents
|
||||
mockFs(mockFsOptions)
|
||||
|
||||
afterEach ->
|
||||
mockFs.restore()
|
||||
|
||||
it 'should return true for files', (done) ->
|
||||
fsUtils.isFile PATHS.file.name, (error, isFile) ->
|
||||
expect(error).to.not.exist
|
||||
expect(isFile).to.be.true
|
||||
done()
|
||||
|
||||
it 'should return false if file doesn\'t exists', (done) ->
|
||||
fsUtils.isFile '/nonexistentfile', (error, isFile) ->
|
||||
expect(error).to.exist
|
||||
expect(isFile).to.be.false
|
||||
done()
|
||||
|
||||
it 'should return false if path is a directory', (done) ->
|
||||
fsUtils.isFile PATHS.directory.name, (error, isFile) ->
|
||||
expect(error).to.not.exist
|
||||
expect(isFile).to.be.false
|
||||
done()
|
@ -21,10 +21,13 @@
|
||||
"coffee-script": "~1.8.0",
|
||||
"mocha-notifier-reporter": "~0.1.0",
|
||||
"gulp-coffeelint": "~0.4.0",
|
||||
"nock": "~0.48.2"
|
||||
"nock": "~0.48.2",
|
||||
"mock-fs": "~2.3.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"request": "~2.47.0",
|
||||
"url-join": "0.0.1"
|
||||
"url-join": "0.0.1",
|
||||
"lodash": "~2.4.1",
|
||||
"async": "~0.9.0"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user