Skip to content

Commit 846870a

Browse files
committed
Add configuration options for report-path and message
1 parent 7605f2f commit 846870a

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ For more information see [Gitalb test coverage parsing](https://docs.gitlab.com/
88

99
## Getting Started
1010

11-
Add this snippet to yout build script.
11+
Add this snippet to your build script.
1212

1313
```
1414
plugins {
@@ -33,16 +33,32 @@ build-gradle:
3333

3434
## Configuration
3535

36+
Configuration for the default 'printCoverage' task:
3637
```
3738
printcoverage {
3839
coverageType = 'INSTRUCTION'
40+
reportFile = "${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml"
41+
message = 'Coverage: %s%%'
3942
}
4043
```
4144

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

53+
To print coverage information for different reports, define custom tasks:
54+
```
55+
task printCombinedCoverage(type: PrintCoverageTask) {
56+
reportFile = "$buildDir/reports/jacoco/combinedCoverageReport/combinedCoverageReport.xml"
57+
message = 'Total Coverage: %s%%'
58+
}
59+
```
60+
61+
4662
## Publishing Workflow
4763

4864
Every commit on this repository gets tested via [circleci](https://circleci.com/gh/jansauer/gradle-print-coverage-plugin).
@@ -78,4 +94,4 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
7894
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
7995
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
8096
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
81-
```
97+
```

src/main/groovy/de/jansauer/printcoverage/PrintCoverageExtension.groovy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import org.gradle.api.provider.Property
66
class PrintCoverageExtension {
77

88
final Property<String> coverageType
9+
final Property<String> reportFile
10+
final Property<String> message
911

1012
PrintCoverageExtension(Project project) {
1113
coverageType = project.objects.property(String)
12-
coverageType.set('INSTRUCTION')
14+
reportFile = project.objects.property(String)
15+
message = project.objects.property(String)
1316
}
1417
}

src/main/groovy/de/jansauer/printcoverage/PrintCoveragePlugin.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ class PrintCoveragePlugin implements Plugin<Project> {
1515
}
1616

1717
def extension = target.extensions.create('printcoverage', PrintCoverageExtension, target)
18+
1819
Task task = target.tasks.create('printCoverage', PrintCoverageTask) {
1920
coverageType = extension.coverageType
21+
reportFile = extension.reportFile
22+
message = extension.message
2023
}
2124
task.dependsOn(target.tasks.withType(JacocoReport))
2225
}

src/main/groovy/de/jansauer/printcoverage/PrintCoverageTask.groovy

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@ import org.gradle.api.DefaultTask
44
import org.gradle.api.GradleException
55
import org.gradle.api.provider.Property
66
import org.gradle.api.tasks.Input
7+
import org.gradle.api.tasks.Optional
78
import org.gradle.api.tasks.TaskAction
89

910
class PrintCoverageTask extends DefaultTask {
1011

1112
@Input
13+
@Optional
1214
final Property<String> coverageType = project.objects.property(String)
1315

16+
@Input
17+
@Optional
18+
final Property<String> reportFile = project.objects.property(String)
19+
20+
@Input
21+
@Optional
22+
final Property<String> message = project.objects.property(String)
23+
1424
PrintCoverageTask() {
1525
setDescription('Prints code coverage for gitlab.')
1626
setGroup('coverage')
@@ -22,16 +32,20 @@ class PrintCoverageTask extends DefaultTask {
2232
slurper.setFeature('http://apache.org/xml/features/disallow-doctype-decl', false)
2333
slurper.setFeature('http://apache.org/xml/features/nonvalidating/load-external-dtd', false)
2434

25-
File jacocoTestReport = new File("${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml")
35+
File jacocoTestReport = new File(reportFile.getOrElse("${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml"))
2636
if (!jacocoTestReport.exists()) {
2737
logger.error('Jacoco test report is missing.')
2838
throw new GradleException('Jacoco test report is missing.')
2939
}
3040

41+
def finalCoverageType = coverageType.getOrElse('INSTRUCTION')
42+
def finalMessage = message.getOrElse('Coverage: %s%%')
3143
def report = slurper.parse(jacocoTestReport)
32-
double missed = report.counter.find { it.'@type' == coverageType.get() }.@missed.toDouble()
33-
double covered = report.counter.find { it.'@type' == coverageType.get() }.@covered.toDouble()
44+
45+
double missed = report.counter.find { it.'@type' == finalCoverageType }.@missed.toDouble()
46+
double covered = report.counter.find { it.'@type' == finalCoverageType }.@covered.toDouble()
3447
def coverage = (100 / (missed + covered) * covered).round(2)
35-
println 'Coverage: ' + coverage + '%'
48+
49+
println String.format(finalMessage, coverage)
3650
}
3751
}

0 commit comments

Comments
 (0)