diff --git a/src/main/groovy/de/jansauer/printcoverage/PrintCoverageTask.groovy b/src/main/groovy/de/jansauer/printcoverage/PrintCoverageTask.groovy index e62ded2..638fbd8 100644 --- a/src/main/groovy/de/jansauer/printcoverage/PrintCoverageTask.groovy +++ b/src/main/groovy/de/jansauer/printcoverage/PrintCoverageTask.groovy @@ -6,11 +6,17 @@ import org.gradle.api.provider.Property import org.gradle.api.tasks.Input import org.gradle.api.tasks.TaskAction +import java.nio.file.Files +import java.nio.file.Path +import java.util.regex.Pattern + class PrintCoverageTask extends DefaultTask { @Input final Property coverageType = project.objects.property(String) + def testReportPattern = Pattern.compile("jacoco.*Report\\.xml", Pattern.CASE_INSENSITIVE) + PrintCoverageTask() { setDescription('Prints code coverage for gitlab.') setGroup('coverage') @@ -22,16 +28,29 @@ class PrintCoverageTask extends DefaultTask { slurper.setFeature('http://apache.org/xml/features/disallow-doctype-decl', false) slurper.setFeature('http://apache.org/xml/features/nonvalidating/load-external-dtd', false) - File jacocoTestReport = new File("${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml") - if (!jacocoTestReport.exists()) { + Collection jacocoTestReports = Files.find( + Path.of(project.buildDir.path, "reports", "jacoco"), + 2, + { path, att -> path.fileName.toString().matches(testReportPattern) }) + .collect() + + if (!jacocoTestReports.any()) { logger.error('Jacoco test report is missing.') throw new GradleException('Jacoco test report is missing.') } - def report = slurper.parse(jacocoTestReport) - double missed = report.counter.find { it.'@type' == coverageType.get() }.@missed.toDouble() - double covered = report.counter.find { it.'@type' == coverageType.get() }.@covered.toDouble() - def coverage = (100 / (missed + covered) * covered).round(2) - println 'Coverage: ' + coverage + '%' + double maxCoverage = 0 + + jacocoTestReports.each {Path jacocoTestReport -> + def report = slurper.parse(jacocoTestReport.toFile()) + def coverageTag = report.counter.find { it.'@type' == coverageType.get() } + double missed = coverageTag.@missed.toDouble() + double covered = coverageTag.@covered.toDouble() + double coverage = (100 / (missed + covered) * covered).round(2) + // we cannot differ, if the coverage covers the same or different code, so we can only take the maximum value + maxCoverage = Math.max(maxCoverage, coverage) + println "Coverage ($jacocoTestReport.fileName): $coverage%" + } + println "Coverage: $maxCoverage%" } } diff --git a/src/test/groovy/de/jansauer/printcoverage/PrintCoveragePluginTest.groovy b/src/test/groovy/de/jansauer/printcoverage/PrintCoveragePluginTest.groovy index d60a71f..8e454cd 100644 --- a/src/test/groovy/de/jansauer/printcoverage/PrintCoveragePluginTest.groovy +++ b/src/test/groovy/de/jansauer/printcoverage/PrintCoveragePluginTest.groovy @@ -132,6 +132,65 @@ class PrintCoveragePluginTest extends Specification { gradleVersion << SUPPORTED_GRADLE_VERSIONS.findAll { !(it =~ /4\.*/) } } + def "should print from a example class with tests and integration tests"() { + given: + buildFile << """ + plugins { + id 'java' + id 'jacoco' + id 'de.jansauer.printcoverage' + id 'org.unbroken-dome.test-sets' version '2.2.1' + } + + testSets { + integration + } + + repositories { + mavenCentral() + } + + dependencies { + testCompile 'junit:junit:4.12' + } + """ + File classFile = temporaryFolder + .newFolder('src', 'main', 'java', 'sample') + .toPath() + .resolve('Calculator.java') + .toFile() + classFile << new File("src/test/resources/Calculator.java").text + File junitFile = temporaryFolder + .newFolder('src', 'test', 'java', 'sample') + .toPath() + .resolve('CalculatorTest.java') + .toFile() + junitFile << new File("src/test/resources/CalculatorTest.java").text + File integrationFile = temporaryFolder + .newFolder('src', 'integration', 'java', 'sample') + .toPath() + .resolve('CalculatorIT.java') + .toFile() + integrationFile << new File("src/test/resources/CalculatorIT.java").text + + when: + def result = GradleRunner.create() + .withGradleVersion(gradleVersion) + .withProjectDir(temporaryFolder.root) + .withArguments('build', 'integration', 'printCoverage') + .withPluginClasspath() + .build() + + then: + result.output.contains('Coverage (jacocoIntegrationReport.xml): 100.0%') + result.output.contains('Coverage (jacocoTestReport.xml): 79.49%') + result.output.contains('Coverage: 100.0%') + result.task(":printCoverage").outcome == SUCCESS + + where: + gradleVersion << SUPPORTED_GRADLE_VERSIONS.findAll { !(it =~ /4\.*/) } + } + def "should print from a example class with tests with gradle 4.x"() { given: buildFile << """ diff --git a/src/test/resources/CalculatorIT.java b/src/test/resources/CalculatorIT.java new file mode 100644 index 0000000..affe1c0 --- /dev/null +++ b/src/test/resources/CalculatorIT.java @@ -0,0 +1,12 @@ +package sample; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class CalculatorIT { + + @Test + public void startsCalculator() { + Calculator.main(new String[0]); + } +}