diff --git a/lib/components/services/localfilestorage/index.js b/lib/components/services/localfilestorage/index.js index 68a203b..e2ade36 100644 --- a/lib/components/services/localfilestorage/index.js +++ b/lib/components/services/localfilestorage/index.js @@ -2,9 +2,13 @@ const dottie = require('dottie') class LocalFilestorage { boot (options) { + this.rootPath_ = configuredRootPath(options) + const cloudstorage = cloudstorageService(options) cloudstorage.registerProvider(this) - } + } // boot + + get rootPath() { return this.rootPath_ } } // class LocalFilestorage function cloudstorageService (options) { @@ -16,8 +20,18 @@ function cloudstorageService (options) { return options.bootedServices.cloudstorage } // cloudstorageService +function configuredRootPath (options) { + const configKey = 'config.localstorage.rootPath' + const fromConfig = dottie.get(options, configKey) + const fromEnv = process.env.TYMLY_LOCALSTORAGE_ROOTPATH + + const rootPath = fromConfig || fromEnv + if (!rootPath) bootOops(`Could not configure root path from ${configKey} or TYMLY_LOCALSTORAGE_ROOTPATH.`) + return rootPath +} // configuredRootPath + function bootOops (msg) { - throw new Error(`Can not register localfilestorage. ${msg}`) + throw new Error(`Can not boot localfilestorage. ${msg}`) } // bootOops module.exports = { diff --git a/test/boot-service-test.js b/test/boot-service-test.js index f507b03..4e1e429 100644 --- a/test/boot-service-test.js +++ b/test/boot-service-test.js @@ -9,11 +9,11 @@ const LocalStorageService = require('../lib/components/services/localfilestorage describe('Boot localstorage service', () => { let localstorage let options - const onBoot = () => { localstorage.boot(options) } + const onBoot = () => { + localstorage.boot(options) + } beforeEach (() => { - console.log('yes') - localstorage = new LocalStorageService() options = { @@ -21,46 +21,79 @@ describe('Boot localstorage service', () => { cloudstorage: { registerProvider: () => { } } + }, + config: { + localstorage: { + rootPath: '/from/config' + } } } + }) // beforeEach + + afterEach (() => { + delete process.env.TYMLY_LOCALSTORAGE_ROOTPATH }) describe('good boots', () => { + it('picks up filesystem root directory from config', () => { + localstorage.boot(options) + expect(localstorage.rootPath).to.be.a('string') + }) + + it('picks up filesystem root directory from environment', () => { + delete options.config.localstorage + process.env.TYMLY_LOCALSTORAGE_ROOTPATH='/from/env' + + localstorage.boot(options) + expect(localstorage.rootPath).to.eql('/from/env') + }) + + it('prefer config to environment variable', () => { + process.env.TYMLY_LOCALSTORAGE_ROOTPATH='/from/env' + + localstorage.boot(options) + expect(localstorage.rootPath).to.eql('/from/config') + }) + it('localstorage registers with cloudstorage on boot', () => { let registered = false options.bootedServices.cloudstorage.registerProvider = provider => { registered = (provider === localstorage) } localstorage.boot(options) + expect(registered).to.be.true() }) + }) - it('picks up filesystem root directory from config', () => { - localstorage.boot(options) - expect(localstorage.rootPath).to.be.a('string') + describe('bad boots', () => { + it ('loudly fail if path config is missing', () => { + delete options.config.localstorage + + expect(onBoot).to.throw(/could not configure/i) }) - it('picks up filesystem root directory from environment', () => { - localstorage.boot(options) - expect(localstorage.rootPath).to.be.a('string') + it ("don't register if config is missing", () => { + delete options.config.localstorage + + let registered = false + options.bootedServices.cloudstorage.registerProvider = + provider => { registered = (provider === localstorage) } + + expect(onBoot).to.throw(/could not configure/i) + expect(registered).to.be.false() }) - }) - describe('bad boots', () => { it('loudly fail if cloudstorage is not available', () => { delete options.bootedServices.cloudstorage - expect(onBoot).to.throw(/can not register/i) + expect(onBoot).to.throw(/can't find cloudstorage/i) }) it ("loudly fail if cloudstorage isn't right", () => { delete options.bootedServices.cloudstorage.registerProvider - expect(onBoot).to.throw(/can not register/i) - }) - - it ('loudly fail if path config is missing', () => { - expect(onBoot).to.throw(/missing config/i) + expect(onBoot).to.throw(/doesn't have registerProvider/i) }) }) })