Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multiple testsets in one gradle project #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/main/groovy/de/jansauer/printcoverage/PrintCoverageTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> coverageType = project.objects.property(String)

def testReportPattern = Pattern.compile("jacoco.*Report\\.xml", Pattern.CASE_INSENSITIVE)

PrintCoverageTask() {
setDescription('Prints code coverage for gitlab.')
setGroup('coverage')
Expand All @@ -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<Path> 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() }[email protected]()
double covered = report.counter.find { it.'@type' == coverageType.get() }[email protected]()
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 = [email protected]()
double covered = [email protected]()
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%"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 << """
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/CalculatorIT.java
Original file line number Diff line number Diff line change
@@ -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]);
}
}