Skip to content

Commit f2bffcd

Browse files
authored
Change format handling and add tests (#1)
1 parent 85e628e commit f2bffcd

File tree

10 files changed

+1034
-60
lines changed

10 files changed

+1034
-60
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
[![npm](https://img.shields.io/npm/v/serverless-stack-output.svg)](https://www.npmjs.com/package/serverless-stack-output)
44
[![license](https://img.shields.io/github/license/sbstjn/serverless-stack-output.svg)](https://github.com/sbstjn/serverless-stack-output/blob/master/LICENSE.md)
5+
[![CircleCI](https://img.shields.io/circleci/project/github/sbstjn/serverless-stack-output.svg)](https://circleci.com/gh/sbstjn/serverless-stack-output)
6+
[![Coveralls](https://img.shields.io/coveralls/sbstjn/serverless-stack-output.svg)](https://coveralls.io/github/sbstjn/serverless-stack-output)
57

68
A [serverless](https://serverless.com) plugin to store output from your AWS CloudFormation Stack in JSON/YAML/TOML files, or to pass the output to a JavaScript function for further processing.
79

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"author": "Sebastian Müller <[email protected]>",
66
"main": "src/plugin.js",
77
"scripts": {
8-
"test": "jest test",
9-
"test:cover": "jest test --coverage",
8+
"test": "yarn lint && jest test",
9+
"test:cover": "yarn lint && jest test --coverage",
1010
"coveralls": "cat ./coverage/lcov.info | coveralls",
1111
"lint": "standard | snazzy"
1212
},
@@ -27,6 +27,9 @@
2727
"output"
2828
],
2929
"dependencies": {
30+
"jasmine-data-provider": "^2.2.0",
31+
"nativefier": "^7.4.0",
32+
"sinon": "^2.3.6",
3033
"tomlify-j0.4": "^2.0.0",
3134
"yamljs": "^0.3.0"
3235
},

src/file.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,28 @@
11
const fs = require('fs')
22

3-
const formats = {
4-
TYPE_YAML: 'yaml',
5-
TYPE_TOML: 'toml',
6-
TYPE_JSON: 'json'
7-
}
8-
93
class File {
104
constructor (path) {
115
this.path = path
126
}
137

14-
type () {
8+
format (data) {
159
const ext = this.path.split('.').pop()
1610

1711
switch (ext.toUpperCase()) {
12+
case 'JSON':
13+
return JSON.stringify(data, null, 2)
14+
case 'TOML':
15+
return require('tomlify-j0.4')(data, null, 0)
1816
case 'YAML':
1917
case 'YML':
20-
return formats.TYPE_YAML
21-
case 'TOML':
22-
return formats.TYPE_TOML
23-
case 'JSON':
24-
return formats.TYPE_JSON
18+
return require('yamljs').stringify(data)
2519
default:
2620
throw new Error('No formatter found for `' + ext + '` extension')
2721
}
2822
}
2923

30-
format (data) {
31-
const formatter = require('./formats/' + this.type()).format
32-
33-
return formatter(data)
34-
}
35-
36-
save (data, options) {
37-
const content = this.format(data, options)
24+
save (data) {
25+
const content = this.format(data)
3826

3927
try {
4028
fs.writeFileSync(this.path, content)

src/formats/json.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/formats/toml.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/formats/yaml.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Plugin {
2828
}
2929

3030
hasConfig (key) {
31-
return this.serverless.service.custom.output || !!this.serverless.service.custom.output[key]
31+
return !!this.serverless.service.custom.output && !!this.serverless.service.custom.output[key]
3232
}
3333

3434
hasHandler () {

test/file.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict'
2+
3+
const using = require('jasmine-data-provider')
4+
const File = require('../src/file.js')
5+
6+
describe('File', () => {
7+
describe('Constructor', () => {
8+
it('pass path', () => {
9+
const f = new File(__dirname)
10+
expect(f.path).toBe(__dirname)
11+
})
12+
})
13+
14+
describe('Format', () => {
15+
using(
16+
[
17+
{file: 'test.yaml', valid: true, type: 'yaml', data: `foo: bar\n`},
18+
{file: 'test.yml', valid: true, type: 'yaml', data: `foo: bar\n`},
19+
{file: 'test.json', valid: true, type: 'json', data: `{\n "foo": "bar"\n}`},
20+
{file: 'test.toml', valid: true, type: 'toml', data: 'foo = "bar"'},
21+
{file: 'test.zip', valid: false}
22+
],
23+
data => {
24+
it('detects' + (data.valid ? ' valid ' : ' invalid ') + data.file, () => {
25+
const f = new File(data.file)
26+
27+
if (data.valid) {
28+
expect(f.format({ foo: 'bar' })).toBe(data.data)
29+
} else {
30+
expect(() => f.format()).toThrow()
31+
}
32+
})
33+
}
34+
)
35+
})
36+
})

test/plugin.spec.js

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,74 @@
11
'use strict'
22

3+
const sinon = require('sinon')
34
const Plugin = require('../')
45

5-
it('Works', () => {
6-
expect(new Plugin({})).toHaveProperty('serverless')
6+
describe('Plugin', () => {
7+
let providerMock = null
8+
let getProvider = null
9+
let provider = {
10+
request: () => true,
11+
sdk: {
12+
VERSION: '2.21.0'
13+
}
14+
}
15+
16+
beforeEach(() => {
17+
providerMock = sinon.mock(provider)
18+
getProvider = sinon.stub().returns(provider)
19+
})
20+
21+
afterEach(() => {
22+
providerMock.restore()
23+
})
24+
25+
describe('Configuration', () => {
26+
it('hasHandler', () => {
27+
const config = {
28+
cli: { log: () => {} },
29+
region: 'us-east-1',
30+
service: {
31+
provider: {
32+
name: 'aws'
33+
},
34+
custom: {
35+
output: {
36+
handler: 'foo/bar.baz'
37+
}
38+
}
39+
},
40+
getProvider
41+
}
42+
43+
const test = new Plugin(config)
44+
45+
expect(test.hasHandler()).toBe(true)
46+
expect(test.hasFile()).toBe(false)
47+
})
48+
})
49+
50+
describe('Configuration', () => {
51+
it('hasFile', () => {
52+
const config = {
53+
cli: { log: () => {} },
54+
region: 'us-east-1',
55+
service: {
56+
provider: {
57+
name: 'aws'
58+
},
59+
custom: {
60+
output: {
61+
file: './foo/bar.toml'
62+
}
63+
}
64+
},
65+
getProvider
66+
}
67+
68+
const test = new Plugin(config)
69+
70+
expect(test.hasHandler()).toBe(false)
71+
expect(test.hasFile()).toBe(true)
72+
})
73+
})
774
})

0 commit comments

Comments
 (0)