Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Jacoco - coverage reports includes classes excluded in configuration

Tags:

gradle

jacoco

I added to a project a set of sources that don't have tests and I don't want them spoil my test coverage statistics. I configured Jacoco in the next way :

test {
    jacoco{
        excludes = ['org/bla/**']
        includes = ['com/bla/**']
        append = false
    }
}

jacocoTestReport {
    dependsOn test
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled true
        html.enabled true
    }
    classDirectories = fileTree(dir: 'build/classes/main', include: 'com/bla/**')
    sourceDirectories = fileTree(dir: 'scr/main/java', include: 'com/bla/**')
}

But anyway, when generating the report, Jacoco also includes classes from org.bla

Can someone help me ?

EDIT

After some debugging, it appears that all default outputs are added to org.gradle.testing.jacoco.tasks.JacocoReport#classDirectories in a private method org.gradle.testing.jacoco.plugins.JacocoPlugin#addDefaultReportTasks

That's visible when using such code :

jacocoTestReport {
    classDirectories = files('build/classes/main/com/bla')
    println("-----------------------------------------------------------")
    getAllClassDirs().each { File file ->
        println(file.absolutePath)
    }
    println("-----------------------------------------------------------")
    getAdditionalClassDirs().each{ File file ->
        println(file.absolutePath)
    }
}

jacocoTestReport << {
    println("-----------------------------------------------------------")
    getAllClassDirs().each { File file ->
        println(file.absolutePath)
    }
    println("-----------------------------------------------------------")
    getAdditionalClassDirs().each{ File file ->
        println(file.absolutePath)
    }
}

Output
-----------------------------------------------------------
<path_here>\build\classes\main\com\bla
-----------------------------------------------------------
....more text here
-----------------------------------------------------------
<path_here>\build\classes\main\com\bla
<path_here>\build\classes\main
<path_here>\build\resources\main
-----------------------------------------------------------

So - the question is : is it possible to override somehow org.gradle.testing.jacoco.plugins.JacocoPlugin#addDefaultReportTasks method, or override completely org.gradle.testing.jacoco.plugins.JacocoPlugin class ?

like image 394
StKiller Avatar asked Jun 30 '13 20:06

StKiller


1 Answers

Ok, found a workaround :

jacocoTestReport.doFirst{
    classDirectories = files('build/classes/main/com/bla')
}

This overrides classDirectories set by JacocoPlugin class.

like image 181
StKiller Avatar answered Oct 21 '22 21:10

StKiller