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.
147 lines
5.7 KiB
Groovy
147 lines
5.7 KiB
Groovy
#!groovy
|
|
import com.lesfurets.jenkins.unit.BasePipelineTest
|
|
import com.sap.piper.GitUtils
|
|
import org.junit.Before
|
|
import org.junit.Rule
|
|
import org.junit.Test
|
|
import org.junit.rules.ExpectedException
|
|
import org.junit.rules.RuleChain
|
|
import util.JenkinsEnvironmentRule
|
|
import util.JenkinsLoggingRule
|
|
import util.JenkinsReadMavenPomRule
|
|
import util.JenkinsShellCallRule
|
|
import util.JenkinsStepRule
|
|
import util.JenkinsWriteFileRule
|
|
import util.Rules
|
|
|
|
import static org.junit.Assert.assertEquals
|
|
|
|
class ArtifactSetVersionTest extends BasePipelineTest {
|
|
Map dockerParameters
|
|
def mavenExecuteScript
|
|
|
|
def gitUtils
|
|
def sshAgentList = []
|
|
|
|
private ExpectedException thrown = ExpectedException.none()
|
|
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
|
|
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
|
|
private JenkinsWriteFileRule jwfr = new JenkinsWriteFileRule(this)
|
|
private JenkinsStepRule jsr = new JenkinsStepRule(this)
|
|
private JenkinsEnvironmentRule jer = new JenkinsEnvironmentRule(this)
|
|
|
|
@Rule
|
|
public RuleChain ruleChain = Rules
|
|
.getCommonRules(this)
|
|
.around(thrown)
|
|
.around(jlr)
|
|
.around(jscr)
|
|
.around(new JenkinsReadMavenPomRule(this, 'test/resources/MavenArtifactVersioning'))
|
|
.around(jwfr)
|
|
.around(jsr)
|
|
.around(jer)
|
|
|
|
@Before
|
|
void init() throws Throwable {
|
|
dockerParameters = [:]
|
|
|
|
helper.registerAllowedMethod("dockerExecute", [Map.class, Closure.class],
|
|
{ parameters, closure ->
|
|
dockerParameters = parameters
|
|
closure()
|
|
})
|
|
|
|
mavenExecuteScript = loadScript("mavenExecute.groovy").mavenExecute
|
|
|
|
helper.registerAllowedMethod("sshagent", [List.class, Closure.class], { list, closure ->
|
|
sshAgentList = list
|
|
return closure()
|
|
})
|
|
|
|
jscr.setReturnValue('git rev-parse HEAD', 'testCommitId')
|
|
jscr.setReturnValue("date --universal +'%Y%m%d%H%M%S'", '20180101010203')
|
|
jscr.setReturnValue('git diff --quiet HEAD', 0)
|
|
jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
|
|
|
|
binding.setVariable('Jenkins', [instance: [pluginManager: [plugins: [new DockerExecuteTest.PluginMock()]]]])
|
|
|
|
|
|
gitUtils = new GitUtils()
|
|
prepareObjectInterceptors(gitUtils)
|
|
|
|
this.helper.registerAllowedMethod('fileExists', [String.class], {true})
|
|
}
|
|
|
|
@Test
|
|
void testVersioning() {
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', gitSshUrl: 'myGitSshUrl')
|
|
|
|
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
|
|
assertEquals('testCommitId', jer.env.getGitCommitId())
|
|
|
|
assertEquals('mvn --file \'pom.xml\' versions:set -DnewVersion=1.2.3-20180101010203_testCommitId', jscr.shell[6])
|
|
assertEquals('git add .', jscr.shell[7])
|
|
assertEquals ("git commit -m 'update version 1.2.3-20180101010203_testCommitId'", jscr.shell[8])
|
|
assertEquals ("git remote set-url origin myGitSshUrl", jscr.shell[9])
|
|
assertEquals ("git tag build_1.2.3-20180101010203_testCommitId", jscr.shell[10])
|
|
assertEquals ("git push origin build_1.2.3-20180101010203_testCommitId", jscr.shell[11])
|
|
}
|
|
|
|
@Test
|
|
void testVersioningWithoutCommit() {
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', commitVersion: false)
|
|
|
|
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
|
|
assertEquals('mvn --file \'pom.xml\' versions:set -DnewVersion=1.2.3-20180101010203_testCommitId', jscr.shell[6])
|
|
}
|
|
|
|
@Test
|
|
void testVersioningWithoutScript() {
|
|
jsr.step.call(juStabGitUtils: gitUtils, buildTool: 'maven', commitVersion: false)
|
|
|
|
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
|
|
assertEquals('mvn --file \'pom.xml\' versions:set -DnewVersion=1.2.3-20180101010203_testCommitId', jscr.shell[6])
|
|
}
|
|
|
|
@Test
|
|
void testVersioningCustomGitUserAndEMail() {
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', gitSshUrl: 'myGitSshUrl', gitUserEMail: 'test@test.com', gitUserName: 'test')
|
|
|
|
assertEquals ('git -c user.email="test@test.com" -c user.name="test" commit -m \'update version 1.2.3-20180101010203_testCommitId\'', jscr.shell[8])
|
|
}
|
|
|
|
@Test
|
|
void testVersioningWithTimestamp() {
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', timestamp: '2018')
|
|
assertEquals('1.2.3-2018_testCommitId', jer.env.getArtifactVersion())
|
|
}
|
|
|
|
@Test
|
|
void testVersioningNoBuildTool() {
|
|
thrown.expect(Exception)
|
|
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR buildTool')
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils)
|
|
}
|
|
|
|
@Test
|
|
void testVersioningWithCustomTemplate() {
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', versioningTemplate: '${version}-xyz')
|
|
assertEquals('1.2.3-xyz', jer.env.getArtifactVersion())
|
|
}
|
|
|
|
@Test
|
|
void testVersioningWithTypeAppContainer() {
|
|
jer.env.setArtifactVersion('1.2.3-xyz')
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'docker', artifactType: 'appContainer', dockerVersionSource: 'appVersion')
|
|
assertEquals('1.2.3-xyz', jer.env.getArtifactVersion())
|
|
assertEquals('1.2.3-xyz', jwfr.files['VERSION'])
|
|
}
|
|
|
|
void prepareObjectInterceptors(object) {
|
|
object.metaClass.invokeMethod = helper.getMethodInterceptor()
|
|
object.metaClass.static.invokeMethod = helper.getMethodInterceptor()
|
|
object.metaClass.methodMissing = helper.getMethodMissingInterceptor()
|
|
}
|
|
|
|
}
|