2018-02-07 14:17:33 +02:00
|
|
|
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
|
2018-06-06 11:19:19 +02:00
|
|
|
import util.BasePiperTest
|
2018-02-07 14:17:33 +02:00
|
|
|
import util.JenkinsShellCallRule
|
|
|
|
import util.Rules
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals
|
2018-05-07 15:52:23 +02:00
|
|
|
import static org.hamcrest.Matchers.equalTo
|
2018-05-11 14:42:33 +02:00
|
|
|
import static org.junit.Assert.assertTrue
|
2018-05-07 15:52:23 +02:00
|
|
|
import static org.junit.Assert.assertFalse
|
|
|
|
import static org.hamcrest.Matchers.is
|
|
|
|
import static org.hamcrest.Matchers.notNullValue
|
|
|
|
import static org.junit.Assert.assertNotNull
|
2018-03-05 10:04:53 +02:00
|
|
|
import static org.junit.Assert.assertNull
|
2018-05-07 15:52:23 +02:00
|
|
|
import static org.junit.Assert.assertThat
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
class GitUtilsTest extends BasePiperTest {
|
2018-02-07 14:17:33 +02:00
|
|
|
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
|
2018-05-11 14:42:33 +02:00
|
|
|
@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())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-07 14:17:33 +02:00
|
|
|
@Test
|
|
|
|
void testGetGitCommitId() {
|
2018-05-07 14:46:51 +02:00
|
|
|
jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
|
2018-03-05 10:04:53 +02:00
|
|
|
assertEquals('testCommitId', gitUtils.getGitCommitIdOrNull())
|
|
|
|
}
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-03-05 10:04:53 +02:00
|
|
|
@Test
|
|
|
|
void testGetGitCommitIdNotAGitRepo() {
|
2018-05-07 14:46:51 +02:00
|
|
|
jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 128)
|
2018-03-05 10:04:53 +02:00
|
|
|
assertNull(gitUtils.getGitCommitIdOrNull())
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 15:52:23 +02:00
|
|
|
@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)))
|
|
|
|
}
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|