2018-02-07 14:17:33 +02:00
|
|
|
package com.sap.piper
|
|
|
|
|
2018-10-08 11:30:42 +02:00
|
|
|
import static org.hamcrest.Matchers.equalTo
|
|
|
|
import static org.hamcrest.Matchers.hasEntry
|
|
|
|
import static org.hamcrest.Matchers.hasItem
|
|
|
|
import static org.hamcrest.Matchers.is
|
|
|
|
import static org.hamcrest.Matchers.notNullValue
|
|
|
|
import static org.hamcrest.Matchers.startsWith
|
|
|
|
|
2018-02-07 14:17:33 +02:00
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.rules.ExpectedException
|
|
|
|
import org.junit.rules.RuleChain
|
2018-10-08 11:30:42 +02:00
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
import util.BasePiperTest
|
2018-10-08 11:30:42 +02:00
|
|
|
import util.JenkinsLoggingRule
|
2018-02-07 14:17:33 +02:00
|
|
|
import util.JenkinsShellCallRule
|
|
|
|
import util.Rules
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals
|
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.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-09-13 16:01:35 +02:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired
|
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
class GitUtilsTest extends BasePiperTest {
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-09-13 16:01:35 +02:00
|
|
|
@Autowired
|
|
|
|
GitUtils gitUtils
|
|
|
|
|
2018-10-08 11:30:42 +02:00
|
|
|
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
|
|
|
|
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
|
|
|
|
private ExpectedException thrown = ExpectedException.none()
|
2018-02-07 14:17:33 +02:00
|
|
|
|
|
|
|
@Rule
|
2018-10-08 11:30:42 +02:00
|
|
|
public RuleChain ruleChain = Rules.getCommonRules(this)
|
|
|
|
.around(jlr)
|
|
|
|
.around(jscr)
|
|
|
|
.around(thrown)
|
2018-02-07 14:17:33 +02:00
|
|
|
|
|
|
|
@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-10-08 11:30:42 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
void testHandleTestRepository() {
|
|
|
|
def result, gitMap, stashName, config = [
|
|
|
|
testRepository: 'repoUrl',
|
|
|
|
gitSshKeyCredentialsId: 'abc',
|
|
|
|
gitBranch: 'master'
|
|
|
|
]
|
|
|
|
|
|
|
|
helper.registerAllowedMethod('git', [Map.class], {m -> gitMap = m })
|
|
|
|
helper.registerAllowedMethod("stash", [String.class], { s -> stashName = s})
|
|
|
|
|
|
|
|
result = GitUtils.handleTestRepository(nullScript, config)
|
|
|
|
// asserts
|
|
|
|
assertThat(gitMap, hasEntry('url', config.testRepository))
|
|
|
|
assertThat(gitMap, hasEntry('credentialsId', config.gitSshKeyCredentialsId))
|
|
|
|
assertThat(gitMap, hasEntry('branch', config.gitBranch))
|
|
|
|
assertThat(stashName, startsWith('testContent-'))
|
|
|
|
assertThat(result, startsWith('testContent-'))
|
|
|
|
}
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|