added tesst for gen command

feature/linter
Niclas Thobaben 2021-07-07 20:34:08 +02:00
parent 6d6021b793
commit 95dbfc31b6
2 changed files with 53 additions and 21 deletions

View File

@ -81,6 +81,13 @@ function generateTemplate(config) {
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) {
console.trace('cmd.gen.prepare')
prepareUserDir(config)
@ -106,12 +113,18 @@ module.exports = {
'--template <template-path>': 'Path to a template spec to use for generation. (optional)',
'--info <info-path>': "Path to an info 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) {
let config = prepare({ args })
let spec = generateTemplate(config)
if(!args.q && !args.quiet) {
writeToFile(config, spec)
}
return spec
}

View File

@ -2,51 +2,70 @@ const assert = require('assert').strict
const loader = require('../src/loader')
const logging = require('../src/logging')
const expect = require('chai').expect
const fs = require('fs')
const path = require('path')
logging.init(-1)
const commands = loader.load()
describe("gen command", () => {
const test_spec = {
name: 'test-service',
description: 'Description for service.',
baseurl: 'https://nclazz.de',
namespace: 'de.nclazz' ,
quiet: true
}
it('throws an error on missing name', () => {
expect(() => commands.gen.run({ })).to.throw('Name must not be empty!')
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' })).to.throw('Description must not be empty!')
expect(() => commands.gen.run({ name: 'testename', quiet: true })).to.throw('Description must not be empty!')
})
it('generates a spec', () => {
let spec = commands.gen.run({
name: 'test-service',
description: 'Description for service.',
baseurl: 'https://nclazz.de',
namspace: 'de.nclazz'
});
let spec = commands.gen.run(test_spec);
expect(spec).to.not.equal(null)
expect(spec).to.not.equal(undefined)
expect(spec).to.not.equal({})
})
it('includes [name, description, base_url, namespace] as top level properties', () => {
let spec = commands.gen.run({
name: 'test-service',
description: 'Description for service.',
baseurl: 'https://nclazz.de',
namespace: 'de.nclazz'
});
let spec = commands.gen.run(test_spec);
expect(spec).to.have.ownProperty('name')
expect(spec).to.have.ownProperty('description')
expect(spec).to.have.ownProperty('base_url')
expect(spec).to.have.ownProperty('namespace')
})
it('correctly propagates [name, description, base_url, namespace] to spec', () => {
let spec = commands.gen.run({
name: 'test-service',
description: 'Description for service.',
baseurl: 'https://nclazz.de',
namespace: 'de.nclazz'
});
let spec = commands.gen.run(test_spec);
expect(spec.name).to.equal('test-service')
expect(spec.description).to.equal('Description for service.')
expect(spec.base_url).to.equal('https://nclazz.de')
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)
})
})