github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/templates/PiperPipelineStageComplianceTest.groovy (about) 1 package templates 2 3 import org.junit.Before 4 import org.junit.Rule 5 import org.junit.Test 6 import org.junit.rules.RuleChain 7 import util.* 8 9 import static org.hamcrest.Matchers.* 10 import static org.junit.Assert.assertNotNull 11 import static org.junit.Assert.assertThat 12 13 class PiperPipelineStageComplianceTest extends BasePiperTest { 14 private JenkinsStepRule jsr = new JenkinsStepRule(this) 15 private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this) 16 17 @Rule 18 public RuleChain rules = Rules 19 .getCommonRules(this) 20 .around(new JenkinsReadYamlRule(this)) 21 .around(jlr) 22 .around(jsr) 23 24 private List stepsCalled = [] 25 private Map stepParameters = [:] 26 27 @Before 28 void init() { 29 binding.variables.env.STAGE_NAME = 'Compliance' 30 helper.registerAllowedMethod('piperStageWrapper', [Map.class, Closure.class], {m, body -> 31 return body() 32 }) 33 helper.registerAllowedMethod('sonarExecuteScan', [Map.class], {m -> 34 stepsCalled.add('sonarExecuteScan') 35 stepParameters.sonarExecuteScan = m 36 }) 37 } 38 39 @Test 40 void testStageDefault() { 41 jsr.step.piperPipelineStageCompliance( 42 script: nullScript, 43 juStabUtils: utils, 44 ) 45 assertThat(stepsCalled, not(anyOf(hasItems('sonarExecuteScan')))) 46 } 47 48 @Test 49 void testSonarExecuteScan() { 50 jsr.step.piperPipelineStageCompliance( 51 script: nullScript, 52 juStabUtils: utils, 53 sonarExecuteScan: true 54 ) 55 assertThat(stepsCalled, hasItems('sonarExecuteScan')) 56 assertNotNull(stepParameters.sonarExecuteScan) 57 } 58 }