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 = -> exports.get = ->
return prefix return prefix
exports.set = (newPrefix) -> exports.set = (newPrefix, callback) ->
if not fsUtils.isValidPath(newPrefix) if not fsUtils.isValidPath(newPrefix)
throw new Error('Invalid path') return callback?(new Error('Invalid path'))
prefix = newPrefix prefix = newPrefix
return callback?(null, prefix)
exports.clear = -> exports.clear = ->
prefix = null prefix = null

View File

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

View File

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

View File

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