Make data.prefix.set() async

This commit is contained in:
Juan Cruz Viotti 2014-11-17 15:20:19 -04:00
parent 5eeb4e04ee
commit 11998a63cd
4 changed files with 23 additions and 17 deletions

View File

@ -5,11 +5,12 @@ prefix = null
exports.get = ->
return prefix
exports.set = (newPrefix) ->
exports.set = (newPrefix, callback) ->
if not fsUtils.isValidPath(newPrefix)
throw new Error('Invalid path')
return callback?(new Error('Invalid path'))
prefix = newPrefix
return callback?(null, prefix)
exports.clear = ->
prefix = null

View File

@ -22,19 +22,22 @@ describe 'DataPrefix:', ->
describe '#set()', ->
it 'should be able to set a prefix', ->
it 'should be able to set a prefix', (done) ->
expect(dataPrefix.get()).to.not.exist
dataPrefix.set(PREFIXES.main)
expect(dataPrefix.get()).to.equal(PREFIXES.main)
dataPrefix.set PREFIXES.main, (error) ->
expect(error).to.not.exist
expect(dataPrefix.get()).to.equal(PREFIXES.main)
done()
it 'should throw an error if passing an invalid path', ->
setInvalidPrefix = _.partial(dataPrefix.set, PREFIXES.invalid)
expect(setInvalidPrefix).to.throw(Error)
it 'should throw an error if passing an invalid path', (done) ->
dataPrefix.set PREFIXES.invalid, (error) ->
expect(error).to.be.an.instanceof(Error)
done()
describe 'given a prefix', ->
beforeEach ->
dataPrefix.set(PREFIXES.main)
beforeEach (done) ->
dataPrefix.set(PREFIXES.main, done)
describe '#get()', ->
@ -43,10 +46,12 @@ describe 'DataPrefix:', ->
describe '#set()', ->
it 'should be able to override the prefix', ->
it 'should be able to override the prefix', (done) ->
expect(dataPrefix.get()).to.equal(PREFIXES.main)
dataPrefix.set(PREFIXES.new)
expect(dataPrefix.get()).to.equal(PREFIXES.new)
dataPrefix.set PREFIXES.new, (error) ->
expect(error).to.not.exist
expect(dataPrefix.get()).to.equal(PREFIXES.new)
done()
describe '#clear()', ->

View File

@ -58,9 +58,9 @@ describe 'Data:', ->
describe 'given a prefix', ->
beforeEach ->
beforeEach (done) ->
mock.fs.init(FILESYSTEM)
data.prefix.set(config.dataPrefix)
data.prefix.set(config.dataPrefix, done)
afterEach ->
mock.fs.restore()

View File

@ -39,9 +39,9 @@ describe 'FsUtils:', ->
describe '#isDirectory()', ->
beforeEach ->
beforeEach (done) ->
mock.fs.init(FILESYSTEM)
data.prefix.set(config.dataPrefix)
data.prefix.set(config.dataPrefix, done)
afterEach ->
mock.fs.restore()