mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
e61f16abfb
This approach works also in case we are in a subdirectory of the git repo, e.g. in the closure of a dir statement. The decission is delegate to git rather than using internal knowledge about git file names. Well, the directory will be always named '.git', but nevertheless ... May endup in a false positive in case we did not clone a repo AND there is another git repo somewhere upwards in the file system. Maybe some other git repo is located upstairs containing e.g. the jenkins setup. The advantage of working also for subdirectories outweights the disadvantage for the false positive. It is not likely that we have to deal with another git repo upstairs, since the pipeline script is expected to be located in the payload git repo. A phantom repo upstairs looks like a pure academical discussion.
53 lines
1.5 KiB
Groovy
53 lines
1.5 KiB
Groovy
package com.sap.piper
|
|
|
|
import com.lesfurets.jenkins.unit.BasePipelineTest
|
|
import org.junit.Before
|
|
import org.junit.Rule
|
|
import org.junit.Test
|
|
import org.junit.rules.ExpectedException
|
|
import org.junit.rules.RuleChain
|
|
import util.JenkinsShellCallRule
|
|
import util.MockHelper
|
|
import util.Rules
|
|
|
|
import static org.junit.Assert.assertEquals
|
|
import static org.junit.Assert.assertNull
|
|
|
|
class GitUtilsTest extends BasePipelineTest {
|
|
|
|
JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
|
|
ExpectedException thrown = ExpectedException.none()
|
|
|
|
@Rule
|
|
public RuleChain ruleChain = Rules.getCommonRules(this).around(jscr).around(thrown)
|
|
|
|
GitUtils gitUtils
|
|
|
|
@Before
|
|
void init() throws Exception {
|
|
gitUtils = new GitUtils()
|
|
prepareObjectInterceptors(gitUtils)
|
|
|
|
jscr.setReturnValue('git rev-parse HEAD', 'testCommitId')
|
|
}
|
|
|
|
void prepareObjectInterceptors(object) {
|
|
object.metaClass.invokeMethod = helper.getMethodInterceptor()
|
|
object.metaClass.static.invokeMethod = helper.getMethodInterceptor()
|
|
object.metaClass.methodMissing = helper.getMethodMissingInterceptor()
|
|
}
|
|
|
|
@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())
|
|
}
|
|
|
|
}
|