added tesst for gen command
parent
6d6021b793
commit
95dbfc31b6
|
@ -81,6 +81,13 @@ function generateTemplate(config) {
|
||||||
return template
|
return template
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function writeToFile(config, template) {
|
||||||
|
console.trace('cmd.gen.writeToFile')
|
||||||
|
console.debug(`Write to ${config.output}`)
|
||||||
|
|
||||||
|
fs.writeFileSync(config.output, JSON.stringify(template, null, '\t'))
|
||||||
|
}
|
||||||
|
|
||||||
function prepare(config) {
|
function prepare(config) {
|
||||||
console.trace('cmd.gen.prepare')
|
console.trace('cmd.gen.prepare')
|
||||||
prepareUserDir(config)
|
prepareUserDir(config)
|
||||||
|
@ -106,12 +113,18 @@ module.exports = {
|
||||||
'--template <template-path>': 'Path to a template spec to use for generation. (optional)',
|
'--template <template-path>': 'Path to a template spec to use for generation. (optional)',
|
||||||
'--info <info-path>': "Path to an info template. (optional)",
|
'--info <info-path>': "Path to an info template. (optional)",
|
||||||
'--annotations <annotations-path>': "Path to an annotations template. (optional)",
|
'--annotations <annotations-path>': "Path to an annotations template. (optional)",
|
||||||
'--out <output-path>': "Path to output the generated template. (optional)"
|
'--out <output-path>': "Path to output the generated template. (optional)",
|
||||||
|
'--quiet, -q': "Do not create the spec file, just output it to stdout. (optional)"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
run(args) {
|
run(args) {
|
||||||
let config = prepare({ args })
|
let config = prepare({ args })
|
||||||
let spec = generateTemplate(config)
|
let spec = generateTemplate(config)
|
||||||
|
|
||||||
|
if(!args.q && !args.quiet) {
|
||||||
|
writeToFile(config, spec)
|
||||||
|
}
|
||||||
|
|
||||||
return spec
|
return spec
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,51 +2,70 @@ const assert = require('assert').strict
|
||||||
const loader = require('../src/loader')
|
const loader = require('../src/loader')
|
||||||
const logging = require('../src/logging')
|
const logging = require('../src/logging')
|
||||||
const expect = require('chai').expect
|
const expect = require('chai').expect
|
||||||
|
const fs = require('fs')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
logging.init(-1)
|
logging.init(-1)
|
||||||
const commands = loader.load()
|
const commands = loader.load()
|
||||||
|
|
||||||
|
|
||||||
describe("gen command", () => {
|
describe("gen command", () => {
|
||||||
it('throws an error on missing name', () => {
|
|
||||||
expect(() => commands.gen.run({ })).to.throw('Name must not be empty!')
|
const test_spec = {
|
||||||
})
|
|
||||||
it('throws an error on missing description', () => {
|
|
||||||
expect(() => commands.gen.run({ name: 'testename' })).to.throw('Description must not be empty!')
|
|
||||||
})
|
|
||||||
it('generates a spec', () => {
|
|
||||||
let spec = commands.gen.run({
|
|
||||||
name: 'test-service',
|
name: 'test-service',
|
||||||
description: 'Description for service.',
|
description: 'Description for service.',
|
||||||
baseurl: 'https://nclazz.de',
|
baseurl: 'https://nclazz.de',
|
||||||
namspace: 'de.nclazz'
|
namespace: 'de.nclazz' ,
|
||||||
});
|
quiet: true
|
||||||
|
}
|
||||||
|
|
||||||
|
it('throws an error on missing name', () => {
|
||||||
|
expect(() => commands.gen.run({ quiet: true })).to.throw('Name must not be empty!')
|
||||||
|
})
|
||||||
|
it('throws an error on missing description', () => {
|
||||||
|
expect(() => commands.gen.run({ name: 'testename', quiet: true })).to.throw('Description must not be empty!')
|
||||||
|
})
|
||||||
|
it('generates a spec', () => {
|
||||||
|
let spec = commands.gen.run(test_spec);
|
||||||
expect(spec).to.not.equal(null)
|
expect(spec).to.not.equal(null)
|
||||||
expect(spec).to.not.equal(undefined)
|
expect(spec).to.not.equal(undefined)
|
||||||
expect(spec).to.not.equal({})
|
expect(spec).to.not.equal({})
|
||||||
})
|
})
|
||||||
it('includes [name, description, base_url, namespace] as top level properties', () => {
|
it('includes [name, description, base_url, namespace] as top level properties', () => {
|
||||||
let spec = commands.gen.run({
|
let spec = commands.gen.run(test_spec);
|
||||||
name: 'test-service',
|
|
||||||
description: 'Description for service.',
|
|
||||||
baseurl: 'https://nclazz.de',
|
|
||||||
namespace: 'de.nclazz'
|
|
||||||
});
|
|
||||||
expect(spec).to.have.ownProperty('name')
|
expect(spec).to.have.ownProperty('name')
|
||||||
expect(spec).to.have.ownProperty('description')
|
expect(spec).to.have.ownProperty('description')
|
||||||
expect(spec).to.have.ownProperty('base_url')
|
expect(spec).to.have.ownProperty('base_url')
|
||||||
expect(spec).to.have.ownProperty('namespace')
|
expect(spec).to.have.ownProperty('namespace')
|
||||||
})
|
})
|
||||||
it('correctly propagates [name, description, base_url, namespace] to spec', () => {
|
it('correctly propagates [name, description, base_url, namespace] to spec', () => {
|
||||||
let spec = commands.gen.run({
|
let spec = commands.gen.run(test_spec);
|
||||||
name: 'test-service',
|
|
||||||
description: 'Description for service.',
|
|
||||||
baseurl: 'https://nclazz.de',
|
|
||||||
namespace: 'de.nclazz'
|
|
||||||
});
|
|
||||||
expect(spec.name).to.equal('test-service')
|
expect(spec.name).to.equal('test-service')
|
||||||
expect(spec.description).to.equal('Description for service.')
|
expect(spec.description).to.equal('Description for service.')
|
||||||
expect(spec.base_url).to.equal('https://nclazz.de')
|
expect(spec.base_url).to.equal('https://nclazz.de')
|
||||||
expect(spec.namespace).to.equal('de.nclazz')
|
expect(spec.namespace).to.equal('de.nclazz')
|
||||||
})
|
})
|
||||||
|
it('does not creates a file in quiet mode', () => {
|
||||||
|
let spec = commands.gen.run({
|
||||||
|
...test_spec,
|
||||||
|
out: './test-spec.json'
|
||||||
|
});
|
||||||
|
expect(fs.existsSync('./test-spec.json')).to.equal(false)
|
||||||
|
})
|
||||||
|
it('creates a file with valid json spec', () => {
|
||||||
|
let spec_path = path.join(__dirname, 'test-spec.json')
|
||||||
|
commands.gen.run({
|
||||||
|
...test_spec,
|
||||||
|
out: spec_path,
|
||||||
|
quiet: false
|
||||||
|
});
|
||||||
|
expect(fs.existsSync(spec_path)).to.equal(true)
|
||||||
|
let spec_json = require(spec_path)
|
||||||
|
expect(spec_json).to.have.ownProperty('name')
|
||||||
|
expect(spec_json).to.have.ownProperty('description')
|
||||||
|
expect(spec_json).to.have.ownProperty('base_url')
|
||||||
|
expect(spec_json).to.have.ownProperty('namespace')
|
||||||
|
fs.unlinkSync(spec_path)
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
Loading…
Reference in New Issue