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

     1  package com.sap.piper
     2  
     3  import hudson.AbortException
     4  import static org.hamcrest.Matchers.equalTo
     5  import static org.hamcrest.Matchers.hasEntry
     6  import static org.hamcrest.Matchers.is
     7  import static org.hamcrest.Matchers.notNullValue
     8  import static org.hamcrest.Matchers.startsWith
     9  
    10  import org.junit.Before
    11  import org.junit.Rule
    12  import org.junit.Test
    13  import org.junit.rules.ExpectedException
    14  import org.junit.rules.RuleChain
    15  
    16  import util.BasePiperTest
    17  import util.JenkinsLoggingRule
    18  import util.JenkinsShellCallRule
    19  import util.Rules
    20  
    21  import static org.junit.Assert.assertEquals
    22  import static org.junit.Assert.assertTrue
    23  import static org.junit.Assert.assertFalse
    24  import static org.junit.Assert.assertNotNull
    25  import static org.junit.Assert.assertNull
    26  import static org.junit.Assert.assertThat
    27  
    28  import org.springframework.beans.factory.annotation.Autowired
    29  
    30  class GitUtilsTest extends BasePiperTest {
    31  
    32      @Autowired
    33      GitUtils gitUtils
    34  
    35      private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
    36      private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
    37      private ExpectedException thrown = ExpectedException.none()
    38  
    39      @Rule
    40      public RuleChain ruleChain = Rules.getCommonRules(this)
    41          .around(loggingRule)
    42          .around(shellRule)
    43          .around(thrown)
    44  
    45      @Before
    46      void init() throws Exception {
    47          shellRule.setReturnValue('git rev-parse HEAD', 'testCommitId')
    48      }
    49  
    50      @Test
    51      void TestIsInsideWorkTree() {
    52          shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
    53          assertTrue(gitUtils.insideWorkTree())
    54      }
    55  
    56      @Test
    57      void TestIsNotInsideWorkTree() {
    58          shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 1)
    59          assertFalse(gitUtils.insideWorkTree())
    60      }
    61  
    62      @Test
    63      void testWorkTreeDirty() {
    64          shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
    65          shellRule.setReturnValue('git diff --quiet HEAD', 0)
    66          assertFalse(gitUtils.isWorkTreeDirty())
    67      }
    68  
    69      @Test
    70      void testWorkTreeDirtyOutsideWorktree() {
    71          thrown.expect(AbortException)
    72          thrown.expectMessage('Method \'isWorkTreeClean\' called outside a git work tree.')
    73          shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 1)
    74          gitUtils.isWorkTreeDirty()
    75      }
    76  
    77      @Test
    78      void testWorkTreeNotDirty() {
    79          shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
    80          shellRule.setReturnValue('git diff --quiet HEAD', 1)
    81          assertTrue(gitUtils.isWorkTreeDirty())
    82      }
    83  
    84      @Test
    85      void testWorkTreeDirtyGeneralGitTrouble() {
    86          thrown.expect(AbortException)
    87          thrown.expectMessage('git command \'git diff --quiet HEAD\' return with code \'129\'. This indicates general trouble with git.')
    88          shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
    89          shellRule.setReturnValue('git diff --quiet HEAD', 129) // e.g. when called outside work tree
    90          gitUtils.isWorkTreeDirty()
    91      }
    92  
    93      @Test
    94      void testGetGitCommitId() {
    95          shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
    96          assertEquals('testCommitId', gitUtils.getGitCommitIdOrNull())
    97      }
    98  
    99      @Test
   100      void testGetGitCommitIdNotAGitRepo() {
   101          shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 128)
   102          assertNull(gitUtils.getGitCommitIdOrNull())
   103      }
   104  
   105      @Test
   106      void testExtractLogLinesWithDefaults() {
   107          gitUtils.extractLogLines()
   108          assertTrue(shellRule.shell
   109                           .stream()
   110                             .anyMatch( { it ->
   111                               it.contains('git log --pretty=format:%b origin/master..HEAD')}))
   112      }
   113  
   114      @Test
   115      void testExtractLogLinesWithCustomValues() {
   116          gitUtils.extractLogLines('myFilter', 'HEAD~5', 'HEAD~1', '%B')
   117          assertTrue( shellRule.shell
   118                            .stream()
   119                              .anyMatch( { it ->
   120                                 it.contains('git log --pretty=format:%B HEAD~5..HEAD~1')}))
   121      }
   122  
   123      @Test
   124      void testExtractLogLinesFilter() {
   125          shellRule.setReturnValue('#!/bin/bash git log --pretty=format:%b origin/master..HEAD', 'abc\n123')
   126          String[] log = gitUtils.extractLogLines('12.*')
   127          assertThat(log, is(notNullValue()))
   128          assertThat(log.size(),is(equalTo(1)))
   129          assertThat(log[0], is(equalTo('123')))
   130      }
   131  
   132      @Test
   133      void testExtractLogLinesFilterNoMatch() {
   134          shellRule.setReturnValue('#!/bin/bash git log --pretty=format:%b origin/master..HEAD', 'abc\n123')
   135          String[] log = gitUtils.extractLogLines('xyz')
   136          assertNotNull(log)
   137          assertThat(log.size(),is(equalTo(0)))
   138      }
   139  
   140      @Test
   141      void testHandleTestRepository() {
   142          def result, gitMap, stashName, config = [
   143              testRepository: 'repoUrl',
   144              gitSshKeyCredentialsId: 'abc',
   145              gitBranch: 'master'
   146          ]
   147  
   148          helper.registerAllowedMethod('git', [Map.class], {m -> gitMap = m })
   149          helper.registerAllowedMethod("stash", [String.class], { s -> stashName = s})
   150  
   151          result = GitUtils.handleTestRepository(nullScript, config)
   152          // asserts
   153          assertThat(gitMap, hasEntry('url', config.testRepository))
   154          assertThat(gitMap, hasEntry('credentialsId', config.gitSshKeyCredentialsId))
   155          assertThat(gitMap, hasEntry('branch', config.gitBranch))
   156          assertThat(stashName, startsWith('testContent-'))
   157          assertThat(result, startsWith('testContent-'))
   158      }
   159  }