Create prefix directory when using data.prefix.set

This commit is contained in:
Juan Cruz Viotti 2014-11-17 15:32:35 -04:00
parent 11998a63cd
commit 3c3cb3e850
3 changed files with 44 additions and 3 deletions

View File

@ -1,3 +1,4 @@
mkdirp = require('mkdirp')
fsUtils = require('../fs-utils/fs-utils')
prefix = null
@ -9,8 +10,10 @@ exports.set = (newPrefix, callback) ->
if not fsUtils.isValidPath(newPrefix)
return callback?(new Error('Invalid path'))
prefix = newPrefix
return callback?(null, prefix)
mkdirp newPrefix, (error) ->
return callback?(error) if error?
prefix = newPrefix
return callback?()
exports.clear = ->
prefix = null

View File

@ -1,7 +1,12 @@
expect = require('chai').expect
_ = require('lodash')
async = require('async')
fs = require('fs')
fsUtils = require('../fs-utils/fs-utils')
rimraf = require('rimraf')
dataPrefix = require('./data-prefix')
config = require('../config')
mock = require('../../tests/utils/mock')
PREFIXES =
main: config.dataPrefix
@ -10,6 +15,12 @@ PREFIXES =
describe 'DataPrefix:', ->
beforeEach ->
mock.fs.init()
afterEach ->
mock.fs.restore()
describe 'given no prefix', ->
beforeEach ->
@ -22,6 +33,9 @@ describe 'DataPrefix:', ->
describe '#set()', ->
beforeEach (done) ->
rimraf(PREFIXES.main, done)
it 'should be able to set a prefix', (done) ->
expect(dataPrefix.get()).to.not.exist
dataPrefix.set PREFIXES.main, (error) ->
@ -34,6 +48,29 @@ describe 'DataPrefix:', ->
expect(error).to.be.an.instanceof(Error)
done()
it 'should create the directory if it doesn\'t exist', (done) ->
async.waterfall [
(callback) ->
fs.exists PREFIXES.main, (exists) ->
return callback(null, exists)
(exists, callback) ->
expect(exists).to.be.false
dataPrefix.set(PREFIXES.main, callback)
(callback) ->
fsUtils.isDirectory(PREFIXES.main, callback)
(isDirectory, callback) ->
expect(isDirectory).to.be.true
return callback()
], (error) ->
expect(error).to.not.exist
done()
describe 'given a prefix', ->
beforeEach (done) ->

View File

@ -30,6 +30,7 @@
"lodash": "~2.4.1",
"async": "~0.9.0",
"rimraf": "~2.2.8",
"commander": "~2.5.0"
"commander": "~2.5.0",
"mkdirp": "~0.5.0"
}
}