2020-09-24 13:47:20 +02:00
|
|
|
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
|
|
|
|
2020-09-24 13:47:20 +02:00
|
|
|
import com.sap.piper.Utils
|
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
import util.BasePiperTest
|
2018-01-30 14:33:28 +02:00
|
|
|
|
2020-11-25 16:39:33 +02:00
|
|
|
import static org.hamcrest.Matchers.hasItem
|
|
|
|
import static org.hamcrest.Matchers.containsInAnyOrder
|
|
|
|
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-08-31 10:22:43 +02:00
|
|
|
|
2018-02-01 14:17:17 +02:00
|
|
|
import util.Rules
|
2018-08-31 10:22:43 +02:00
|
|
|
import util.JenkinsReadYamlRule
|
2018-02-28 14:11:09 +02:00
|
|
|
import util.JenkinsStepRule
|
2018-01-30 14:33:28 +02:00
|
|
|
|
2018-08-31 10:22:43 +02:00
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
class ChecksPublishResultsTest extends BasePiperTest {
|
2018-03-02 11:53:46 +02:00
|
|
|
Map publisherStepOptions
|
|
|
|
List archiveStepPatterns
|
|
|
|
|
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)
|
2018-08-31 10:22:43 +02:00
|
|
|
.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 = []
|
2018-01-30 14:33:28 +02:00
|
|
|
// add handler for generic step call
|
2020-11-25 16:39:33 +02:00
|
|
|
helper.registerAllowedMethod("recordIssues", [Map.class], {
|
|
|
|
parameters -> publisherStepOptions[parameters.tools[0].publisher] = parameters
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod("pmdParser", [Map.class], {
|
|
|
|
parameters -> return parameters.plus([publisher: "PmdPublisher"])
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod("cpd", [Map.class], {
|
|
|
|
parameters -> return parameters.plus([publisher: "DryPublisher"])
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod("findBugs", [Map.class], {
|
|
|
|
parameters -> return parameters.plus([publisher: "FindBugsPublisher"])
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod("checkStyle", [Map.class], {
|
|
|
|
parameters -> return parameters.plus([publisher: "CheckStylePublisher"])
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod("esLint", [Map.class], {
|
|
|
|
parameters -> return parameters.plus([publisher: "ESLintPublisher"])
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod("pyLint", [Map.class], {
|
|
|
|
parameters -> return parameters.plus([publisher: "PyLintPublisher"])
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod("taskScanner", [Map.class], {
|
|
|
|
parameters -> 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)
|
|
|
|
})
|
2020-09-24 13:47:20 +02:00
|
|
|
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 {
|
2020-11-25 16:39:33 +02:00
|
|
|
// test
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.checksPublishResults(script: nullScript)
|
2020-11-25 16:39:33 +02:00
|
|
|
// 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')))
|
2018-01-30 16:26:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testPublishForJavaWithDefaultSettings() throws Exception {
|
2020-11-25 16:39:33 +02:00
|
|
|
// test
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.checksPublishResults(script: nullScript, pmd: true, cpd: true, findbugs: true, checkstyle: true)
|
2020-11-25 16:39:33 +02:00
|
|
|
// 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
|
2020-11-25 16:39:33 +02:00
|
|
|
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
|
2020-11-25 16:39:33 +02:00
|
|
|
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 {
|
2020-11-25 16:39:33 +02:00
|
|
|
// test
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.checksPublishResults(script: nullScript, eslint: true)
|
2020-11-25 16:39:33 +02:00
|
|
|
// assert
|
|
|
|
assertThat(publisherStepOptions, hasKey('ESLintPublisher'))
|
2018-01-30 16:26:54 +02:00
|
|
|
// ensure correct parser is set set
|
2020-11-25 16:39:33 +02:00
|
|
|
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
|
2020-11-25 16:39:33 +02:00
|
|
|
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 {
|
2020-11-25 16:39:33 +02:00
|
|
|
// test
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.checksPublishResults(script: nullScript, pylint: true)
|
2020-11-25 16:39:33 +02:00
|
|
|
// assert
|
|
|
|
assertThat(publisherStepOptions, hasKey('PyLintPublisher'))
|
2018-01-30 16:26:54 +02:00
|
|
|
// ensure correct parser is set set
|
2020-11-25 16:39:33 +02:00
|
|
|
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
|
2020-11-25 16:39:33 +02:00
|
|
|
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 {
|
2020-11-25 16:39:33 +02:00
|
|
|
// test
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.checksPublishResults(script: nullScript, pmd: false)
|
2020-11-25 16:39:33 +02:00
|
|
|
// 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 {
|
2020-11-25 16:39:33 +02:00
|
|
|
// test
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.checksPublishResults(script: nullScript, pmd: [:])
|
2020-11-25 16:39:33 +02:00
|
|
|
// assert
|
|
|
|
// ensure pmd is published
|
|
|
|
assertThat(publisherStepOptions, hasKey('PmdPublisher'))
|
2018-02-08 11:50:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testPublishNothingExplicitActiveFalse() throws Exception {
|
2020-11-25 16:39:33 +02:00
|
|
|
// test
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.checksPublishResults(script: nullScript, pmd: [active: false])
|
2020-11-25 16:39:33 +02:00
|
|
|
// assert
|
2018-02-08 11:50:11 +02:00
|
|
|
// ensure pmd is not published
|
2020-11-25 16:39:33 +02:00
|
|
|
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 {
|
2020-11-25 16:39:33 +02:00
|
|
|
// init
|
2018-02-05 14:48:39 +02:00
|
|
|
// pmd has been set to active: true in step configuration
|
2020-11-25 16:39:33 +02:00
|
|
|
def cpe = [configuration: [steps: [checksPublishResults: [pmd: [active: true]]]]]
|
|
|
|
// test
|
|
|
|
stepRule.step.checksPublishResults(script: [commonPipelineEnvironment: cpe])
|
|
|
|
// assert
|
|
|
|
assertThat(publisherStepOptions, hasKey('PmdPublisher'))
|
2018-02-05 14:48:39 +02:00
|
|
|
// ensure nothing else is published
|
2020-11-25 16:39:33 +02:00
|
|
|
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 {
|
2020-11-25 16:39:33 +02:00
|
|
|
// 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'])
|
2020-11-25 16:39:33 +02:00
|
|
|
// assert
|
|
|
|
assertThat(publisherStepOptions, hasKey('PmdPublisher'))
|
|
|
|
assertThat(publisherStepOptions, hasKey('ESLintPublisher'))
|
2018-01-30 16:26:54 +02:00
|
|
|
// ensure custom patterns are set
|
2020-11-25 16:39:33 +02:00
|
|
|
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
|
2020-11-25 16:39:33 +02:00
|
|
|
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 {
|
2020-11-25 16:39:33 +02:00
|
|
|
// 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)
|
2020-11-25 16:39:33 +02:00
|
|
|
// 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 {
|
2020-11-25 16:39:33 +02:00
|
|
|
// 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)
|
2020-11-25 16:39:33 +02:00
|
|
|
// 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 {
|
2020-11-25 16:39:33 +02:00
|
|
|
// test
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.checksPublishResults(script: nullScript, pmd: true)
|
2020-11-25 16:39:33 +02:00
|
|
|
// 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
|
2020-11-25 16:39:33 +02:00
|
|
|
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
|
|
|
|
2020-11-25 16:39:33 +02:00
|
|
|
@Test
|
|
|
|
void testPublishWithCustomThresholds() throws Exception {
|
|
|
|
// test
|
|
|
|
stepRule.step.checksPublishResults(script: nullScript, pmd: [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
|
|
|
}
|
|
|
|
}
|