1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/test/groovy/ChecksPublishResultsTest.groovy

351 lines
15 KiB
Groovy
Raw Normal View History

import org.junit.After
2018-01-30 14:33:28 +02:00
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
2018-02-05 17:02:59 +02:00
import org.junit.Ignore
2018-01-30 14:33:28 +02:00
import com.sap.piper.Utils
import util.BasePiperTest
2018-01-30 14:33:28 +02:00
import static org.hamcrest.Matchers.hasItem
import static org.hamcrest.Matchers.containsInAnyOrder
import static org.hamcrest.Matchers.empty
import static org.hamcrest.Matchers.not
import static org.hamcrest.Matchers.allOf
import static org.hamcrest.Matchers.is
import static org.hamcrest.Matchers.hasKey
import static org.hamcrest.Matchers.hasSize
import static org.hamcrest.Matchers.hasEntry
import static org.junit.Assert.assertThat
2018-02-01 14:17:17 +02:00
import util.Rules
import util.JenkinsReadYamlRule
2018-02-28 14:11:09 +02:00
import util.JenkinsStepRule
2018-01-30 14:33:28 +02:00
class ChecksPublishResultsTest extends BasePiperTest {
2018-03-02 11:53:46 +02:00
Map publisherStepOptions
List archiveStepPatterns
List invokedReportingTools
2018-03-02 11:53:46 +02:00
2019-01-22 10:25:42 +02:00
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
2018-01-30 14:33:28 +02:00
@Rule
2018-02-28 14:11:09 +02:00
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
2019-01-22 10:25:42 +02:00
.around(stepRule)
2018-01-30 14:33:28 +02:00
@Before
void init() {
publisherStepOptions = [:]
2018-01-30 16:26:54 +02:00
archiveStepPatterns = []
invokedReportingTools = []
2018-01-30 14:33:28 +02:00
// add handler for generic step call
helper.registerAllowedMethod("recordIssues", [Map.class], {
parameters ->
if(parameters.tools[0] in Map && parameters.tools[0].containsKey('publisher')) {
publisherStepOptions[parameters.tools[0].publisher] = parameters;
}
})
helper.registerAllowedMethod("pmdParser", [Map.class], {
parameters ->
invokedReportingTools << "pmdParser";
return parameters.plus([publisher: "PmdPublisher"])
})
helper.registerAllowedMethod("cpd", [Map.class], {
parameters ->
invokedReportingTools << "cpd";
return parameters.plus([publisher: "DryPublisher"])
})
helper.registerAllowedMethod("findBugs", [Map.class], {
parameters ->
invokedReportingTools << "findBugs";
return parameters.plus([publisher: "FindBugsPublisher"])
})
helper.registerAllowedMethod("checkStyle", [Map.class], {
parameters ->
invokedReportingTools << "checkStyle";
return parameters.plus([publisher: "CheckStylePublisher"])
})
helper.registerAllowedMethod("esLint", [Map.class], {
parameters ->
invokedReportingTools << "esLint";
return parameters.plus([publisher: "ESLintPublisher"])
})
helper.registerAllowedMethod("pyLint", [Map.class], {
parameters ->
invokedReportingTools << "pyLint";
return parameters.plus([publisher: "PyLintPublisher"])
})
helper.registerAllowedMethod("taskScanner", [Map.class], {
parameters ->
invokedReportingTools << "taskScanner";
return parameters.plus([publisher: "TaskPublisher"])
2018-01-30 14:33:28 +02:00
})
2018-01-30 16:26:54 +02:00
helper.registerAllowedMethod("archiveArtifacts", [Map.class], {
parameters -> archiveStepPatterns.push(parameters.artifacts)
})
Utils.metaClass.echo = { def m -> }
}
@After
public void tearDown() {
Utils.metaClass = null
2018-01-30 14:33:28 +02:00
}
@Test
void testPublishWithDefaultSettings() throws Exception {
// test
2019-01-22 10:25:42 +02:00
stepRule.step.checksPublishResults(script: nullScript)
// assert
// ensure nothing is published
assertThat(publisherStepOptions, not(hasKey('PmdPublisher')))
assertThat(publisherStepOptions, not(hasKey('DryPublisher')))
assertThat(publisherStepOptions, not(hasKey('FindBugsPublisher')))
assertThat(publisherStepOptions, not(hasKey('CheckStylePublisher')))
assertThat(publisherStepOptions, not(hasKey('ESLintPublisher')))
assertThat(publisherStepOptions, not(hasKey('PyLintPublisher')))
assertThat(publisherStepOptions, not(hasKey('TaskPublisher')))
assertThat(invokedReportingTools, is(empty()))
2018-01-30 16:26:54 +02:00
}
@Test
void testPublishForJavaWithDefaultSettings() throws Exception {
// test
2019-01-22 10:25:42 +02:00
stepRule.step.checksPublishResults(script: nullScript, pmd: true, cpd: true, findbugs: true, checkstyle: true)
// assert
assertThat(publisherStepOptions, hasKey('PmdPublisher'))
assertThat(publisherStepOptions, hasKey('DryPublisher'))
assertThat(publisherStepOptions, hasKey('FindBugsPublisher'))
assertThat(publisherStepOptions, hasKey('CheckStylePublisher'))
2018-01-30 16:26:54 +02:00
// ensure default patterns are set
assertThat(publisherStepOptions['PmdPublisher'], hasKey('tools'))
assertThat(publisherStepOptions['PmdPublisher']['tools'], hasItem(hasEntry('pattern', '**/target/pmd.xml')))
assertThat(publisherStepOptions['DryPublisher'], hasKey('tools'))
assertThat(publisherStepOptions['DryPublisher']['tools'], hasItem(hasEntry('pattern', '**/target/cpd.xml')))
assertThat(publisherStepOptions['FindBugsPublisher'], hasKey('tools'))
assertThat(publisherStepOptions['FindBugsPublisher']['tools'], hasItem(hasEntry('pattern', '**/target/findbugsXml.xml, **/target/findbugs.xml')))
assertThat(publisherStepOptions['CheckStylePublisher'], hasKey('tools'))
assertThat(publisherStepOptions['CheckStylePublisher']['tools'], hasItem(hasEntry('pattern', '**/target/checkstyle-result.xml')))
2018-01-30 16:26:54 +02:00
// ensure nothing else is published
assertThat(publisherStepOptions, not(hasKey('ESLintPublisher')))
assertThat(publisherStepOptions, not(hasKey('PyLintPublisher')))
assertThat(publisherStepOptions, not(hasKey('TaskPublisher')))
2018-01-30 16:26:54 +02:00
}
@Test
void testPublishForJavaScriptWithDefaultSettings() throws Exception {
// test
2019-01-22 10:25:42 +02:00
stepRule.step.checksPublishResults(script: nullScript, eslint: true)
// assert
assertThat(publisherStepOptions, hasKey('ESLintPublisher'))
2018-01-30 16:26:54 +02:00
// ensure correct parser is set set
assertThat(publisherStepOptions['ESLintPublisher'], hasKey('tools'))
assertThat(publisherStepOptions['ESLintPublisher']['tools'], hasItem(hasEntry('pattern', '**/eslint.xml')))
2018-01-30 16:26:54 +02:00
// ensure nothing else is published
assertThat(publisherStepOptions, not(hasKey('PmdPublisher')))
assertThat(publisherStepOptions, not(hasKey('DryPublisher')))
assertThat(publisherStepOptions, not(hasKey('FindBugsPublisher')))
assertThat(publisherStepOptions, not(hasKey('CheckStylePublisher')))
assertThat(publisherStepOptions, not(hasKey('PyLintPublisher')))
assertThat(publisherStepOptions, not(hasKey('TaskPublisher')))
2018-01-30 16:26:54 +02:00
}
@Test
void testPublishForPythonWithDefaultSettings() throws Exception {
// test
2019-01-22 10:25:42 +02:00
stepRule.step.checksPublishResults(script: nullScript, pylint: true)
// assert
assertThat(publisherStepOptions, hasKey('PyLintPublisher'))
2018-01-30 16:26:54 +02:00
// ensure correct parser is set set
assertThat(publisherStepOptions['PyLintPublisher'], hasKey('tools'))
assertThat(publisherStepOptions['PyLintPublisher']['tools'], hasItem(hasEntry('pattern', '**/pylint.log')))
2018-01-30 16:26:54 +02:00
// ensure nothing else is published
assertThat(publisherStepOptions, not(hasKey('PmdPublisher')))
assertThat(publisherStepOptions, not(hasKey('DryPublisher')))
assertThat(publisherStepOptions, not(hasKey('FindBugsPublisher')))
assertThat(publisherStepOptions, not(hasKey('CheckStylePublisher')))
assertThat(publisherStepOptions, not(hasKey('ESLintPublisher')))
assertThat(publisherStepOptions, not(hasKey('TaskPublisher')))
2018-01-30 16:26:54 +02:00
}
2018-02-05 14:48:39 +02:00
@Test
void testPublishNothingExplicitFalse() throws Exception {
// test
2019-01-22 10:25:42 +02:00
stepRule.step.checksPublishResults(script: nullScript, pmd: false)
// assert
// ensure pmd is not published
assertThat(publisherStepOptions, not(hasKey('PmdPublisher')))
2018-02-05 14:48:39 +02:00
}
2018-02-08 11:50:11 +02:00
@Test
void testPublishNothingImplicitTrue() throws Exception {
// test
2019-01-22 10:25:42 +02:00
stepRule.step.checksPublishResults(script: nullScript, pmd: [:])
// assert
// ensure pmd is published
assertThat(publisherStepOptions, hasKey('PmdPublisher'))
2018-02-08 11:50:11 +02:00
}
@Test
void testPublishNothingExplicitActiveFalse() throws Exception {
// test
2019-01-22 10:25:42 +02:00
stepRule.step.checksPublishResults(script: nullScript, pmd: [active: false])
// assert
2018-02-08 11:50:11 +02:00
// ensure pmd is not published
assertThat(publisherStepOptions, not(hasKey('PmdPublisher')))
2018-02-08 11:50:11 +02:00
}
2018-02-05 14:48:39 +02:00
@Test
void testPublishWithChangedStepDefaultSettings() throws Exception {
// init
2018-02-05 14:48:39 +02:00
// pmd has been set to active: true in step configuration
nullScript.commonPipelineEnvironment.configuration =
[
steps: [
checksPublishResults: [
pmd: [active: true]
]
]
]
// test
stepRule.step.checksPublishResults(script: nullScript)
// assert
assertThat(publisherStepOptions, hasKey('PmdPublisher'))
2018-02-05 14:48:39 +02:00
// ensure nothing else is published
assertThat(publisherStepOptions, not(hasKey('DryPublisher')))
assertThat(publisherStepOptions, not(hasKey('FindBugsPublisher')))
assertThat(publisherStepOptions, not(hasKey('CheckStylePublisher')))
assertThat(publisherStepOptions, not(hasKey('ESLintPublisher')))
assertThat(publisherStepOptions, not(hasKey('PyLintPublisher')))
assertThat(publisherStepOptions, not(hasKey('TaskPublisher')))
2018-02-05 14:48:39 +02:00
}
2018-01-30 16:26:54 +02:00
@Test
void testPublishWithCustomPattern() throws Exception {
// test
2019-01-22 10:25:42 +02:00
stepRule.step.checksPublishResults(script: nullScript, eslint: [pattern: 'my-fancy-file.ext'], pmd: [pattern: 'this-is-not-a-patter.xml'])
// assert
assertThat(publisherStepOptions, hasKey('PmdPublisher'))
assertThat(publisherStepOptions, hasKey('ESLintPublisher'))
2018-01-30 16:26:54 +02:00
// ensure custom patterns are set
assertThat(publisherStepOptions['PmdPublisher'], hasKey('tools'))
assertThat(publisherStepOptions['PmdPublisher']['tools'], hasItem(hasEntry('pattern', 'this-is-not-a-patter.xml')))
assertThat(publisherStepOptions['ESLintPublisher'], hasKey('tools'))
assertThat(publisherStepOptions['ESLintPublisher']['tools'], hasItem(hasEntry('pattern', 'my-fancy-file.ext')))
2018-01-30 16:26:54 +02:00
// ensure nothing else is published
assertThat(publisherStepOptions, not(hasKey('DryPublisher')))
assertThat(publisherStepOptions, not(hasKey('FindBugsPublisher')))
assertThat(publisherStepOptions, not(hasKey('CheckStylePublisher')))
assertThat(publisherStepOptions, not(hasKey('PyLintPublisher')))
assertThat(publisherStepOptions, not(hasKey('TaskPublisher')))
2018-01-30 16:26:54 +02:00
}
@Test
void testPublishWithArchive() throws Exception {
// test
2019-01-22 10:25:42 +02:00
stepRule.step.checksPublishResults(script: nullScript, archive: true, eslint: true, pmd: true, cpd: true, findbugs: true, checkstyle: true)
// assert
assertThat(archiveStepPatterns, hasSize(5))
assertThat(archiveStepPatterns, allOf(
hasItem('**/target/pmd.xml'),
hasItem('**/target/cpd.xml'),
hasItem('**/target/findbugsXml.xml, **/target/findbugs.xml'),
hasItem('**/target/checkstyle-result.xml'),
hasItem('**/eslint.xml'),
))
2018-01-30 16:26:54 +02:00
}
@Test
void testPublishWithPartialArchive() throws Exception {
// test
2019-01-22 10:25:42 +02:00
stepRule.step.checksPublishResults(script: nullScript, archive: true, eslint: [archive: false], pmd: true, cpd: true, findbugs: true, checkstyle: true)
// assert
assertThat(archiveStepPatterns, hasSize(4))
assertThat(archiveStepPatterns, allOf(
hasItem('**/target/pmd.xml'),
hasItem('**/target/cpd.xml'),
hasItem('**/target/findbugsXml.xml, **/target/findbugs.xml'),
hasItem('**/target/checkstyle-result.xml'),
not(hasItem('**/eslint.xml')),
))
2018-01-30 16:26:54 +02:00
}
2018-02-08 11:43:06 +02:00
@Test
void testPublishWithDefaultThresholds() throws Exception {
// test
2019-01-22 10:25:42 +02:00
stepRule.step.checksPublishResults(script: nullScript, pmd: true)
// assert
assertThat(publisherStepOptions, hasKey('PmdPublisher'))
assertThat(publisherStepOptions['PmdPublisher'], hasKey('qualityGates'))
assertThat(publisherStepOptions['PmdPublisher']['qualityGates'], allOf(
hasSize(2),
hasItem(allOf(
hasEntry('threshold', 1),
hasEntry('type', 'TOTAL_ERROR'),
hasEntry('unstable', false),
)),
hasItem(allOf(
hasEntry('threshold', 1),
hasEntry('type', 'TOTAL_HIGH'),
hasEntry('unstable', false),
)),
))
2018-02-08 11:43:06 +02:00
}
2018-01-30 16:26:54 +02:00
@Test
void testPublishWithLegacyThresholds() throws Exception {
// test
stepRule.step.checksPublishResults(script: nullScript, pmd: [thresholds: [fail: [high: '10']]])
// assert
assertThat(publisherStepOptions, hasKey('PmdPublisher'))
assertThat(publisherStepOptions['PmdPublisher'], hasKey('qualityGates'))
assertThat(publisherStepOptions['PmdPublisher']['qualityGates'], allOf(
//TODO: thresholds are added to existing qualityGates, thus we have 2 defined in the end
hasSize(3),
hasItem(allOf(
hasEntry('threshold', 1),
hasEntry('type', 'TOTAL_ERROR'),
hasEntry('unstable', false),
)),
hasItem(allOf(
hasEntry('threshold', 1),
hasEntry('type', 'TOTAL_HIGH'),
hasEntry('unstable', false),
)),
hasItem(allOf(
hasEntry('threshold', 11),
hasEntry('type', 'TOTAL_HIGH'),
hasEntry('unstable', false),
)),
))
}
2018-01-30 16:26:54 +02:00
@Test
void testPublishWithCustomThresholds() throws Exception {
// test
stepRule.step.checksPublishResults(script: nullScript, pmd: [active: true, qualityGates: [[threshold: 20, type: 'TOTAL_LOW', unstable: false],[threshold: 10, type: 'TOTAL_NORMAL', unstable: false]]])
// assert
assertThat(publisherStepOptions, hasKey('PmdPublisher'))
assertThat(publisherStepOptions['PmdPublisher'], hasKey('qualityGates'))
assertThat(publisherStepOptions['PmdPublisher']['qualityGates'], allOf(
hasSize(2),
hasItem(allOf(
hasEntry('threshold', 10),
hasEntry('type', 'TOTAL_NORMAL'),
hasEntry('unstable', false),
)),
hasItem(allOf(
hasEntry('threshold', 20),
hasEntry('type', 'TOTAL_LOW'),
hasEntry('unstable', false),
)),
))
2018-01-30 14:33:28 +02:00
}
}