1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/test/groovy/MavenExecuteTest.groovy

105 lines
4.3 KiB
Groovy
Raw Normal View History

import org.junit.Rule
import org.junit.Test
2018-01-16 16:03:00 +02:00
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsDockerExecuteRule
import util.JenkinsReadYamlRule
import util.JenkinsShellCallRule
import util.JenkinsStepRule
import util.Rules
import static org.hamcrest.Matchers.allOf
import static org.hamcrest.Matchers.containsString
import static org.hamcrest.Matchers.containsString
import static org.hamcrest.Matchers.not
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertThat
import static org.junit.Assert.assertTrue
2018-05-30 12:39:01 +02:00
class MavenExecuteTest extends BasePiperTest {
Map dockerParameters
2018-01-16 16:03:00 +02:00
2019-01-22 10:19:28 +02:00
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
private JenkinsDockerExecuteRule dockerExecuteRule = new JenkinsDockerExecuteRule(this)
2019-01-22 10:25:42 +02:00
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(dockerExecuteRule)
2019-01-22 10:19:28 +02:00
.around(shellRule)
2019-01-22 10:25:42 +02:00
.around(stepRule)
@Test
void testExecuteBasicMavenCommand() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.mavenExecute(script: nullScript, goals: 'clean install')
assertEquals('maven:3.5-jdk-7', dockerExecuteRule.dockerParams.dockerImage)
2018-01-16 16:03:00 +02:00
2019-01-22 10:19:28 +02:00
assert shellRule.shell[0] == 'mvn --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn clean install'
}
@Test
void testExecuteBasicMavenCommandWithDownloadLogsEnabled() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.mavenExecute(script: nullScript, goals: 'clean install', logSuccessfulMavenTransfers: true)
assertEquals('maven:3.5-jdk-7', dockerExecuteRule.dockerParams.dockerImage)
2019-01-22 10:19:28 +02:00
assert shellRule.shell[0] == 'mvn --batch-mode clean install'
}
@Test
void testExecuteMavenCommandWithParameter() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.mavenExecute(
2018-05-30 12:39:01 +02:00
script: nullScript,
dockerImage: 'maven:3.5-jdk-8-alpine',
goals: 'clean install',
globalSettingsFile: 'globalSettingsFile.xml',
projectSettingsFile: 'projectSettingsFile.xml',
pomPath: 'pom.xml',
flags: '-o',
m2Path: 'm2Path',
defines: '-Dmaven.tests.skip=true')
assertEquals('maven:3.5-jdk-8-alpine', dockerExecuteRule.dockerParams.dockerImage)
String mvnCommand = "mvn --global-settings 'globalSettingsFile.xml' -Dmaven.repo.local='m2Path' --settings 'projectSettingsFile.xml' --file 'pom.xml' -o --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn clean install -Dmaven.tests.skip=true"
2019-01-22 10:19:28 +02:00
assertTrue(shellRule.shell.contains(mvnCommand))
}
@Test
void testMavenCommandForwardsDockerOptions() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.mavenExecute(script: nullScript, goals: 'clean install')
assertEquals('maven:3.5-jdk-7', dockerExecuteRule.dockerParams.dockerImage)
2019-01-22 10:19:28 +02:00
assertEquals('mvn --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn clean install', shellRule.shell[0])
}
@Test
void testMavenCommandWithShortBatchModeFlag() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.mavenExecute(script: nullScript, goals: 'clean install', flags: '-B')
assertEquals('maven:3.5-jdk-7', dockerExecuteRule.dockerParams.dockerImage)
2019-01-22 10:19:28 +02:00
assertEquals('mvn -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn clean install', shellRule.shell[0])
}
@Test
void testMavenCommandWithFalsePositiveMinusBFlag() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.mavenExecute(script: nullScript, goals: 'clean install', flags: '-Blah')
assertEquals('maven:3.5-jdk-7', dockerExecuteRule.dockerParams.dockerImage)
2019-01-22 10:19:28 +02:00
assertThat(shellRule.shell[0],
allOf(containsString('-Blah'),
containsString('--batch-mode')))
}
@Test
void testMavenCommandWithBatchModeMultiline() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.mavenExecute(script: nullScript, goals: 'clean install', flags: ('''-B\\
|--show-version''' as CharSequence).stripMargin())
2019-01-22 10:19:28 +02:00
assertThat(shellRule.shell[0], not(containsString('--batch-mode')))
}
}