1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/test/groovy/com/sap/piper/GitUtilsTest.groovy

126 lines
4.3 KiB
Groovy

package com.sap.piper
import hudson.AbortException
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsShellCallRule
import util.Rules
import static org.junit.Assert.assertEquals
import static org.hamcrest.Matchers.equalTo
import static org.junit.Assert.assertTrue
import static org.junit.Assert.assertFalse
import static org.hamcrest.Matchers.is
import static org.hamcrest.Matchers.notNullValue
import static org.junit.Assert.assertNotNull
import static org.junit.Assert.assertNull
import static org.junit.Assert.assertThat
class GitUtilsTest extends BasePiperTest {
JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
ExpectedException thrown = ExpectedException.none()
@Rule
public RuleChain ruleChain = Rules.getCommonRules(this).around(jscr).around(thrown)
@Before
void init() throws Exception {
jscr.setReturnValue('git rev-parse HEAD', 'testCommitId')
}
@Test
void TestIsInsideWorkTree() {
jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
assertTrue(gitUtils.insideWorkTree())
}
@Test
void TestIsNotInsideWorkTree() {
jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 1)
assertFalse(gitUtils.insideWorkTree())
}
@Test
void testWorkTreeDirty() {
jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
jscr.setReturnValue('git diff --quiet HEAD', 0)
assertFalse(gitUtils.isWorkTreeDirty())
}
@Test
void testWorkTreeDirtyOutsideWorktree() {
thrown.expect(AbortException)
thrown.expectMessage('Method \'isWorkTreeClean\' called outside a git work tree.')
jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 1)
gitUtils.isWorkTreeDirty()
}
@Test
void testWorkTreeNotDirty() {
jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
jscr.setReturnValue('git diff --quiet HEAD', 1)
assertTrue(gitUtils.isWorkTreeDirty())
}
@Test
void testWorkTreeDirtyGeneralGitTrouble() {
thrown.expect(AbortException)
thrown.expectMessage('git command \'git diff --quiet HEAD\' return with code \'129\'. This indicates general trouble with git.')
jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
jscr.setReturnValue('git diff --quiet HEAD', 129) // e.g. when called outside work tree
gitUtils.isWorkTreeDirty()
}
@Test
void testGetGitCommitId() {
jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
assertEquals('testCommitId', gitUtils.getGitCommitIdOrNull())
}
@Test
void testGetGitCommitIdNotAGitRepo() {
jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 128)
assertNull(gitUtils.getGitCommitIdOrNull())
}
@Test
void testExtractLogLinesWithDefaults() {
gitUtils.extractLogLines()
assertTrue(jscr.shell
.stream()
.anyMatch( { it ->
it.contains('git log --pretty=format:%b origin/master..HEAD')}))
}
@Test
void testExtractLogLinesWithCustomValues() {
gitUtils.extractLogLines('myFilter', 'HEAD~5', 'HEAD~1', '%B')
assertTrue( jscr.shell
.stream()
.anyMatch( { it ->
it.contains('git log --pretty=format:%B HEAD~5..HEAD~1')}))
}
@Test
void testExtractLogLinesFilter() {
jscr.setReturnValue('#!/bin/bash git log --pretty=format:%b origin/master..HEAD', 'abc\n123')
String[] log = gitUtils.extractLogLines('12.*')
assertThat(log, is(notNullValue()))
assertThat(log.size(),is(equalTo(1)))
assertThat(log[0], is(equalTo('123')))
}
@Test
void testExtractLogLinesFilterNoMatch() {
jscr.setReturnValue('#!/bin/bash git log --pretty=format:%b origin/master..HEAD', 'abc\n123')
String[] log = gitUtils.extractLogLines('xyz')
assertNotNull(log)
assertThat(log.size(),is(equalTo(0)))
}
}