added linting to generator

feature/linter
Niclas Thobaben 2021-07-07 21:40:18 +02:00
parent 66dd66959f
commit a6ee8d4593
4 changed files with 47 additions and 4 deletions

View File

@ -123,7 +123,9 @@ module.exports = {
let spec = generateTemplate(config)
if(!args['skip-linting']) {
linter.lint(spec)
if(!linter.lint(spec).success) {
throw 'Lintin Error!'
}
}
if(!args.q && !args.quiet) {

View File

@ -41,14 +41,22 @@ const linterMappings = [
module.exports.lint = function(spec) {
let messages = [];
let results = {
success: true
}
linterMappings.forEach(element => {
let value = objectPath.get(spec, element.path);
element.linters.forEach(linter => {
linter(element.path, value, messages);
let msg = linter(element.path, value)
if(msg) {
results.success = false
}
msg = msg || 'OK'
results[element.path] = msg;
console.log(element.path, msg)
});
});
return messages;
return results;
}
module.exports.regex = {

View File

@ -67,5 +67,18 @@ describe("gen command", () => {
expect(spec_json).to.have.ownProperty('namespace')
fs.unlinkSync(spec_path)
})
it('fails on invalid linting properties', () => {
expect(() => commands.gen.run({
...test_spec,
name: 'Test'
})).to.throw()
})
it('does not fail on invalid linting properties when lintin disabled', () => {
expect(() => commands.gen.run({
...test_spec,
name: 'Test',
'skip-linting': true
})).to.not.throw()
})
})

View File

@ -30,4 +30,24 @@ describe('linter validators', () => {
expect(linter.validators.regex(/[0-9]+/)('path', ' ')).to.not.be.empty
expect(linter.validators.regex(/.+/)('path', ' ')).to.not.be.empty
})
})
describe('linter for spec', () => {
it('fails on invalid name property', () => {
let result = linter.lint({
name: "Test",
description: "Test."
})
expect(result.success).to.equal(false)
expect(result.name).to.not.equal('OK')
})
it('fails on invalid description property', () => {
let result = linter.lint({
name: "test-service",
description: "test."
})
expect(result.success).to.equal(false)
expect(result.description).to.not.equal('OK')
})
})