Kotlin / kotlinx-kover

Apache License 2.0
1.37k stars 53 forks source link

Using custom verification rules for different sets of classes in `v0.8.0` #609

Closed jmartinesp closed 6 months ago

jmartinesp commented 6 months ago

Describe what you would like to clarify about Kover

In our project we have sets of rules which apply to certain types of classes, like:

// Rule to ensure that coverage of Presenters is sufficient.
rule("Check code coverage of presenters") {
    isEnabled = true
    entity = kotlinx.kover.gradle.plugin.dsl.GroupingEntityType.CLASS
    filters {
        includes {
            classes(
                "*Presenter",
            )
        }
    }
    bound {
        minValue = 85
        metric = MetricType.INSTRUCTION
        aggregation = AggregationType.COVERED_PERCENTAGE
    }
}

We have other for Compose views and similar certain classes where we want to be more or less strict about coverage than in the total coverage verification.

However, on v0.8.0 overriding filters for rules is deprecated, so I'm not sure if it's possible to have these checks anymore. If it is, what's the new way to define these? And if it's not, then what's the point of having named rules if you can't apply them to different sources?

sandwwraith commented 6 months ago

You have to create a new report variant and apply filters and rules in it. See here: https://github.com/Kotlin/kotlinx-kover/blob/v0.8.0/docs/gradle-plugin/migrations/migration-to-0.8.0.md#the-format-specific-reports-filters-has-been-removed

So configuration will look like this:

kover {
    currentProject {
        createVariant("myScope") {
            add("jvm")
        }
    }

    reports {
        variant("myScope") {
            filters {
                // your filters
            }
            verify {
               // your rules
            }
        }
    }
}
jmartinesp commented 6 months ago

Thanks, then I was on the right track, then I guess it doesn't make sense to have several rules for a variant.

I think I understand the 'right' way to use it now, but then I have another question. At the moment we're only testing our gplayDebug Android variant which in turn uses all the rules.

With the new changes, does this mean we'll have to use:

./gradlew :app:verifyGplayDebug // Current one
./gradlew :app:verifyPresenters // From a rule
./gradlew :app:verifyViews // From another rule
// etc.

Separately, for each variant created from a rule? Or is there some way to test only the gplayDebug Android variant and all the other newly defined ones? Some way to create a parent one that also runs the child ones?

sandwwraith commented 6 months ago

You can declare a Gradle task that depends on all the verify* tasks that you want to run, that should do the trick.

jmartinesp commented 6 months ago

I was so focused in finding a solution for this in Kover that I forgot I could actually handle this in Gradle 😅 .

I think that answers all my questions, thanks again!