Skip to content

Commit

Permalink
Change format handling and add tests (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbstjn authored Jun 30, 2017
1 parent 85e628e commit f2bffcd
Show file tree
Hide file tree
Showing 10 changed files with 1,034 additions and 60 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

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.

Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"author": "Sebastian Müller <[email protected]>",
"main": "src/plugin.js",
"scripts": {
"test": "jest test",
"test:cover": "jest test --coverage",
"test": "yarn lint && jest test",
"test:cover": "yarn lint && jest test --coverage",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"lint": "standard | snazzy"
},
Expand All @@ -27,6 +27,9 @@
"output"
],
"dependencies": {
"jasmine-data-provider": "^2.2.0",
"nativefier": "^7.4.0",
"sinon": "^2.3.6",
"tomlify-j0.4": "^2.0.0",
"yamljs": "^0.3.0"
},
Expand Down
28 changes: 8 additions & 20 deletions src/file.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
const fs = require('fs')

const formats = {
TYPE_YAML: 'yaml',
TYPE_TOML: 'toml',
TYPE_JSON: 'json'
}

class File {
constructor (path) {
this.path = path
}

type () {
format (data) {
const ext = this.path.split('.').pop()

switch (ext.toUpperCase()) {
case 'JSON':
return JSON.stringify(data, null, 2)
case 'TOML':
return require('tomlify-j0.4')(data, null, 0)
case 'YAML':
case 'YML':
return formats.TYPE_YAML
case 'TOML':
return formats.TYPE_TOML
case 'JSON':
return formats.TYPE_JSON
return require('yamljs').stringify(data)
default:
throw new Error('No formatter found for `' + ext + '` extension')
}
}

format (data) {
const formatter = require('./formats/' + this.type()).format

return formatter(data)
}

save (data, options) {
const content = this.format(data, options)
save (data) {
const content = this.format(data)

try {
fs.writeFileSync(this.path, content)
Expand Down
3 changes: 0 additions & 3 deletions src/formats/json.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/formats/toml.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/formats/yaml.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Plugin {
}

hasConfig (key) {
return this.serverless.service.custom.output || !!this.serverless.service.custom.output[key]
return !!this.serverless.service.custom.output && !!this.serverless.service.custom.output[key]
}

hasHandler () {
Expand Down
36 changes: 36 additions & 0 deletions test/file.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

const using = require('jasmine-data-provider')
const File = require('../src/file.js')

describe('File', () => {
describe('Constructor', () => {
it('pass path', () => {
const f = new File(__dirname)
expect(f.path).toBe(__dirname)
})
})

describe('Format', () => {
using(
[
{file: 'test.yaml', valid: true, type: 'yaml', data: `foo: bar\n`},
{file: 'test.yml', valid: true, type: 'yaml', data: `foo: bar\n`},
{file: 'test.json', valid: true, type: 'json', data: `{\n "foo": "bar"\n}`},
{file: 'test.toml', valid: true, type: 'toml', data: 'foo = "bar"'},
{file: 'test.zip', valid: false}
],
data => {
it('detects' + (data.valid ? ' valid ' : ' invalid ') + data.file, () => {
const f = new File(data.file)

if (data.valid) {
expect(f.format({ foo: 'bar' })).toBe(data.data)
} else {
expect(() => f.format()).toThrow()
}
})
}
)
})
})
71 changes: 69 additions & 2 deletions test/plugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,74 @@
'use strict'

const sinon = require('sinon')
const Plugin = require('../')

it('Works', () => {
expect(new Plugin({})).toHaveProperty('serverless')
describe('Plugin', () => {
let providerMock = null
let getProvider = null
let provider = {
request: () => true,
sdk: {
VERSION: '2.21.0'
}
}

beforeEach(() => {
providerMock = sinon.mock(provider)
getProvider = sinon.stub().returns(provider)
})

afterEach(() => {
providerMock.restore()
})

describe('Configuration', () => {
it('hasHandler', () => {
const config = {
cli: { log: () => {} },
region: 'us-east-1',
service: {
provider: {
name: 'aws'
},
custom: {
output: {
handler: 'foo/bar.baz'
}
}
},
getProvider
}

const test = new Plugin(config)

expect(test.hasHandler()).toBe(true)
expect(test.hasFile()).toBe(false)
})
})

describe('Configuration', () => {
it('hasFile', () => {
const config = {
cli: { log: () => {} },
region: 'us-east-1',
service: {
provider: {
name: 'aws'
},
custom: {
output: {
file: './foo/bar.toml'
}
}
},
getProvider
}

const test = new Plugin(config)

expect(test.hasHandler()).toBe(false)
expect(test.hasFile()).toBe(true)
})
})
})
Loading

0 comments on commit f2bffcd

Please sign in to comment.