balena-cli/lib/resin/data/data-prefix.spec.coffee

97 lines
2.3 KiB
CoffeeScript
Raw Normal View History

2014-11-07 16:42:02 +00:00
expect = require('chai').expect
_ = require('lodash')
async = require('async')
fs = require('fs')
2014-12-08 15:02:23 +00:00
fsPlus = require('fs-plus')
rimraf = require('rimraf')
2014-11-07 16:42:02 +00:00
dataPrefix = require('./data-prefix')
settings = require('../settings')
2014-11-26 16:56:52 +00:00
mock = require('../../../tests/utils/mock')
2014-11-07 16:42:02 +00:00
2014-11-07 16:43:10 +00:00
describe 'DataPrefix:', ->
2014-11-07 16:42:02 +00:00
beforeEach ->
2014-12-05 16:52:31 +00:00
@prefix = settings.get('dataPrefix')
mock.fs.init()
afterEach ->
mock.fs.restore()
2014-11-07 16:42:02 +00:00
describe 'given no prefix', ->
beforeEach ->
dataPrefix.clear()
describe '#get()', ->
it 'should return nothing', ->
expect(dataPrefix.get()).to.not.exist
describe '#set()', ->
beforeEach (done) ->
2014-12-05 16:52:31 +00:00
rimraf(@prefix, done)
2014-11-17 19:20:19 +00:00
it 'should be able to set a prefix', (done) ->
2014-11-07 16:42:02 +00:00
expect(dataPrefix.get()).to.not.exist
2014-12-05 16:52:31 +00:00
dataPrefix.set @prefix, (error) =>
2014-11-17 19:20:19 +00:00
expect(error).to.not.exist
2014-12-05 16:52:31 +00:00
expect(dataPrefix.get()).to.equal(@prefix)
2014-11-17 19:20:19 +00:00
done()
2014-11-07 16:42:02 +00:00
2014-11-17 19:20:19 +00:00
it 'should throw an error if passing an invalid path', (done) ->
2014-12-05 16:52:31 +00:00
dataPrefix.set { path: '/abc' }, (error) ->
2014-11-17 19:20:19 +00:00
expect(error).to.be.an.instanceof(Error)
done()
2014-11-07 16:42:02 +00:00
it 'should create the directory if it doesn\'t exist', (done) ->
async.waterfall [
(callback) ->
2014-12-05 16:52:31 +00:00
fs.exists @prefix, (exists) ->
return callback(null, exists)
2014-12-05 16:52:31 +00:00
(exists, callback) =>
expect(exists).to.be.false
2014-12-05 16:52:31 +00:00
dataPrefix.set(@prefix, callback)
2014-12-05 16:52:31 +00:00
(callback) =>
2014-12-08 15:02:23 +00:00
fsPlus.isDirectory @prefix, (isDirectory) ->
return callback(null, isDirectory)
(isDirectory, callback) ->
expect(isDirectory).to.be.true
return callback()
], (error) ->
expect(error).to.not.exist
done()
2014-11-07 16:42:02 +00:00
describe 'given a prefix', ->
2014-11-17 19:20:19 +00:00
beforeEach (done) ->
2014-12-05 16:52:31 +00:00
dataPrefix.set(@prefix, done)
2014-11-07 16:42:02 +00:00
describe '#get()', ->
it 'should return the saved prefix', ->
2014-12-05 16:52:31 +00:00
expect(dataPrefix.get()).to.equal(@prefix)
2014-11-07 16:42:02 +00:00
describe '#set()', ->
2014-11-17 19:20:19 +00:00
it 'should be able to override the prefix', (done) ->
2014-12-05 16:52:31 +00:00
newPrefix = "#{settings.get('dataPrefix')}-new"
expect(dataPrefix.get()).to.equal(@prefix)
dataPrefix.set newPrefix, (error) ->
2014-11-17 19:20:19 +00:00
expect(error).to.not.exist
2014-12-05 16:52:31 +00:00
expect(dataPrefix.get()).to.equal(newPrefix)
2014-11-17 19:20:19 +00:00
done()
2014-11-07 16:42:02 +00:00
describe '#clear()', ->
it 'should clear the prefix', ->
2014-12-05 16:52:31 +00:00
expect(dataPrefix.get()).to.equal(@prefix)
2014-11-07 16:42:02 +00:00
dataPrefix.clear()
expect(dataPrefix.get()).to.not.exist