Hey everything , Recently i was trying to improve ...
# gradle
a
Hey everything , Recently i was trying to improve ci speed , so i saw dropbox's AffectedModuleDetector , for running only changed files , module UTs , but somehow it was not working for me , Then i got https://github.com/ismaeldivita/change-tracker-plugin , i modified this plugin according to my usecase , It's working fine independently , But i am not able to integrate this with jacoco. 🧵
Here is my jacoco file
Copy code
package plugins

plugins { jacoco }

configurations.all {
    resolutionStrategy {
        eachDependency {
            when (requested.group) {
                "org.jacoco" -> useVersion(Versions.BuildPlugins.jacoco)
            // ...etc
            }
        }
    }
}

val jacocoExceptionsExtension = project.extensions.getByType(JacocoExceptionsExtension::class.java)

apply {
    project.apply<DiffCoveragePlugin>()
    if (isAndroidModule()) {
        setupAndroidReporting()
    } else {
        setupKotlinReporting()
    }
}

fun isAndroidLibraryModule(): Boolean {
    return project.plugins.hasPlugin("com.android.library")
}

fun isAndroidModule(): Boolean {
    val isAndroidLibrary = project.plugins.hasPlugin("com.android.library")
    val isAndroidApp = project.plugins.hasPlugin("com.android.application")
    return isAndroidLibrary || isAndroidApp
}

fun setupKotlinReporting() {
    registerTasks("test")
}

fun setupAndroidReporting() {
    tasks.withType<Test> {
        configure<JacocoTaskExtension> {
            isIncludeNoLocationClasses = true
            ignoreFailures = false
            excludes = listOf("jdk.internal.*")
        }
    }

    registerTasks("testDebugUnitTest", isAndroidLibraryModule())
}

fun registerTasks(dependsOnTask: String, isAndroidLibrary: Boolean = false) {
    /** Jacoco command to generate report of unit test coverage */
    if (tasks.findByName("jacocoAndroidTestReport") == null) {

        tasks.register<JacocoReport>("jacocoAndroidTestReport") {
            enabled = true
            group = "verification"
            description = "Code coverage report for both Android and Unit tests."
            setDirectories(jacocoExceptionsExtension.fileFilters.orNull)
            dependsOn(dependsOnTask)

            // this is to fix a compile issue where for libs it is asking to declare this dependency
            if (isAndroidLibrary) {
                dependsOn("compileDebugLibraryResources")
            }
            reports { reports() }
            setDirectories(jacocoExceptionsExtension.fileFilters.orNull)
        }
    }

    if (tasks.findByName("jacocoAndroidCoverageVerification") == null) {
        tasks.register<JacocoCoverageVerification>("jacocoAndroidCoverageVerification") {
            enabled = true
            group = "verification"
            description = "Code coverage verification for Android both Android and Unit tests."
            dependsOn(dependsOnTask)
            violationRules {
                rule {
                    limit {
                        counter = "INSTRUCTIONAL"
                        value = "COVEREDRATIO"
                        minimum = "0.5".toBigDecimal()
                    }
                }
            }
            setDirectories(jacocoExceptionsExtension.fileFilters.orNull)
        }
    }
}

fun getClassDirectoriesTree(filters: Array<String>?): FileTree {
    return fileTree(project.buildDir) {
        include(JacocoConstants.classDirectories)

        exclude(getFileFilters(filters))
    }
}

fun getFileFilters(filters: Array<String>?): List<String> {
    return if (filters != null) {
        getDefaultFileFilter().toMutableList().apply { addAll(filters) }
    } else {
        getDefaultFileFilter()
    }
}

fun getDefaultFileFilter(): List<String> = JacocoConstants.fileFilter.toMutableList()

private val sourceDirectoriesTree =
    fileTree("${project.buildDir}") { include(JacocoConstants.sourceDirectories) }

private val executionDataTree =
    fileTree(project.buildDir) { include(JacocoConstants.executionDirectories) }

fun JacocoReportsContainer.reports() {
    xml.required.set(true)
    html.required.set(true)
    xml.outputLocation.set(file("${buildDir}/reports/jacoco/jacocoTestReport/jacocoTestReport.xml"))
    html.outputLocation.set(file("${buildDir}/reports/jacoco/jacocoTestReport/html"))
}

fun JacocoCoverageVerification.setDirectories(filters: Array<String>?) {
    sourceDirectories.setFrom(sourceDirectoriesTree)
    classDirectories.setFrom(getClassDirectoriesTree(filters))
    executionData.setFrom(executionDataTree)
}

fun JacocoReport.setDirectories(filters: Array<String>?) {
    sourceDirectories.setFrom(sourceDirectoriesTree)
    classDirectories.setFrom(getClassDirectoriesTree(filters))
    executionData.setFrom(executionDataTree)
}
Here i replaced testDebugUnitTest with the command that runs UTS of all changed file , module. provided in the plugin , I applied that plugin in build.gradle ( root level )
v
To start with, your question is off-topic here, if it is not Kotlin-related, please always consider server topics, channel names, and channel topics in open communities. ;-) Besides that, to improve CI speed, you should consider just using the build cache, then worthy tasks like compilation and test execution do not need to be executed if there was no change to inputs and outputs but the last result can simply be reused without the need to do any custom change-tracking or similar. Besides that, when you move your question to a more appropriate place, you should include some more information, especially some more focussed question. What do you try and what does not work exactly? Do you get some error somewhere? Or is something just not working as expected and if so, what do you expect and what happens instead? It is relatively unlikely that someone will analyze a 134 lines "snippet" for you to guess what your problem might be. 😉
👍 1