2014-11-07 18:15:59 +00:00
|
|
|
expect = require('chai').expect
|
|
|
|
_ = require('lodash')
|
2014-11-26 18:22:01 +00:00
|
|
|
fsUtils = require('./fs-utils/fs-utils')
|
2014-11-26 16:56:52 +00:00
|
|
|
mock = require('../../../tests/utils/mock')
|
2014-11-14 17:19:37 +00:00
|
|
|
async = require('async')
|
2014-12-03 16:03:54 +00:00
|
|
|
settings = require('../settings')
|
2014-11-07 18:15:59 +00:00
|
|
|
data = require('./data')
|
|
|
|
|
|
|
|
FILES_FIXTURES =
|
|
|
|
hello:
|
|
|
|
filename: 'hello_world.test'
|
|
|
|
contents: 'Hello World!'
|
|
|
|
nested:
|
|
|
|
filename: 'nested/hello_world.test'
|
|
|
|
contents: 'Nested Hello World!'
|
|
|
|
|
|
|
|
FILESYSTEM =
|
|
|
|
text:
|
2014-12-05 14:53:59 +00:00
|
|
|
name: "#{settings.get('dataPrefix')}/text"
|
2014-11-07 18:15:59 +00:00
|
|
|
contents: 'Hello World'
|
|
|
|
key: 'text'
|
|
|
|
directory:
|
2014-12-05 14:53:59 +00:00
|
|
|
name: "#{settings.get('dataPrefix')}/directory"
|
2014-11-14 19:48:37 +00:00
|
|
|
contents: {}
|
2014-11-07 18:15:59 +00:00
|
|
|
key: 'directory'
|
|
|
|
nested:
|
2014-12-05 14:53:59 +00:00
|
|
|
name: "#{settings.get('dataPrefix')}/nested/text"
|
2014-11-07 18:15:59 +00:00
|
|
|
contents: 'Nested Hello World'
|
|
|
|
key: 'nested/text'
|
|
|
|
|
|
|
|
describe 'Data:', ->
|
|
|
|
|
|
|
|
describe 'given no prefix', ->
|
|
|
|
|
|
|
|
describe '#get()', ->
|
|
|
|
|
|
|
|
it 'should throw an error', ->
|
|
|
|
getDataKey = _.partial(data.get, 'foobar')
|
|
|
|
expect(getDataKey).to.throw(Error)
|
|
|
|
|
|
|
|
describe '#set()', ->
|
|
|
|
|
|
|
|
it 'should throw an error', ->
|
|
|
|
setDataKey = _.partial(data.set, 'foobar', 'Foo Bar!')
|
|
|
|
expect(setDataKey).to.throw(Error)
|
|
|
|
|
2014-11-14 17:19:37 +00:00
|
|
|
describe '#remove()', ->
|
|
|
|
|
|
|
|
it 'should throw an error', ->
|
|
|
|
removeDataKey = _.partial(data.remove, 'foobar')
|
|
|
|
expect(removeDataKey).to.throw(Error)
|
|
|
|
|
2014-11-14 17:34:04 +00:00
|
|
|
describe '#has()', ->
|
|
|
|
|
|
|
|
it 'should throw an error', ->
|
|
|
|
hasDataKey = _.partial(data.has, 'foobar')
|
|
|
|
expect(hasDataKey).to.throw(Error)
|
|
|
|
|
2014-11-07 18:15:59 +00:00
|
|
|
describe 'given a prefix', ->
|
|
|
|
|
2014-11-17 19:20:19 +00:00
|
|
|
beforeEach (done) ->
|
2014-11-14 19:48:37 +00:00
|
|
|
mock.fs.init(FILESYSTEM)
|
2014-12-05 14:53:59 +00:00
|
|
|
data.prefix.set(settings.get('dataPrefix'), done)
|
2014-11-07 18:15:59 +00:00
|
|
|
|
|
|
|
afterEach ->
|
2014-11-14 19:48:37 +00:00
|
|
|
mock.fs.restore()
|
2014-11-07 18:15:59 +00:00
|
|
|
data.prefix.clear()
|
|
|
|
|
|
|
|
describe '#get()', ->
|
|
|
|
|
|
|
|
it 'should be able to read a valid key', (done) ->
|
|
|
|
data.get FILESYSTEM.text.key, encoding: 'utf8', (error, value) ->
|
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(value).to.equal(FILESYSTEM.text.contents)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should be able to read a nested key', (done) ->
|
|
|
|
data.get FILESYSTEM.nested.key, encoding: 'utf8', (error, value) ->
|
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(value).to.equal(FILESYSTEM.nested.contents)
|
|
|
|
done()
|
|
|
|
|
2014-11-14 18:58:56 +00:00
|
|
|
it 'should return undefined if reading an invalid key', (done) ->
|
2014-11-07 18:15:59 +00:00
|
|
|
data.get 'nonexistantkey', encoding: 'utf8', (error, value) ->
|
2014-11-14 18:58:56 +00:00
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(value).to.be.undefined
|
2014-11-07 18:15:59 +00:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return an error if not reading a file', (done) ->
|
|
|
|
data.get FILESYSTEM.directory.key, encoding: 'utf8', (error, value) ->
|
|
|
|
expect(error).to.be.an.instanceof(Error)
|
|
|
|
expect(value).to.not.exist
|
|
|
|
done()
|
|
|
|
|
2014-11-14 18:56:52 +00:00
|
|
|
it 'should return undefined if key doesn\'t exists', (done) ->
|
|
|
|
data.get 'nontexistantkey', encoding: 'utf8', (error, value) ->
|
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(value).to.be.undefined
|
|
|
|
done()
|
|
|
|
|
2014-11-14 17:34:04 +00:00
|
|
|
describe '#has()', ->
|
|
|
|
|
|
|
|
it 'should return true if a file exists', (done) ->
|
|
|
|
data.has FILESYSTEM.text.key, (hasKey) ->
|
|
|
|
expect(hasKey).to.be.true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return true if a directory exists', (done) ->
|
|
|
|
data.has FILESYSTEM.directory.key, (hasKey) ->
|
|
|
|
expect(hasKey).to.be.true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return false if the file doesn\'t exists', (done) ->
|
|
|
|
data.has 'foobar', (hasKey) ->
|
|
|
|
expect(hasKey).to.be.false
|
|
|
|
done()
|
|
|
|
|
2014-11-07 18:15:59 +00:00
|
|
|
describe '#set()', ->
|
|
|
|
|
2014-11-14 17:19:37 +00:00
|
|
|
writeAndCheckFixture = (fixture) ->
|
|
|
|
return (done) ->
|
|
|
|
filename = fixture.filename
|
|
|
|
contents = fixture.contents
|
2014-11-07 18:15:59 +00:00
|
|
|
|
2014-11-14 17:19:37 +00:00
|
|
|
async.waterfall [
|
2014-11-07 18:15:59 +00:00
|
|
|
|
2014-11-14 17:19:37 +00:00
|
|
|
(callback) ->
|
|
|
|
data.get filename, encoding: 'utf8', (error, value) ->
|
2014-11-14 18:58:56 +00:00
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(value).to.be.undefined
|
2014-11-14 17:19:37 +00:00
|
|
|
return callback()
|
|
|
|
|
|
|
|
(callback) ->
|
|
|
|
data.set(filename, contents, encoding: 'utf8', callback)
|
2014-11-07 18:15:59 +00:00
|
|
|
|
2014-11-14 17:19:37 +00:00
|
|
|
(callback) ->
|
|
|
|
data.get(filename, encoding: 'utf8', callback)
|
|
|
|
|
|
|
|
(value, callback) ->
|
2014-11-07 18:15:59 +00:00
|
|
|
expect(value).to.equal(contents)
|
2014-11-14 17:19:37 +00:00
|
|
|
return callback()
|
2014-11-07 18:15:59 +00:00
|
|
|
|
2014-11-14 17:19:37 +00:00
|
|
|
], (error) ->
|
|
|
|
expect(error).to.not.exist
|
|
|
|
done()
|
2014-11-07 18:15:59 +00:00
|
|
|
|
2014-11-14 17:19:37 +00:00
|
|
|
it('should be able to write a file', writeAndCheckFixture(FILES_FIXTURES.hello))
|
|
|
|
it('should be able to write a nested file', writeAndCheckFixture(FILES_FIXTURES.nested))
|
|
|
|
|
|
|
|
describe '#remove()', ->
|
|
|
|
|
|
|
|
removeAndCheckFile = (file) ->
|
|
|
|
return (done) ->
|
|
|
|
key = file.key
|
|
|
|
|
|
|
|
async.waterfall [
|
2014-11-07 18:15:59 +00:00
|
|
|
|
2014-11-14 17:19:37 +00:00
|
|
|
(callback) ->
|
|
|
|
data.get(key, encoding: 'utf8', callback)
|
|
|
|
|
|
|
|
(value, callback) ->
|
|
|
|
expect(value).to.equal(file.contents)
|
|
|
|
data.remove(key, callback)
|
|
|
|
|
|
|
|
(callback) ->
|
|
|
|
data.get key, encoding: 'utf8', (error, value) ->
|
2014-11-14 18:58:56 +00:00
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(value).to.be.undefined
|
2014-11-14 17:19:37 +00:00
|
|
|
return callback()
|
|
|
|
|
|
|
|
], (error) ->
|
2014-11-07 18:15:59 +00:00
|
|
|
expect(error).to.not.exist
|
2014-11-14 17:19:37 +00:00
|
|
|
done()
|
2014-11-07 18:15:59 +00:00
|
|
|
|
2014-11-14 17:19:37 +00:00
|
|
|
it('should be able to remove a file', removeAndCheckFile(FILESYSTEM.text))
|
|
|
|
|
|
|
|
it('should be able to remove a nested file', removeAndCheckFile(FILESYSTEM.nested))
|
|
|
|
|
|
|
|
it 'should be able to remove a directory', (done) ->
|
|
|
|
directory = FILESYSTEM.directory
|
|
|
|
|
|
|
|
async.waterfall [
|
|
|
|
|
|
|
|
(callback) ->
|
|
|
|
fsUtils.isDirectory(directory.name, callback)
|
|
|
|
|
|
|
|
(isDirectory, callback) ->
|
|
|
|
expect(isDirectory).to.be.true
|
|
|
|
data.remove(directory.key, callback)
|
|
|
|
|
|
|
|
(callback) ->
|
2014-11-14 17:34:04 +00:00
|
|
|
data.has directory.key, (hasKey) ->
|
|
|
|
expect(hasKey).to.be.false
|
2014-11-14 17:19:37 +00:00
|
|
|
return callback()
|
|
|
|
|
|
|
|
], (error) ->
|
|
|
|
expect(error).to.not.exist
|
|
|
|
done()
|