github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/NpmExecuteTest.groovy (about)

     1  import static org.junit.Assert.assertEquals
     2  import hudson.AbortException
     3  import org.junit.After
     4  import org.junit.Before
     5  import org.junit.Rule
     6  import org.junit.Test
     7  import org.junit.rules.ExpectedException
     8  import org.junit.rules.RuleChain
     9  import util.BasePiperTest
    10  import util.JenkinsDockerExecuteRule
    11  import util.JenkinsReadYamlRule
    12  import util.JenkinsShellCallRule
    13  import util.JenkinsStepRule
    14  import util.Rules
    15  import com.sap.piper.Utils
    16  
    17  class NpmExecuteTest extends BasePiperTest {
    18  
    19      private ExpectedException thrown = new ExpectedException().none()
    20      private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
    21      private JenkinsDockerExecuteRule dockerExecuteRule = new JenkinsDockerExecuteRule(this)
    22      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    23      private JenkinsReadYamlRule yamlRule = new JenkinsReadYamlRule(this)
    24  
    25      @Rule
    26      public RuleChain ruleChain = Rules
    27          .getCommonRules(this)
    28          .around(thrown)
    29          .around(yamlRule)
    30          .around(dockerExecuteRule)
    31          .around(shellRule)
    32          .around(stepRule)
    33  
    34      @Before
    35      void init() {
    36          helper.registerAllowedMethod 'fileExists', [String], { s -> s == 'package.json' }
    37          Utils.metaClass.echo = { def m -> }
    38      }
    39  
    40      @After
    41      public void tearDown() {
    42          Utils.metaClass = null
    43      }
    44  
    45      @Test
    46      void testNpmExecute() {
    47          stepRule.step.npmExecute(script: nullScript, dockerImage: 'node:lts-buster')
    48          assertEquals 'node:lts-buster', dockerExecuteRule.dockerParams.dockerImage
    49      }
    50  
    51      @Test
    52      void testDockerFromCustomStepConfiguration() {
    53  
    54          def expectedImage = 'image:test'
    55          def expectedEnvVars = ['env1': 'value1', 'env2': 'value2']
    56          def expectedOptions = '--opt1=val1 --opt2=val2 --opt3'
    57          def expectedWorkspace = '/path/to/workspace'
    58  
    59          nullScript.commonPipelineEnvironment.configuration = [steps:[npmExecute:[
    60              dockerImage: expectedImage,
    61              dockerOptions: expectedOptions,
    62              dockerEnvVars: expectedEnvVars,
    63              dockerWorkspace: expectedWorkspace
    64              ]]]
    65  
    66          stepRule.step.npmExecute(
    67              script: nullScript,
    68              juStabUtils: utils
    69          )
    70  
    71          assert expectedImage == dockerExecuteRule.dockerParams.dockerImage
    72          assert expectedOptions == dockerExecuteRule.dockerParams.dockerOptions
    73          assert expectedEnvVars.equals(dockerExecuteRule.dockerParams.dockerEnvVars)
    74          assert expectedWorkspace == dockerExecuteRule.dockerParams.dockerWorkspace
    75      }
    76  
    77      @Test
    78      void testNpmExecuteWithClosure() {
    79          stepRule.step.npmExecute(script: nullScript, dockerImage: 'node:lts-buster', npmCommand: 'run build') { }
    80          assert shellRule.shell.find { c -> c.contains('npm run build') }
    81      }
    82  
    83      @Test
    84      void testNoPackageJson() {
    85          helper.registerAllowedMethod 'fileExists', [String], { false }
    86          thrown.expect AbortException
    87          thrown.expectMessage '[npmExecute] package.json is not found.'
    88          stepRule.step.npmExecute(script: nullScript, dockerImage: 'node:lts-buster', npmCommand: 'run build')
    89      }
    90  }