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

99 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-11-26 18:22:01 +00:00
fsUtils = require('./fs-utils/fs-utils')
rimraf = require('rimraf')
2014-11-07 16:42:02 +00:00
dataPrefix = require('./data-prefix')
2014-11-26 17:42:05 +00:00
config = require('../config')
2014-11-26 16:56:52 +00:00
mock = require('../../../tests/utils/mock')
2014-11-07 16:42:02 +00:00
PREFIXES =
2014-11-14 19:48:37 +00:00
main: config.dataPrefix
new: "#{config.dataPrefix}-new"
invalid: { path: '/abc' }
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 ->
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) ->
rimraf(PREFIXES.main, 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-11-17 19:20:19 +00:00
dataPrefix.set PREFIXES.main, (error) ->
expect(error).to.not.exist
expect(dataPrefix.get()).to.equal(PREFIXES.main)
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) ->
dataPrefix.set PREFIXES.invalid, (error) ->
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) ->
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()
2014-11-07 16:42:02 +00:00
describe 'given a prefix', ->
2014-11-17 19:20:19 +00:00
beforeEach (done) ->
dataPrefix.set(PREFIXES.main, done)
2014-11-07 16:42:02 +00:00
describe '#get()', ->
it 'should return the saved prefix', ->
expect(dataPrefix.get()).to.equal(PREFIXES.main)
describe '#set()', ->
2014-11-17 19:20:19 +00:00
it 'should be able to override the prefix', (done) ->
2014-11-07 16:42:02 +00:00
expect(dataPrefix.get()).to.equal(PREFIXES.main)
2014-11-17 19:20:19 +00:00
dataPrefix.set PREFIXES.new, (error) ->
expect(error).to.not.exist
expect(dataPrefix.get()).to.equal(PREFIXES.new)
done()
2014-11-07 16:42:02 +00:00
describe '#clear()', ->
it 'should clear the prefix', ->
expect(dataPrefix.get()).to.equal(PREFIXES.main)
dataPrefix.clear()
expect(dataPrefix.get()).to.not.exist