github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/templates/PiperPipelineStageBuildTest.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.BasePiperTest
     8  import util.JenkinsLoggingRule
     9  import util.JenkinsReadYamlRule
    10  import util.JenkinsStepRule
    11  import util.Rules
    12  
    13  import static org.hamcrest.Matchers.anyOf
    14  import static org.hamcrest.Matchers.hasItem
    15  import static org.hamcrest.Matchers.hasItem
    16  import static org.hamcrest.Matchers.hasItem
    17  import static org.hamcrest.Matchers.hasItem
    18  import static org.hamcrest.Matchers.hasItems
    19  import static org.hamcrest.Matchers.is
    20  import static org.hamcrest.Matchers.not
    21  import static org.junit.Assert.assertThat
    22  
    23  class PiperPipelineStageBuildTest extends BasePiperTest {
    24      private JenkinsStepRule jsr = new JenkinsStepRule(this)
    25      private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
    26  
    27      @Rule
    28      public RuleChain rules = Rules
    29          .getCommonRules(this)
    30          .around(new JenkinsReadYamlRule(this))
    31          .around(jlr)
    32          .around(jsr)
    33  
    34      private List stepsCalled = []
    35      private Map stepParameters = [:]
    36  
    37      @Before
    38      void init()  {
    39  
    40          nullScript.env.STAGE_NAME = 'Build'
    41  
    42          helper.registerAllowedMethod('piperStageWrapper', [Map.class, Closure.class], {m, body ->
    43              assertThat(m.stageName, is('Build'))
    44              return body()
    45          })
    46  
    47          helper.registerAllowedMethod('buildExecute', [Map.class], {m ->
    48              stepsCalled.add('buildExecute')
    49          })
    50  
    51          helper.registerAllowedMethod('pipelineStashFilesAfterBuild', [Map.class], {m ->
    52              stepsCalled.add('pipelineStashFilesAfterBuild')
    53          })
    54  
    55          helper.registerAllowedMethod('checksPublishResults', [Map.class], {m ->
    56              stepsCalled.add('checksPublishResults')
    57          })
    58  
    59          helper.registerAllowedMethod('testsPublishResults', [Map.class], {m ->
    60              stepsCalled.add('testsPublishResults')
    61              stepParameters.testsPublishResults = m
    62          })
    63  
    64          helper.registerAllowedMethod('mavenExecuteStaticCodeChecks', [Map.class], {m ->
    65              stepsCalled.add('mavenExecuteStaticCodeChecks')
    66          })
    67  
    68          helper.registerAllowedMethod('npmExecuteLint', [Map.class], {m ->
    69              stepsCalled.add('npmExecuteLint')
    70          })
    71      }
    72  
    73      @Test
    74      void testBuildDefault() {
    75  
    76          jsr.step.piperPipelineStageBuild(script: nullScript, juStabUtils: utils)
    77  
    78          assertThat(stepsCalled, hasItems('buildExecute', 'checksPublishResults', 'pipelineStashFilesAfterBuild', 'testsPublishResults'))
    79          assertThat(stepParameters.testsPublishResults.junit.updateResults, is(true))
    80          assertThat(stepsCalled, not(anyOf(hasItem('mavenExecuteStaticCodeChecks'), hasItem('npmExecuteLint'))))
    81      }
    82  
    83      @Test
    84      void testBuildWithLinting() {
    85  
    86          nullScript.commonPipelineEnvironment.configuration = [runStep: ['Build': [npmExecuteLint: true]]]
    87  
    88          jsr.step.piperPipelineStageBuild(script: nullScript, juStabUtils: utils)
    89  
    90          assertThat(stepsCalled, hasItems('buildExecute', 'checksPublishResults', 'pipelineStashFilesAfterBuild', 'testsPublishResults', 'npmExecuteLint'))
    91      }
    92  
    93      @Test
    94      void testBuildWithMavenStaticCodeChecks() {
    95  
    96          nullScript.commonPipelineEnvironment.configuration = [runStep: ['Build': [mavenExecuteStaticCodeChecks: true]]]
    97  
    98          jsr.step.piperPipelineStageBuild(script: nullScript, juStabUtils: utils)
    99  
   100          assertThat(stepsCalled, hasItems('buildExecute', 'checksPublishResults', 'pipelineStashFilesAfterBuild', 'testsPublishResults', 'mavenExecuteStaticCodeChecks'))
   101      }
   102  }