From 64e66e229fbeae6176db77fb61d93fa24eab72be Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Fri, 7 Nov 2014 12:42:02 -0400 Subject: [PATCH] Implement DataPrefix module --- lib/data/data-prefix.coffee | 15 +++++++++ lib/data/data-prefix.spec.coffee | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 lib/data/data-prefix.coffee create mode 100644 lib/data/data-prefix.spec.coffee diff --git a/lib/data/data-prefix.coffee b/lib/data/data-prefix.coffee new file mode 100644 index 00000000..0ea125f7 --- /dev/null +++ b/lib/data/data-prefix.coffee @@ -0,0 +1,15 @@ +fsUtils = require('../fs-utils/fs-utils') + +prefix = null + +exports.get = -> + return prefix + +exports.set = (newPrefix) -> + if not fsUtils.isValidPath(newPrefix) + throw new Error('Invalid path') + + prefix = newPrefix + +exports.clear = -> + prefix = null diff --git a/lib/data/data-prefix.spec.coffee b/lib/data/data-prefix.spec.coffee new file mode 100644 index 00000000..71cef879 --- /dev/null +++ b/lib/data/data-prefix.spec.coffee @@ -0,0 +1,55 @@ +expect = require('chai').expect +_ = require('lodash') +dataPrefix = require('./data-prefix') + +PREFIXES = + main: '~/.resin' + new: '~/.resin-new' + invalid: { path: '~/.resin' } + +describe 'DataPrefix', -> + + describe 'given no prefix', -> + + beforeEach -> + dataPrefix.clear() + + describe '#get()', -> + + it 'should return nothing', -> + expect(dataPrefix.get()).to.not.exist + + describe '#set()', -> + + it 'should be able to set a prefix', -> + expect(dataPrefix.get()).to.not.exist + dataPrefix.set(PREFIXES.main) + expect(dataPrefix.get()).to.equal(PREFIXES.main) + + it 'should throw an error if passing an invalid path', -> + setInvalidPrefix = _.partial(dataPrefix.set, PREFIXES.invalid) + expect(setInvalidPrefix).to.throw(Error) + + describe 'given a prefix', -> + + beforeEach -> + dataPrefix.set(PREFIXES.main) + + describe '#get()', -> + + it 'should return the saved prefix', -> + expect(dataPrefix.get()).to.equal(PREFIXES.main) + + describe '#set()', -> + + it 'should be able to override the prefix', -> + expect(dataPrefix.get()).to.equal(PREFIXES.main) + dataPrefix.set(PREFIXES.new) + expect(dataPrefix.get()).to.equal(PREFIXES.new) + + describe '#clear()', -> + + it 'should clear the prefix', -> + expect(dataPrefix.get()).to.equal(PREFIXES.main) + dataPrefix.clear() + expect(dataPrefix.get()).to.not.exist