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/TestsPublishResultsTest.groovy

234 lines
11 KiB
Groovy
Raw Normal View History

import org.junit.After
2018-02-02 13:25:18 +02:00
import org.junit.Before
2018-02-02 15:45:45 +02:00
import org.junit.Ignore
2018-02-02 13:25:18 +02:00
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.rules.ExpectedException
2018-02-02 13:25:18 +02:00
import util.BasePiperTest
import util.JenkinsReadYamlRule
import util.JenkinsStepRule
2018-02-02 13:25:18 +02:00
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue
import com.sap.piper.Utils
2018-02-02 13:25:18 +02:00
import util.Rules
import minimatch.Minimatch
2018-02-02 13:25:18 +02:00
class TestsPublishResultsTest extends BasePiperTest {
2018-02-02 13:25:18 +02:00
Map publisherStepOptions
List archiveStepPatterns
private ExpectedException thrown = ExpectedException.none()
2019-01-22 10:25:42 +02:00
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
2018-02-02 13:25:18 +02:00
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(thrown)
2019-01-22 10:25:42 +02:00
.around(stepRule)
2018-02-02 13:25:18 +02:00
@Before
void init() {
publisherStepOptions = [:]
archiveStepPatterns = []
// prepare checkResultsPublish step
helper.registerAllowedMethod('junit', [Map.class], {
parameters -> publisherStepOptions['junit'] = parameters
})
helper.registerAllowedMethod('jacoco', [Map.class], {
parameters -> publisherStepOptions['jacoco'] = parameters
})
helper.registerAllowedMethod('cobertura', [Map.class], {
parameters -> publisherStepOptions['cobertura'] = parameters
})
2018-02-19 11:26:50 +02:00
helper.registerAllowedMethod('perfReport', [Map.class], {
parameters -> publisherStepOptions['jmeter'] = parameters
})
helper.registerAllowedMethod('cucumber', [Map.class], {
parameters -> publisherStepOptions['cucumber'] = parameters
})
helper.registerAllowedMethod('publishHTML', [Map.class], {
parameters -> publisherStepOptions['htmlPublisher'] = parameters
})
2018-02-02 13:25:18 +02:00
helper.registerAllowedMethod('archiveArtifacts', [Map.class], {
parameters -> archiveStepPatterns.push(parameters.artifacts)
})
Utils.metaClass.echo = { def m -> }
}
@After
public void tearDown() {
Utils.metaClass = null
2018-02-02 13:25:18 +02:00
}
@Test
2018-02-02 15:45:45 +02:00
void testPublishNothingWithDefaultSettings() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.testsPublishResults(script: nullScript)
2018-02-02 13:25:18 +02:00
// ensure nothing is published
2018-02-02 15:45:45 +02:00
assertTrue('WarningsPublisher options not empty', publisherStepOptions.junit == null)
assertTrue('PmdPublisher options not empty', publisherStepOptions.jacoco == null)
assertTrue('DryPublisher options not empty', publisherStepOptions.cobertura == null)
2018-02-19 11:26:50 +02:00
assertTrue('FindBugsPublisher options not empty', publisherStepOptions.jmeter == null)
assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
2018-02-02 15:45:45 +02:00
}
@Test
void testPublishNothingWithAllDisabled() throws Exception {
stepRule.step.testsPublishResults(script: nullScript, junit: false, jacoco: false, cobertura: false, jmeter: false, cucumber: false, htmlPublisher: false)
2018-02-02 15:45:45 +02:00
// ensure nothing is published
assertTrue('WarningsPublisher options not empty', publisherStepOptions.junit == null)
assertTrue('PmdPublisher options not empty', publisherStepOptions.jacoco == null)
assertTrue('DryPublisher options not empty', publisherStepOptions.cobertura == null)
2018-02-19 11:26:50 +02:00
assertTrue('FindBugsPublisher options not empty', publisherStepOptions.jmeter == null)
assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
2018-02-02 13:25:18 +02:00
}
@Test
2018-02-09 00:48:49 +02:00
void testPublishUnitTestsWithDefaultSettings() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.testsPublishResults(script: nullScript, junit: true)
2018-02-02 13:25:18 +02:00
assertTrue('JUnit options are empty', publisherStepOptions.junit != null)
// ensure default patterns are set
assertEquals('JUnit default pattern not set correct',
'**/TEST-*.xml', publisherStepOptions.junit.testResults)
2018-02-02 15:45:45 +02:00
// ensure nothing else is published
assertTrue('JaCoCo options are not empty', publisherStepOptions.jacoco == null)
assertTrue('Cobertura options are not empty', publisherStepOptions.cobertura == null)
2018-02-19 11:26:50 +02:00
assertTrue('JMeter options are not empty', publisherStepOptions.jmeter == null)
assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
2018-02-02 15:45:45 +02:00
}
@Test
void testPublishCoverageWithDefaultSettings() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.testsPublishResults(script: nullScript, jacoco: true, cobertura: true)
2018-02-02 15:45:45 +02:00
assertTrue('JaCoCo options are empty', publisherStepOptions.jacoco != null)
2018-02-02 13:25:18 +02:00
assertEquals('JaCoCo default pattern not set correct',
2018-02-02 15:45:45 +02:00
'**/target/*.exec', publisherStepOptions.jacoco.execPattern)
// ensure nothing else is published
assertTrue('JUnit options are not empty', publisherStepOptions.junit == null)
2018-02-19 11:26:50 +02:00
assertTrue('JMeter options are not empty', publisherStepOptions.jmeter == null)
assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
assertTrue('Cobertura options are empty', publisherStepOptions.cobertura != null)
assertTrue('Cobertura default pattern is empty', publisherStepOptions.cobertura.coberturaReportFile != null)
String sampleCoberturaPathForJava = 'my/workspace/my/project/target/coverage/cobertura-coverage.xml'
assertTrue('Cobertura default pattern does not match files at target/coverage/cobertura-coverage.xml for Java projects',
Minimatch.minimatch(sampleCoberturaPathForJava, publisherStepOptions.cobertura.coberturaReportFile))
String sampleCoberturaPathForKarma = 'my/workspace/my/project/target/coverage/Chrome 78.0.3904 (Mac OS X 10.14.6)/cobertura-coverage.xml'
assertTrue('Cobertura default pattern does not match files at target/coverage/<browser>/cobertura-coverage.xml for UI5 projects',
Minimatch.minimatch(sampleCoberturaPathForKarma, publisherStepOptions.cobertura.coberturaReportFile))
2018-02-02 15:45:45 +02:00
}
@Test
void testPublishJMeterWithDefaultSettings() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.testsPublishResults(script: nullScript, jmeter: true)
2018-02-02 15:45:45 +02:00
2018-02-19 11:26:50 +02:00
assertTrue('JMeter options are empty', publisherStepOptions.jmeter != null)
assertEquals('JMeter default pattern not set',
'**/*.jtl', publisherStepOptions.jmeter.sourceDataFiles)
2018-02-02 15:45:45 +02:00
// ensure nothing else is published
assertTrue('JUnit options are not empty', publisherStepOptions.junit == null)
assertTrue('JaCoCo options are not empty', publisherStepOptions.jacoco == null)
assertTrue('Cobertura options are not empty', publisherStepOptions.cobertura == null)
assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
2018-02-02 13:25:18 +02:00
}
2018-02-19 11:26:50 +02:00
@Test
void testPublishUnitTestsWithCustomSettings() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.testsPublishResults(script: nullScript, junit: [pattern: 'fancy/file/path', archive: true, active: true])
2018-02-19 11:26:50 +02:00
assertTrue('JUnit options are empty', publisherStepOptions.junit != null)
// ensure default patterns are set
assertEquals('JUnit pattern not set correct',
'fancy/file/path', publisherStepOptions.junit.testResults)
assertEquals('JUnit default pattern not set correct',
'fancy/file/path', publisherStepOptions.junit.testResults)
// ensure nothing else is published
assertTrue('JaCoCo options are not empty', publisherStepOptions.jacoco == null)
assertTrue('Cobertura options are not empty', publisherStepOptions.cobertura == null)
assertTrue('JMeter options are not empty', publisherStepOptions.jmeter == null)
assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
}
@Test
void testPublishCucumberResults() throws Exception {
stepRule.step.testsPublishResults(script: nullScript, cucumber: [pattern: 'fancy/file/path', archive: true, active: true])
assertTrue('Cucumber options are empty', publisherStepOptions.cucumber != null)
assertEquals('Cucumber pattern not set correct',
'fancy/file/path', publisherStepOptions.cucumber.testResults)
assertTrue('JUnit options are not empty', publisherStepOptions.junit == null)
assertTrue('JaCoCo options are not empty', publisherStepOptions.jacoco == null)
assertTrue('Cobertura options are not empty', publisherStepOptions.cobertura == null)
assertTrue('JMeter options are not empty', publisherStepOptions.jmeter == null)
assertTrue('HtmlPublisher options not empty', publisherStepOptions.htmlPublisher == null)
}
@Test
void testPublishHtmlResults() throws Exception {
stepRule.step.testsPublishResults(script: nullScript, htmlPublisher: [pattern: 'fancy/file/path', active: true])
assertTrue('HtmlPublisher options are empty', publisherStepOptions.htmlPublisher != null)
assertEquals('HtmlPublisher pattern not set correct',
'fancy/file/path', publisherStepOptions.htmlPublisher.target.reportFiles)
assertTrue('JUnit options are not empty', publisherStepOptions.junit == null)
assertTrue('JaCoCo options are not empty', publisherStepOptions.jacoco == null)
assertTrue('Cobertura options are not empty', publisherStepOptions.cobertura == null)
assertTrue('JMeter options are not empty', publisherStepOptions.jmeter == null)
assertTrue('Cucumber options not empty', publisherStepOptions.cucumber == null)
2018-02-19 11:26:50 +02:00
}
@Test
void testBuildResultStatus() throws Exception {
stepRule.step.testsPublishResults(script: nullScript)
assertJobStatusSuccess()
}
@Test
void testBuildWithTestFailuresAndWithoutFailOnError() throws Exception {
nullScript.currentBuild.getRawBuild = {
return [getAction: { type ->
return [getFailCount: {
return 6
}]
}]
}
stepRule.step.testsPublishResults(script: nullScript)
assertJobStatusSuccess()
}
@Test
void testBuildWithTestFailuresAndWithFailOnError() throws Exception {
nullScript.currentBuild.getRawBuild = {
return [getAction: { type ->
return [getFailCount: {
return 6
}]
}]
}
thrown.expect(hudson.AbortException)
thrown.expectMessage('[testsPublishResults] Some tests failed!')
stepRule.step.testsPublishResults(script: nullScript, failOnError: true)
}
2018-02-02 13:25:18 +02:00
}