Skip to content

Instantly share code, notes, and snippets.

@zerotacg
Last active December 13, 2022 23:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerotacg/a131d73bf3f85c5e0309762ac8bbdf06 to your computer and use it in GitHub Desktop.
Save zerotacg/a131d73bf3f85c5e0309762ac8bbdf06 to your computer and use it in GitHub Desktop.
print coverage stats for usage in gitlab coverage
import groovy.xml.XmlParser
import java.text.DecimalFormat
tasks.register('koverStats') {
dependsOn ':koverXmlReport'
def percentageFormat = new DecimalFormat("#.##%")
ext.srcFile = new File(buildDir, 'reports/kover/xml/report.xml')
inputs.file srcFile
doLast {
println "Coverage Stats."
def report = new XmlParser().parse(srcFile)
report.counter.each { counter ->
def type = counter.@type
def missed = Integer.parseInt(counter.@missed)
def covered = Integer.parseInt(counter.@covered)
def total = missed + covered
def percentage = covered / total
println "Type: $type missed: $missed covered: $covered percentage: ${percentageFormat.format(percentage)}"
}
}
}
@kirkaDev
Copy link

kirkaDev commented Sep 14, 2022

Hello, this snippet looks like solution may help me (I went from here) until collaborators from Kover add own task.

But I can't run this snippet :(

I see message "Cannot resolve symbol 'XmlParser'". and I don't know .
Tell me, please, what should I import to Gradle-file (using Groovy). Thank you.

@zerotacg
Copy link
Author

Thx for your remark I added the missing imports to the snippet

@kirkaDev
Copy link

Thx for your remark I added the missing imports to the snippet

thank you for your snippet :)
I can't import this, may be I should implement some repository for import...

@zerotacg
Copy link
Author

If the XmlParser is not available you could try and implement it by just reading it as plain text and find the coresponding line with an regex as in the kotlin version of the task

Kotlin/kotlinx-kover#155 (comment)

@kirkaDev
Copy link

I tried to convert this to the Groovy, but had a fail. Maybe, will more simple to convert my .gradle file to gradle.kts. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment