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

Display Coverage % in Gitlab #155

Closed
ybentlili opened this issue Mar 23, 2022 · 5 comments
Closed

Display Coverage % in Gitlab #155

ybentlili opened this issue Mar 23, 2022 · 5 comments

Comments

@ybentlili
Copy link

ybentlili commented Mar 23, 2022

Hello
I want to display the % of coverage in Gitlab.
I can get it by
grep -Eo "[0-9]+([.][0-9]+)%" path/to/report//index.html | head -1

But I don't know to display it in Gitlab as Coverage must be regular expression.

Thanks

@sarn0ld
Copy link

sarn0ld commented May 26, 2022

In the script tag block of your job (Inside .gitlab-ci.yml) you can add one line after executing kover:
- cat path/to/report/index.html

Then you can add the regex in a coverage tagcoverage tag for the same job.

@CLOVIS-AI
Copy link

CLOVIS-AI commented Jul 3, 2022

Until Kover provides an easy integration, here is particularly fragile way of doing this:

values=$(cat build/reports/kover/project-xml/report.xml | tail -7 | head -5 | grep 'INSTRUCTION' | cut -d ' ' -f 3,4 | sed 's/missed="//;s/" covered="/ /;s/"\/>//')
missed=$(<<<$values cut -d ' ' -f 1)
covered=$(<<<$values cut -d ' ' -f 2)
echo "Coverage $(($covered * 100 / ($covered + $missed)))%"

A simple way for Kover to make all of this unnecessary would be to provide a Gradle task similar to this:

$ ./gradlew koverStats
Instruction: 12%
Branch: 32%
Line: 15%
Method: 35%
Class: 80%

This command would also be useful for CLI users.

@zerotacg
Copy link

I liked the idea of CLOVIS-AI and integrated it as a gradle task

Still a bit hacky gradle task, maybe someone could improve on that

After that I'm using this gitlab coverage config
coverage: '/Type: INSTRUCTION .*?([\d\.]+)%/'

@stepango
Copy link

stepango commented Sep 8, 2022

In case anyone interested in a Kotlin version of gradle task above(#229)

tasks.register("koverPrintMergedXmlCoverage") {
    val koverMergedXmlReport = tasks.named("koverMergedXmlReport")
    dependsOn(koverMergedXmlReport)
    doLast {
        //language=RegExp
        val regexp = """<counter type="INSTRUCTION" missed="(\d+)" covered="(\d+)"/>""".toRegex()
        koverMergedXmlReport.get().outputs.files.forEach { file ->
            // Read file by lines
            file.useLines { lines ->
                // Last line in file that matches regexp is the total coverage
                lines.last(regexp::containsMatchIn).let { line ->
                    // Found the match
                    regexp.find(line)?.let {
                        val missed = it.groupValues[1].toInt()
                        val covered = it.groupValues[2].toInt()
                        println("Total Code Coverage: ${covered * 100 / (missed + covered)}%")
                    }
                }
            }
        }
    }
}

@shanshin
Copy link
Collaborator

Duplicates #229

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants