Skip to content

Commit

Permalink
Check if stage in options overwrites configuration file (#11)
Browse files Browse the repository at this point in the history
* Check if stage in options overwrites configuration file

* Add tests for stage handling

* Add yarn task to run `$(npm bin)/standard --fix`

* Fix lint error
  • Loading branch information
sbstjn committed Jul 29, 2017
1 parent 2c21710 commit dd71652
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "0.0.0",
"main": "src/plugin.js",
"scripts": {
"fix": "standard --fix",
"test": "jest test",
"test:cover": "jest test --coverage",
"coveralls": "cat ./coverage/lcov.info | coveralls",
Expand Down
11 changes: 9 additions & 2 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ class Plugin {
*
* @param {object} serverless
*/
constructor (serverless) {
constructor (serverless, options) {
this.serverless = serverless

if (options && options.stage) {
this.stage = options.stage
} else {
this.stage = serverless.service.provider.stage
}

this.hooks = {
'deploy:compileEvents': this.beforeDeployResources.bind(this)
}
Expand Down Expand Up @@ -71,7 +78,7 @@ class Plugin {
*/
resources (table, index, config) {
const resources = []
const stage = this.serverless.service.provider.stage
const stage = this.stage
const data = this.defaults(config)

// Start processing configuration
Expand Down
18 changes: 16 additions & 2 deletions test/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Plugin = require('../')

describe('Normalize', () => {
it('converts everything to an array', () => {
const test = new Plugin()
const test = new Plugin({ service: { provider: { stage: 'foo' } } })

expect(test.normalize('test')).toEqual(['test'])
expect(test.normalize(['test'])).toEqual(['test'])
Expand All @@ -21,7 +21,7 @@ describe('Defaults', () => {
write: { minimum: 20 }
}

const test = new Plugin()
const test = new Plugin({ service: { provider: { stage: 'foo' } } })
const data = test.defaults(config)

expect(data.read.minimum).toBe(5)
Expand All @@ -33,3 +33,17 @@ describe('Defaults', () => {
expect(data.write.usage).toBe(0.75)
})
})

describe('Stage', () => {
it('uses stage from configuration', () => {
const p = new Plugin({ service: { provider: { stage: 'foo' } } })

expect(p.stage).toBe('foo')
})

it('overwrites stage from options', () => {
const p = new Plugin({ service: { provider: { stage: 'foo' } } }, { stage: 'bar' })

expect(p.stage).toBe('bar')
})
})

0 comments on commit dd71652

Please sign in to comment.