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
Marcus Holl 3499d4843c GitUtils: Extract lines matching a pattern from a set of commits
In preparation of SOLMAN features. Here we need to get a change
document ID out of the commit message.

Start and end commit can be provided as well as a log format
and a filter condition.
2018-06-11 09:53:26 +02:00

95 lines
3.0 KiB
Groovy

package com.sap.piper
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 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)))
}
}