Skip to content

Commit

Permalink
fix(service): Pick up config, then register
Browse files Browse the repository at this point in the history
  • Loading branch information
jezhiggins committed May 29, 2020
1 parent 1e6102a commit 249053f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
18 changes: 16 additions & 2 deletions lib/components/services/localfilestorage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 = {
Expand Down
67 changes: 50 additions & 17 deletions test/boot-service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,91 @@ 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 = {
bootedServices: {
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)
})
})
})

0 comments on commit 249053f

Please sign in to comment.