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

     1  import org.junit.Before
     2  import org.junit.Rule
     3  import org.junit.Test
     4  import org.junit.rules.ExpectedException
     5  import org.junit.rules.RuleChain
     6  import util.BasePiperTest
     7  import util.JenkinsCredentialsRule
     8  import util.JenkinsFileExistsRule
     9  import util.JenkinsLoggingRule
    10  import util.JenkinsReadJsonRule
    11  import util.JenkinsReadYamlRule
    12  import util.JenkinsShellCallRule
    13  import util.JenkinsStepRule
    14  import util.JenkinsWriteFileRule
    15  import util.Rules
    16  
    17  import static org.hamcrest.Matchers.*
    18  import static org.junit.Assert.assertThat
    19  
    20  class GithubPublishReleaseTest extends BasePiperTest {
    21  
    22      private JenkinsCredentialsRule credentialsRule = new JenkinsCredentialsRule(this)
    23      private JenkinsReadJsonRule readJsonRule = new JenkinsReadJsonRule(this)
    24      private JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this)
    25      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    26      private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this)
    27      private JenkinsFileExistsRule fileExistsRule = new JenkinsFileExistsRule(this, [])
    28  
    29      private List withEnvArgs = []
    30  
    31      @Rule
    32      public RuleChain rules = Rules
    33          .getCommonRules(this)
    34          .around(new JenkinsReadYamlRule(this))
    35          .around(credentialsRule)
    36          .around(readJsonRule)
    37          .around(shellCallRule)
    38          .around(stepRule)
    39          .around(writeFileRule)
    40          .around(fileExistsRule)
    41  
    42      @Before
    43      void init() {
    44          helper.registerAllowedMethod('findFiles', [Map.class], {return null})
    45          helper.registerAllowedMethod("withEnv", [List.class, Closure.class], {arguments, closure ->
    46              arguments.each {arg ->
    47                  withEnvArgs.add(arg.toString())
    48              }
    49              return closure()
    50          })
    51          credentialsRule.withCredentials('githubTokenId', 'thisIsATestToken')
    52          shellCallRule.setReturnValue('[ -x ./piper ]', 1)
    53          shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/githubPublishRelease.yaml\'', '{"githubTokenCredentialsId":"githubTokenId"}')
    54          helper.registerAllowedMethod("writePipelineEnv", [Map.class], {m -> return })
    55          helper.registerAllowedMethod("readPipelineEnv", [Map.class], {m -> return })
    56      }
    57  
    58      @Test
    59      void testGithubPublishReleaseDefault() {
    60          // test
    61          stepRule.step.githubPublishRelease(
    62              script: nullScript,
    63              juStabUtils: utils,
    64              jenkinsUtilsStub: jenkinsUtils,
    65              testParam: "This is test content"
    66          )
    67          // asserts
    68          assertThat(writeFileRule.files['.pipeline/tmp/metadata/githubPublishRelease.yaml'], containsString('name: githubPublishRelease'))
    69          assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'), containsString('"testParam":"This is test content"')))
    70          assertThat(shellCallRule.shell[2], is('./piper githubPublishRelease'))
    71      }
    72  }