github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/PiperPublishWarningsTest.groovy (about) 1 import com.sap.piper.JenkinsUtils 2 3 import static org.hamcrest.Matchers.allOf 4 import static org.hamcrest.Matchers.any 5 import static org.hamcrest.Matchers.containsString 6 import static org.hamcrest.Matchers.hasItem 7 import static org.hamcrest.Matchers.hasEntry 8 import static org.hamcrest.Matchers.hasKey 9 10 import org.junit.After 11 import org.junit.Before 12 import org.junit.Rule 13 import org.junit.Test 14 import org.junit.rules.RuleChain 15 import static org.junit.Assert.assertThat 16 17 import util.BasePiperTest 18 import util.Rules 19 import util.JenkinsLoggingRule 20 import util.JenkinsReadYamlRule 21 import util.JenkinsStepRule 22 import util.JenkinsShellCallRule 23 24 import com.sap.piper.Utils 25 26 import static com.lesfurets.jenkins.unit.MethodSignature.method 27 28 class PiperPublishWarningsTest extends BasePiperTest { 29 private JenkinsStepRule stepRule = new JenkinsStepRule(this) 30 private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this) 31 private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this) 32 33 def warningsParserSettings 34 def groovyScriptParserSettings 35 def warningsPluginOptions 36 37 @Rule 38 public RuleChain ruleChain = Rules 39 .getCommonRules(this) 40 .around(new JenkinsReadYamlRule(this)) 41 .around(loggingRule) 42 .around(shellRule) 43 .around(stepRule) 44 45 @Before 46 void init() throws Exception { 47 warningsParserSettings = [:] 48 groovyScriptParserSettings = [:] 49 warningsPluginOptions = [:] 50 51 // add handler for generic step call 52 helper.registerAllowedMethod("writeFile", [Map.class], null) 53 helper.registerAllowedMethod("recordIssues", [Map.class], { 54 parameters -> warningsPluginOptions = parameters 55 }) 56 helper.registerAllowedMethod("groovyScript", [Map.class], { 57 parameters -> groovyScriptParserSettings = parameters 58 }) 59 JenkinsUtils.metaClass.addWarningsNGParser = { String s1, String s2, String s3, String s4 -> 60 warningsParserSettings = [id: s1, name: s2, regex: s3, script: s4] 61 return true 62 } 63 JenkinsUtils.metaClass.static.getFullBuildLog = { def currentBuild -> return ""} 64 JenkinsUtils.metaClass.static.isPluginActive = { id -> return true} 65 66 Utils.metaClass.echo = { def m -> } 67 } 68 69 @After 70 public void tearDown() { 71 JenkinsUtils.metaClass.addWarningsNGParser = null 72 JenkinsUtils.metaClass.static.getFullBuildLog = null 73 JenkinsUtils.metaClass.static.isPluginActive = null 74 Utils.metaClass = null 75 } 76 77 @Test 78 void testPublishWarnings() throws Exception { 79 stepRule.step.piperPublishWarnings(script: nullScript) 80 // asserts 81 assertThat(loggingRule.log, containsString('[piperPublishWarnings] Added warnings-ng plugin parser \'Piper\' configuration.')) 82 assertThat(warningsParserSettings, hasEntry('id', 'piper')) 83 assertThat(warningsParserSettings, hasEntry('name', 'Piper')) 84 assertThat(warningsParserSettings, hasEntry('regex', '\\[(INFO|WARNING|ERROR)\\] (.*) \\(([^) ]*)\\/([^) ]*)\\)')) 85 assertThat(warningsParserSettings, hasKey('script')) 86 assertThat(warningsPluginOptions, allOf( 87 hasEntry('enabledForFailure', true), 88 hasEntry('blameDisabled', true) 89 )) 90 assertThat(warningsPluginOptions, hasKey('tools')) 91 92 assertThat(groovyScriptParserSettings, hasEntry('parserId', 'piper')) 93 assertThat(groovyScriptParserSettings, hasEntry('pattern', 'build.log')) 94 } 95 }