Kotlin / kotlinx-kover

Apache License 2.0
1.3k stars 48 forks source link

Difficulty merging/running different types of sub projects/modules after upgrading to Kover 0.7.x #429

Closed JWvanV closed 11 months ago

JWvanV commented 12 months ago

Hi,

I'm having difficulty generating a single coverage report for the entire project. We had this working fine for Kover 0.6.x, but we can not seem to get it to work with the current documentation for Kover 0.7.x.

I have a project setup with the following modules and build variants:

Note here that

All gradle build files are in build.gradle.kts Kotlin Scripts.

For consistency, we define kover in once in the root build.gradle.kts file:

dependencies {
    kover(project(":app"))
    forEachModuleLayer { kover(project(it)) }
}

allprojects {
    apply(plugin = Plugin.KotlinX.kover.id)

    koverReport {
        filters {
            excludes {
                classes(
                    // Build
                    "*.BuildConfig",

                    // Design and Android
                    "*.components.*",
                    "*.databinding.*",
                    "*ViewHolder", // Kiel view holders
                    "*GraphAppDirections\$*", // Generated by navigation library
                    "*GraphAppDirections", // Generated by navigation library
                    "*FragmentDirections\$*", // Generated by navigation library
                    "*FragmentDirections", // Generated by navigation library
                    "*FragmentArgs\$*", // Generated by navigation library
                    "*FragmentArgs", // Generated by navigation library
                    "*Fragment\$*",
                    "*Activity\$*",
                    "*App\$*",

                    // Dagger Hilt
                    "*.di.*Module",
                    "*.Hilt_*",
                    "*_HiltModules*",
                    "*_Impl*",
                    "*_Provide*",
                    "*_Factory*",
                    "*_AssistedFactory_Impl*",
                    "*_MembersInjector*",
                    "*_GeneratedInjector",
                    "dagger.hilt.internal.aggregatedroot.codegen.*",
                    "hilt_aggregated_deps.*",

                    // App excludes
                    // Core excludes
                    // FeatureX excludes
                )

                packages(
                    "com.squareup.moshi.adapters",
                    "com.github.ybq.android.spinkit"
                )

                annotatedBy(
                    "dagger.Module", // Binders and Providers
                    "dagger.hilt.EntryPoint", // Entry points
                    "dagger.hilt.android.AndroidEntryPoint", // Activities and fragments
                    "dagger.internal.DaggerGenerated", // Dagger generated
                    "com.squareup.moshi.JsonClass" // Network entities
                )
            }
        }

        defaults {
            xml {
                // Generate an XML report when running the `check` task
                onCheck = false
                // XML report file
                setReportFile(layout.buildDirectory.file("artifacts/reports/kover/coverageResults.xml"))
            }
            html {
                // Custom header in HTML reports, project path by default
                title = "Test coverage"
                //  generate a HTML report when running the `check` task
                onCheck = true
                // Directory for HTML report
                setReportDir(layout.buildDirectory.dir("artifacts/reports/kover/coverageResults"))
            }
            verify {
                // Verify coverage when running the `check` task
                onCheck = true

                rule {
                    isEnabled = false
                    entity = GroupingEntityType.APPLICATION
                    bound {
                        minValue = 80
                        maxValue = 100
                        metric = MetricType.INSTRUCTION
                        aggregation = AggregationType.COVERED_PERCENTAGE
                    }
                }
            }
        }
    }
}

We tried running all the generated koverHtmlReport, koverHtmlReportRelease and koverHtmlReportTstRelease tasks, but they do not seem to give the wanted result.

Are we going about this the wrong way? If there are any pointers to get this to work it would be greatly appreciated :D

shanshin commented 12 months ago

Hi, If your Gradle build uses mixed K/JVM and Android plugins in different projects and you want to generate a report using the koverHtmlReport command, then it is necessary in the project in which the report will be generated to specify the coverage of which build variants should be added to the merged report. See docs and examples like this

koverReport {
    defaults {
        // adds the coverage of `release` build variant to default reports
        mergeWith("release")
    }
}

Also, koverReport block must be specified in one project in which the merged report will be generated.

JWvanV commented 11 months ago

Sorry for the late response, (but still thanks for your quick initial response :D):

We now do have an issue in our pipeline though. Version 0.7 (or our configuration of it) seems to require way more memory than we needed on 0.6, generating java heap exceptions before the final html report is generated. (it's quite a big project).

Do you have any advice on reducing memory use in the new 0.7 version?

shanshin commented 11 months ago

We now do have an issue in our pipeline though. Version 0.7 (or our configuration of it) seems to require way more memory than we needed on 0.6, generating java heap exceptions before the final html report is generated. (it's quite a big project).

Could you clarify what is the approximate increase in memory consumption and on which Gradle task exactly is happening?

If it is not possible to increase the java heap size, you may try using the build cache to perform tests in stages (Gradle build for several subprojects), and generate a Kover report with a separate command.

JWvanV commented 11 months ago

Again, sorry for the late response. I also had to refactor our unit test suite so be more stable. After doing that first, and then applying the previously mentioned migration, Kover 0.7.3 runs great in our pipeline now :D The memory issue was probably because of the unstable test suite.

shanshin commented 11 months ago

Great, thanks!)