2018-02-07 14:17:33 +02:00
|
|
|
package com.sap.piper
|
|
|
|
|
2018-09-03 13:08:42 +02:00
|
|
|
import hudson.AbortException
|
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.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
|
|
|
|
|
2019-01-22 10:22:15 +02:00
|
|
|
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
|
2019-01-22 10:19:28 +02:00
|
|
|
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
|
2018-10-08 11:30:42 +02:00
|
|
|
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)
|
2019-01-22 10:22:15 +02:00
|
|
|
.around(loggingRule)
|
2019-01-22 10:19:28 +02:00
|
|
|
.around(shellRule)
|
2018-10-08 11:30:42 +02:00
|
|
|
.around(thrown)
|
2018-02-07 14:17:33 +02:00
|
|
|
|
|
|
|
@Before
|
|
|
|
void init() throws Exception {
|
2019-01-22 10:19:28 +02:00
|
|
|
shellRule.setReturnValue('git rev-parse HEAD', 'testCommitId')
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
2018-05-11 14:42:33 +02:00
|
|
|
@Test
|
|
|
|
void TestIsInsideWorkTree() {
|
2019-01-22 10:19:28 +02:00
|
|
|
shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
|
2018-05-11 14:42:33 +02:00
|
|
|
assertTrue(gitUtils.insideWorkTree())
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void TestIsNotInsideWorkTree() {
|
2019-01-22 10:19:28 +02:00
|
|
|
shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 1)
|
2018-05-11 14:42:33 +02:00
|
|
|
assertFalse(gitUtils.insideWorkTree())
|
|
|
|
}
|
|
|
|
|
2018-09-03 13:08:42 +02:00
|
|
|
@Test
|
|
|
|
void testWorkTreeDirty() {
|
2019-01-22 10:19:28 +02:00
|
|
|
shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
|
|
|
|
shellRule.setReturnValue('git diff --quiet HEAD', 0)
|
2018-09-03 13:08:42 +02:00
|
|
|
assertFalse(gitUtils.isWorkTreeDirty())
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testWorkTreeDirtyOutsideWorktree() {
|
|
|
|
thrown.expect(AbortException)
|
|
|
|
thrown.expectMessage('Method \'isWorkTreeClean\' called outside a git work tree.')
|
2019-01-22 10:19:28 +02:00
|
|
|
shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 1)
|
2018-09-03 13:08:42 +02:00
|
|
|
gitUtils.isWorkTreeDirty()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testWorkTreeNotDirty() {
|
2019-01-22 10:19:28 +02:00
|
|
|
shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
|
|
|
|
shellRule.setReturnValue('git diff --quiet HEAD', 1)
|
2018-09-03 13:08:42 +02:00
|
|
|
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.')
|
2019-01-22 10:19:28 +02:00
|
|
|
shellRule.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
|
|
|
|
shellRule.setReturnValue('git diff --quiet HEAD', 129) // e.g. when called outside work tree
|
2018-09-03 13:08:42 +02:00
|
|
|
gitUtils.isWorkTreeDirty()
|
|
|
|
}
|
2018-05-11 14:42:33 +02:00
|
|
|
|
2018-02-07 14:17:33 +02:00
|
|
|
@Test
|
|
|
|
void testGetGitCommitId() {
|
2019-01-22 10:19:28 +02:00
|
|
|
shellRule.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() {
|
2019-01-22 10:19:28 +02:00
|
|
|
shellRule.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()
|
2019-01-22 10:19:28 +02:00
|
|
|
assertTrue(shellRule.shell
|
2018-05-07 15:52:23 +02:00
|
|
|
.stream()
|
|
|
|
.anyMatch( { it ->
|
|
|
|
it.contains('git log --pretty=format:%b origin/master..HEAD')}))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testExtractLogLinesWithCustomValues() {
|
|
|
|
gitUtils.extractLogLines('myFilter', 'HEAD~5', 'HEAD~1', '%B')
|
2019-01-22 10:19:28 +02:00
|
|
|
assertTrue( shellRule.shell
|
2018-05-07 15:52:23 +02:00
|
|
|
.stream()
|
|
|
|
.anyMatch( { it ->
|
|
|
|
it.contains('git log --pretty=format:%B HEAD~5..HEAD~1')}))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testExtractLogLinesFilter() {
|
2019-01-22 10:19:28 +02:00
|
|
|
shellRule.setReturnValue('#!/bin/bash git log --pretty=format:%b origin/master..HEAD', 'abc\n123')
|
2018-05-07 15:52:23 +02:00
|
|
|
String[] log = gitUtils.extractLogLines('12.*')
|
|
|
|
assertThat(log, is(notNullValue()))
|
|
|
|
assertThat(log.size(),is(equalTo(1)))
|
|
|
|
assertThat(log[0], is(equalTo('123')))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testExtractLogLinesFilterNoMatch() {
|
2019-01-22 10:19:28 +02:00
|
|
|
shellRule.setReturnValue('#!/bin/bash git log --pretty=format:%b origin/master..HEAD', 'abc\n123')
|
2018-05-07 15:52:23 +02:00
|
|
|
String[] log = gitUtils.extractLogLines('xyz')
|
|
|
|
assertNotNull(log)
|
|
|
|
assertThat(log.size(),is(equalTo(0)))
|
2018-09-03 13:08:42 +02:00
|
|
|
}
|
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-11-07 12:44:15 +02:00
|
|
|
}
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|