Skip to content

Add JacocoCoverageVerification task configuration #171

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

Open
mattinger opened this issue Apr 30, 2020 · 7 comments
Open

Add JacocoCoverageVerification task configuration #171

mattinger opened this issue Apr 30, 2020 · 7 comments

Comments

@mattinger
Copy link

mattinger commented Apr 30, 2020

It would be awesome if the plugin would also include configuration of the appropriate tasks of type JacocoCoverageVerification. Right now, i have to kind of hack around this by doing lazy creation of the task (due to the use of afterEvaluate to define the tasks in the plugin. I can then find the reporting task and copy the configuration from that into the verification task that gets created.

Obviously this is hokey, and would be better done in the plugin.

afterEvaluate {
        if (depth > 0 && !skippedProjects.contains(name)) {
            tasks.create("jacocoCoverageVerificationDebug") {
                doLast {
                    val reportTask =
                        tasks.findByName("jacocoTestReportDebug") as org.gradle.testing.jacoco.tasks.JacocoReport

                    val verify = tasks.findByName("__jacocoCoverageVerificationDebug") as? JacocoCoverageVerification
                        ?: tasks.create<JacocoCoverageVerification>("__jacocoCoverageVerificationDebug") {

                            executionData(reportTask.executionData)
                            sourceDirectories.setFrom(reportTask.sourceDirectories)
                            classDirectories.setFrom(reportTask.classDirectories)
                            additionalSourceDirs.setFrom(reportTask.additionalSourceDirs)
                            additionalClassDirs.setFrom(reportTask.additionalClassDirs)

                            violationRules {
                                rule {
                                    limit {
                                        counter = "LINE"
                                        minimum = "0.5".toBigDecimal()
                                    }
                                }
                                rule {
                                    limit {
                                        counter = "BRANCH"
                                        minimum = "0.4".toBigDecimal()
                                    }
                                }
                            }

                            setOnlyIf { true }
                        }

                    if (reportTask.executionData.find { it.exists() } != null) {
                        reportTask.generate()
                    }

                    if (reportTask.executionData.find { it.exists() } != null) {
                        verify.check()
                    }
                }
            }
        }
    }
@vanniktech
Copy link
Owner

Feel free to create a PR!

@mattinger
Copy link
Author

I can likely do that. I just have to get approval for submission (lawyers)

@vanniktech
Copy link
Owner

Sure!

@bittelc
Copy link

bittelc commented Sep 4, 2020

@braedongough could you please review

@bittelc
Copy link

bittelc commented Sep 4, 2020

@dominiquejb plz have braedongough review

@CyxouD
Copy link

CyxouD commented Sep 25, 2023

Hi @mattinger. I know it is pretty old, but did you have the opportunity to get approval?

@CyxouD
Copy link

CyxouD commented Sep 25, 2023

I couldn't make @mattinger solution work. Firstly, I had to delete 'val' keywords and 'as? ...' conversion since I use Groovy instead of Kotlin, after this it told me about missing property skippedProjects, I deleted it. After that, I changed to my product flavor. Eventually, it tells me that build is successful for this task, but it doesn't correctly check limits (it doesn't report errors).

To make it work, I used the solution from this root build gradle which is described in this video which eventually tells me Rule violated for bundle app: instructions covered ratio is 0.3, but expected minimum is 0.8 as expected.
The code in my case added to root's build.gradle. Note that I use my product flavor Mock in 3 places, so you need to change it to your flavor or delete it if your don't use flavors in your project

subprojects {
    afterEvaluate() {
        tasks.register('verifyCodeCoverageMockDebug', JacocoCoverageVerification) {
            dependsOn 'jacocoTestReportMockDebug'

            sourceDirectories.from = file("${project.projectDir}/src/main/java") // main source set
            classDirectories.from = files(JacocoUtil.getKotlinFileTree(project))
            executionData.from = fileTree(dir: project.buildDir, includes: [
                    '**/*.exec', // unit tests
                    '**/*.ec' // ui tests
            ])

            violationRules {
                rule {
                    limit {
                        minimum = 0.8 // 30%
                    }
                }
            }
        }
    }
}

class JacocoUtil {
    static ConfigurableFileTree getKotlinFileTree(Project project) {
        return project.fileTree(
                // Where generated Kotlin classes are located
                dir: "$project.buildDir/tmp/kotlin-classes/mockDebug",
                // Exclude everything that is not created by you, e.g. created by HILT
                excludes: [
                        '**/BuildConfig.*',
                        '**/*$*',
                        '**/Hilt_*.class',
                        'hilt_**',
                        'dagger/hilt/**',
                        '**/*JsonAdapter.*'
                ]
        )
    }
}

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

No branches or pull requests

4 participants