github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/ChecksPublishResultsTest.groovy (about) 1 import org.junit.After 2 import org.junit.Before 3 import org.junit.Rule 4 import org.junit.Test 5 import org.junit.rules.RuleChain 6 import org.junit.Ignore 7 8 import com.sap.piper.Utils 9 10 import util.BasePiperTest 11 12 import static org.hamcrest.Matchers.hasItem 13 import static org.hamcrest.Matchers.containsInAnyOrder 14 import static org.hamcrest.Matchers.empty 15 import static org.hamcrest.Matchers.not 16 import static org.hamcrest.Matchers.allOf 17 import static org.hamcrest.Matchers.is 18 import static org.hamcrest.Matchers.hasKey 19 import static org.hamcrest.Matchers.hasSize 20 import static org.hamcrest.Matchers.hasEntry 21 import static org.junit.Assert.assertThat 22 23 import util.Rules 24 import util.JenkinsReadYamlRule 25 import util.JenkinsStepRule 26 27 28 class ChecksPublishResultsTest extends BasePiperTest { 29 Map publisherStepOptions 30 List archiveStepPatterns 31 List invokedReportingTools 32 33 private JenkinsStepRule stepRule = new JenkinsStepRule(this) 34 35 @Rule 36 public RuleChain ruleChain = Rules 37 .getCommonRules(this) 38 .around(new JenkinsReadYamlRule(this)) 39 .around(stepRule) 40 41 @Before 42 void init() { 43 publisherStepOptions = [:] 44 archiveStepPatterns = [] 45 invokedReportingTools = [] 46 47 // add handler for generic step call 48 helper.registerAllowedMethod("recordIssues", [Map.class], { 49 parameters -> 50 if(parameters.tools[0] in Map && parameters.tools[0].containsKey('publisher')) { 51 publisherStepOptions[parameters.tools[0].publisher] = parameters; 52 } 53 }) 54 helper.registerAllowedMethod("pmdParser", [Map.class], { 55 parameters -> 56 invokedReportingTools << "pmdParser"; 57 return parameters.plus([publisher: "PmdPublisher"]) 58 }) 59 helper.registerAllowedMethod("cpd", [Map.class], { 60 parameters -> 61 invokedReportingTools << "cpd"; 62 return parameters.plus([publisher: "DryPublisher"]) 63 }) 64 helper.registerAllowedMethod("findBugs", [Map.class], { 65 parameters -> 66 invokedReportingTools << "findBugs"; 67 return parameters.plus([publisher: "FindBugsPublisher"]) 68 }) 69 helper.registerAllowedMethod("checkStyle", [Map.class], { 70 parameters -> 71 invokedReportingTools << "checkStyle"; 72 return parameters.plus([publisher: "CheckStylePublisher"]) 73 }) 74 helper.registerAllowedMethod("esLint", [Map.class], { 75 parameters -> 76 invokedReportingTools << "esLint"; 77 return parameters.plus([publisher: "ESLintPublisher"]) 78 }) 79 helper.registerAllowedMethod("pyLint", [Map.class], { 80 parameters -> 81 invokedReportingTools << "pyLint"; 82 return parameters.plus([publisher: "PyLintPublisher"]) 83 }) 84 helper.registerAllowedMethod("taskScanner", [Map.class], { 85 parameters -> 86 invokedReportingTools << "taskScanner"; 87 return parameters.plus([publisher: "TaskPublisher"]) 88 }) 89 helper.registerAllowedMethod("archiveArtifacts", [Map.class], { 90 parameters -> archiveStepPatterns.push(parameters.artifacts) 91 }) 92 Utils.metaClass.echo = { def m -> } 93 } 94 95 @After 96 public void tearDown() { 97 Utils.metaClass = null 98 } 99 100 @Test 101 void testPublishWithDefaultSettings() throws Exception { 102 // test 103 stepRule.step.checksPublishResults(script: nullScript) 104 // assert 105 // ensure nothing is published 106 assertThat(publisherStepOptions, not(hasKey('PmdPublisher'))) 107 assertThat(publisherStepOptions, not(hasKey('DryPublisher'))) 108 assertThat(publisherStepOptions, not(hasKey('FindBugsPublisher'))) 109 assertThat(publisherStepOptions, not(hasKey('CheckStylePublisher'))) 110 assertThat(publisherStepOptions, not(hasKey('ESLintPublisher'))) 111 assertThat(publisherStepOptions, not(hasKey('PyLintPublisher'))) 112 assertThat(publisherStepOptions, not(hasKey('TaskPublisher'))) 113 114 assertThat(invokedReportingTools, is(empty())) 115 } 116 117 @Test 118 void testPublishForJavaWithDefaultSettings() throws Exception { 119 // test 120 stepRule.step.checksPublishResults(script: nullScript, pmd: true, cpd: true, findbugs: true, checkstyle: true) 121 // assert 122 assertThat(publisherStepOptions, hasKey('PmdPublisher')) 123 assertThat(publisherStepOptions, hasKey('DryPublisher')) 124 assertThat(publisherStepOptions, hasKey('FindBugsPublisher')) 125 assertThat(publisherStepOptions, hasKey('CheckStylePublisher')) 126 // ensure default patterns are set 127 assertThat(publisherStepOptions['PmdPublisher'], hasKey('tools')) 128 assertThat(publisherStepOptions['PmdPublisher']['tools'], hasItem(hasEntry('pattern', '**/target/pmd.xml'))) 129 assertThat(publisherStepOptions['DryPublisher'], hasKey('tools')) 130 assertThat(publisherStepOptions['DryPublisher']['tools'], hasItem(hasEntry('pattern', '**/target/cpd.xml'))) 131 assertThat(publisherStepOptions['FindBugsPublisher'], hasKey('tools')) 132 assertThat(publisherStepOptions['FindBugsPublisher']['tools'], hasItem(hasEntry('pattern', '**/target/findbugsXml.xml, **/target/findbugs.xml'))) 133 assertThat(publisherStepOptions['CheckStylePublisher'], hasKey('tools')) 134 assertThat(publisherStepOptions['CheckStylePublisher']['tools'], hasItem(hasEntry('pattern', '**/target/checkstyle-result.xml'))) 135 // ensure nothing else is published 136 assertThat(publisherStepOptions, not(hasKey('ESLintPublisher'))) 137 assertThat(publisherStepOptions, not(hasKey('PyLintPublisher'))) 138 assertThat(publisherStepOptions, not(hasKey('TaskPublisher'))) 139 } 140 141 @Test 142 void testPublishForJavaScriptWithDefaultSettings() throws Exception { 143 // test 144 stepRule.step.checksPublishResults(script: nullScript, eslint: true) 145 // assert 146 assertThat(publisherStepOptions, hasKey('ESLintPublisher')) 147 // ensure correct parser is set set 148 assertThat(publisherStepOptions['ESLintPublisher'], hasKey('tools')) 149 assertThat(publisherStepOptions['ESLintPublisher']['tools'], hasItem(hasEntry('pattern', '**/eslint.xml'))) 150 // ensure nothing else is published 151 assertThat(publisherStepOptions, not(hasKey('PmdPublisher'))) 152 assertThat(publisherStepOptions, not(hasKey('DryPublisher'))) 153 assertThat(publisherStepOptions, not(hasKey('FindBugsPublisher'))) 154 assertThat(publisherStepOptions, not(hasKey('CheckStylePublisher'))) 155 assertThat(publisherStepOptions, not(hasKey('PyLintPublisher'))) 156 assertThat(publisherStepOptions, not(hasKey('TaskPublisher'))) 157 } 158 159 @Test 160 void testPublishForPythonWithDefaultSettings() throws Exception { 161 // test 162 stepRule.step.checksPublishResults(script: nullScript, pylint: true) 163 // assert 164 assertThat(publisherStepOptions, hasKey('PyLintPublisher')) 165 // ensure correct parser is set set 166 assertThat(publisherStepOptions['PyLintPublisher'], hasKey('tools')) 167 assertThat(publisherStepOptions['PyLintPublisher']['tools'], hasItem(hasEntry('pattern', '**/pylint.log'))) 168 // ensure nothing else is published 169 assertThat(publisherStepOptions, not(hasKey('PmdPublisher'))) 170 assertThat(publisherStepOptions, not(hasKey('DryPublisher'))) 171 assertThat(publisherStepOptions, not(hasKey('FindBugsPublisher'))) 172 assertThat(publisherStepOptions, not(hasKey('CheckStylePublisher'))) 173 assertThat(publisherStepOptions, not(hasKey('ESLintPublisher'))) 174 assertThat(publisherStepOptions, not(hasKey('TaskPublisher'))) 175 } 176 177 @Test 178 void testPublishNothingExplicitFalse() throws Exception { 179 // test 180 stepRule.step.checksPublishResults(script: nullScript, pmd: false) 181 // assert 182 // ensure pmd is not published 183 assertThat(publisherStepOptions, not(hasKey('PmdPublisher'))) 184 } 185 186 @Test 187 void testPublishNothingImplicitTrue() throws Exception { 188 // test 189 stepRule.step.checksPublishResults(script: nullScript, pmd: [:]) 190 // assert 191 // ensure pmd is published 192 assertThat(publisherStepOptions, hasKey('PmdPublisher')) 193 } 194 195 @Test 196 void testPublishNothingExplicitActiveFalse() throws Exception { 197 // test 198 stepRule.step.checksPublishResults(script: nullScript, pmd: [active: false]) 199 // assert 200 // ensure pmd is not published 201 assertThat(publisherStepOptions, not(hasKey('PmdPublisher'))) 202 } 203 204 @Test 205 void testPublishWithChangedStepDefaultSettings() throws Exception { 206 // init 207 // pmd has been set to active: true in step configuration 208 nullScript.commonPipelineEnvironment.configuration = 209 [ 210 steps: [ 211 checksPublishResults: [ 212 pmd: [active: true] 213 ] 214 ] 215 ] 216 // test 217 stepRule.step.checksPublishResults(script: nullScript) 218 // assert 219 assertThat(publisherStepOptions, hasKey('PmdPublisher')) 220 // ensure nothing else is published 221 assertThat(publisherStepOptions, not(hasKey('DryPublisher'))) 222 assertThat(publisherStepOptions, not(hasKey('FindBugsPublisher'))) 223 assertThat(publisherStepOptions, not(hasKey('CheckStylePublisher'))) 224 assertThat(publisherStepOptions, not(hasKey('ESLintPublisher'))) 225 assertThat(publisherStepOptions, not(hasKey('PyLintPublisher'))) 226 assertThat(publisherStepOptions, not(hasKey('TaskPublisher'))) 227 } 228 229 @Test 230 void testPublishWithCustomPattern() throws Exception { 231 // test 232 stepRule.step.checksPublishResults(script: nullScript, eslint: [pattern: 'my-fancy-file.ext'], pmd: [pattern: 'this-is-not-a-patter.xml']) 233 // assert 234 assertThat(publisherStepOptions, hasKey('PmdPublisher')) 235 assertThat(publisherStepOptions, hasKey('ESLintPublisher')) 236 // ensure custom patterns are set 237 assertThat(publisherStepOptions['PmdPublisher'], hasKey('tools')) 238 assertThat(publisherStepOptions['PmdPublisher']['tools'], hasItem(hasEntry('pattern', 'this-is-not-a-patter.xml'))) 239 assertThat(publisherStepOptions['ESLintPublisher'], hasKey('tools')) 240 assertThat(publisherStepOptions['ESLintPublisher']['tools'], hasItem(hasEntry('pattern', 'my-fancy-file.ext'))) 241 // ensure nothing else is published 242 assertThat(publisherStepOptions, not(hasKey('DryPublisher'))) 243 assertThat(publisherStepOptions, not(hasKey('FindBugsPublisher'))) 244 assertThat(publisherStepOptions, not(hasKey('CheckStylePublisher'))) 245 assertThat(publisherStepOptions, not(hasKey('PyLintPublisher'))) 246 assertThat(publisherStepOptions, not(hasKey('TaskPublisher'))) 247 } 248 249 @Test 250 void testPublishWithArchive() throws Exception { 251 // test 252 stepRule.step.checksPublishResults(script: nullScript, archive: true, eslint: true, pmd: true, cpd: true, findbugs: true, checkstyle: true) 253 // assert 254 assertThat(archiveStepPatterns, hasSize(5)) 255 assertThat(archiveStepPatterns, allOf( 256 hasItem('**/target/pmd.xml'), 257 hasItem('**/target/cpd.xml'), 258 hasItem('**/target/findbugsXml.xml, **/target/findbugs.xml'), 259 hasItem('**/target/checkstyle-result.xml'), 260 hasItem('**/eslint.xml'), 261 )) 262 } 263 264 @Test 265 void testPublishWithPartialArchive() throws Exception { 266 // test 267 stepRule.step.checksPublishResults(script: nullScript, archive: true, eslint: [archive: false], pmd: true, cpd: true, findbugs: true, checkstyle: true) 268 // assert 269 assertThat(archiveStepPatterns, hasSize(4)) 270 assertThat(archiveStepPatterns, allOf( 271 hasItem('**/target/pmd.xml'), 272 hasItem('**/target/cpd.xml'), 273 hasItem('**/target/findbugsXml.xml, **/target/findbugs.xml'), 274 hasItem('**/target/checkstyle-result.xml'), 275 not(hasItem('**/eslint.xml')), 276 )) 277 } 278 279 @Test 280 void testPublishWithDefaultThresholds() throws Exception { 281 // test 282 stepRule.step.checksPublishResults(script: nullScript, pmd: true) 283 // assert 284 assertThat(publisherStepOptions, hasKey('PmdPublisher')) 285 assertThat(publisherStepOptions['PmdPublisher'], hasKey('qualityGates')) 286 assertThat(publisherStepOptions['PmdPublisher']['qualityGates'], allOf( 287 hasSize(2), 288 hasItem(allOf( 289 hasEntry('threshold', 1), 290 hasEntry('type', 'TOTAL_ERROR'), 291 hasEntry('unstable', false), 292 )), 293 hasItem(allOf( 294 hasEntry('threshold', 1), 295 hasEntry('type', 'TOTAL_HIGH'), 296 hasEntry('unstable', false), 297 )), 298 )) 299 } 300 301 @Test 302 void testPublishWithLegacyThresholds() throws Exception { 303 // test 304 stepRule.step.checksPublishResults(script: nullScript, pmd: [thresholds: [fail: [high: '10']]]) 305 // assert 306 assertThat(publisherStepOptions, hasKey('PmdPublisher')) 307 assertThat(publisherStepOptions['PmdPublisher'], hasKey('qualityGates')) 308 assertThat(publisherStepOptions['PmdPublisher']['qualityGates'], allOf( 309 //TODO: thresholds are added to existing qualityGates, thus we have 2 defined in the end 310 hasSize(3), 311 hasItem(allOf( 312 hasEntry('threshold', 1), 313 hasEntry('type', 'TOTAL_ERROR'), 314 hasEntry('unstable', false), 315 )), 316 hasItem(allOf( 317 hasEntry('threshold', 1), 318 hasEntry('type', 'TOTAL_HIGH'), 319 hasEntry('unstable', false), 320 )), 321 hasItem(allOf( 322 hasEntry('threshold', 11), 323 hasEntry('type', 'TOTAL_HIGH'), 324 hasEntry('unstable', false), 325 )), 326 )) 327 } 328 329 @Test 330 void testPublishWithCustomThresholds() throws Exception { 331 // test 332 stepRule.step.checksPublishResults(script: nullScript, pmd: [active: true, qualityGates: [[threshold: 20, type: 'TOTAL_LOW', unstable: false],[threshold: 10, type: 'TOTAL_NORMAL', unstable: false]]]) 333 // assert 334 assertThat(publisherStepOptions, hasKey('PmdPublisher')) 335 assertThat(publisherStepOptions['PmdPublisher'], hasKey('qualityGates')) 336 assertThat(publisherStepOptions['PmdPublisher']['qualityGates'], allOf( 337 hasSize(2), 338 hasItem(allOf( 339 hasEntry('threshold', 10), 340 hasEntry('type', 'TOTAL_NORMAL'), 341 hasEntry('unstable', false), 342 )), 343 hasItem(allOf( 344 hasEntry('threshold', 20), 345 hasEntry('type', 'TOTAL_LOW'), 346 hasEntry('unstable', false), 347 )), 348 )) 349 } 350 }