Skip to content

Commit

Permalink
Add configuration options for report-path and message
Browse files Browse the repository at this point in the history
  • Loading branch information
mvol-eb committed May 14, 2020
1 parent 7605f2f commit 97e94b9
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 9 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ For more information see [Gitalb test coverage parsing](https://docs.gitlab.com/

## Getting Started

Add this snippet to yout build script.
Add this snippet to your build script.

```
plugins {
Expand All @@ -33,16 +33,32 @@ build-gradle:

## Configuration

Configuration for the default 'printCoverage' task:
```
printcoverage {
coverageType = 'INSTRUCTION'
reportFile = "${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml"
message = 'Coverage: %s%%'
}
```

* `coverageType`: Type of [coverage metric](http://www.eclemma.org/jacoco/trunk/doc/counters.html) to be printed.<br>
One of 'INSTRUCTION', 'BRANCH', 'LINE', 'COMPLEXITY', 'METHOD' or 'CLASS'<br>
Default: 'INSTRUCTION'
* `reportFile`: Path to the Jacoco xml-report to be used
Default: "${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml"
* `message`: Format string to be used for printing the coverage information
Default: 'Coverage: %s%%'

To print coverage information for different reports, define custom tasks:
```
task printCombinedCoverage(type: PrintCoverage) {
reportFile = "$buildDir/reports/jacoco/combinedCoverageReport/combinedCoverageReport.xml"
message = 'Total Coverage: %s%%'
}
```


## Publishing Workflow

Every commit on this repository gets tested via [circleci](https://circleci.com/gh/jansauer/gradle-print-coverage-plugin).
Expand Down Expand Up @@ -78,4 +94,4 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import org.gradle.api.provider.Property
class PrintCoverageExtension {

final Property<String> coverageType
final Property<String> reportFile
final Property<String> message

PrintCoverageExtension(Project project) {
coverageType = project.objects.property(String)
coverageType.set('INSTRUCTION')
reportFile = project.objects.property(String)
message = project.objects.property(String)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ class PrintCoveragePlugin implements Plugin<Project> {
}

def extension = target.extensions.create('printcoverage', PrintCoverageExtension, target)

Task task = target.tasks.create('printCoverage', PrintCoverageTask) {
coverageType = extension.coverageType
reportFile = extension.reportFile
message = extension.message
}

target.ext.PrintCoverage = PrintCoverageTask

task.dependsOn(target.tasks.withType(JacocoReport))
}
}
22 changes: 18 additions & 4 deletions src/main/groovy/de/jansauer/printcoverage/PrintCoverageTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction

class PrintCoverageTask extends DefaultTask {

@Input
@Optional
final Property<String> coverageType = project.objects.property(String)

@Input
@Optional
final Property<String> reportFile = project.objects.property(String)

@Input
@Optional
final Property<String> message = project.objects.property(String)

PrintCoverageTask() {
setDescription('Prints code coverage for gitlab.')
setGroup('coverage')
Expand All @@ -22,16 +32,20 @@ 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")
File jacocoTestReport = new File(reportFile.getOrElse("${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml"))
if (!jacocoTestReport.exists()) {
logger.error('Jacoco test report is missing.')
throw new GradleException('Jacoco test report is missing.')
}

def finalCoverageType = coverageType.getOrElse('INSTRUCTION')
def finalMessage = message.getOrElse('Coverage: %s%%')
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()

double missed = report.counter.find { it.'@type' == finalCoverageType }.@missed.toDouble()
double covered = report.counter.find { it.'@type' == finalCoverageType }.@covered.toDouble()
def coverage = (100 / (missed + covered) * covered).round(2)
println 'Coverage: ' + coverage + '%'

println String.format(finalMessage, coverage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class PrintCoveragePluginTest extends Specification {

File reportFile

File customReportFile

def setup() {
temporaryFolder = new TemporaryFolder()
temporaryFolder.create()
Expand All @@ -30,11 +32,16 @@ class PrintCoveragePluginTest extends Specification {
org.gradle.jvmargs=-Xmx512m -Xms256m
"""
buildFile = temporaryFolder.newFile('build.gradle')
reportFile = temporaryFolder
.newFolder('build', 'reports', 'jacoco', 'test')
def buildDir = temporaryFolder
.newFolder('build', 'reports', 'jacoco', 'test')
reportFile = buildDir
.toPath()
.resolve('jacocoTestReport.xml')
.toFile()
customReportFile = buildDir
.toPath()
.resolve('customTestReport.xml')
.toFile()
}

def "should fail if the jacoco plugin is missing"() {
Expand Down Expand Up @@ -246,4 +253,96 @@ class PrintCoveragePluginTest extends Specification {
'METHOD' | '66.67%'
'CLASS' | '100.0%'
}

def "should use configured message" () {
given:
buildFile << """
plugins {
id 'jacoco'
id 'de.jansauer.printcoverage'
}
printcoverage {
message = 'Custom message: %s%%'
}
"""
reportFile << new File("src/test/resources/jacocoTestReport.xml").text

when:
def result = GradleRunner.create()
.withGradleVersion(gradleVersion)
.withProjectDir(temporaryFolder.root)
.withArguments('printCoverage')
.withPluginClasspath()
.build()

then:
result.output.contains('Custom message: 3.13%')
result.task(":printCoverage").outcome == SUCCESS

where:
gradleVersion << SUPPORTED_GRADLE_VERSIONS
}

def "should use configured report-path" () {
given:
buildFile << """
plugins {
id 'jacoco'
id 'de.jansauer.printcoverage'
}
printcoverage {
reportFile = "\$buildDir/reports/jacoco/test/customTestReport.xml".toString()
}
"""
customReportFile << new File("src/test/resources/jacocoTestReport.xml").text

when:
def result = GradleRunner.create()
.withGradleVersion(gradleVersion)
.withProjectDir(temporaryFolder.root)
.withArguments('printCoverage')
.withPluginClasspath()
.build()

then:
result.output.contains('Coverage: 3.13%')
result.task(":printCoverage").outcome == SUCCESS

where:
gradleVersion << SUPPORTED_GRADLE_VERSIONS
}

def "should allow custom task definitions" () {
given:
buildFile << """
plugins {
id 'jacoco'
id 'de.jansauer.printcoverage'
}
task printCustomCoverage(type: PrintCoverage) {
reportFile = "\$buildDir/reports/jacoco/test/customTestReport.xml".toString()
message = 'Custom coverage: %s%%'
}
"""
customReportFile << new File("src/test/resources/jacocoTestReport.xml").text

when:
def result = GradleRunner.create()
.withGradleVersion(gradleVersion)
.withProjectDir(temporaryFolder.root)
.withArguments('printCustomCoverage')
.withPluginClasspath()
.build()

then:
result.output.contains('Custom coverage: 3.13%')
result.task(":printCustomCoverage").outcome == SUCCESS

where:
gradleVersion << SUPPORTED_GRADLE_VERSIONS
}

}

0 comments on commit 97e94b9

Please sign in to comment.