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 846870a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 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: PrintCoverageTask) {
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,8 +15,11 @@ 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
}
task.dependsOn(target.tasks.withType(JacocoReport))
}
Expand Down
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)
}
}

0 comments on commit 846870a

Please sign in to comment.