Skip to content

Commit 5db925e

Browse files
authored
Enable code coverage collection (#184)
1 parent 0bf5915 commit 5db925e

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

build.gradle.kts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import kotlinx.kover.gradle.plugin.dsl.MetricType
12
import kotlinx.validation.build.mavenCentralMetadata
23
import kotlinx.validation.build.mavenRepositoryPublishing
34
import kotlinx.validation.build.signPublicationIfKeyPresent
@@ -14,6 +15,7 @@ plugins {
1415
`maven-publish`
1516
`jvm-test-suite`
1617
id("org.jetbrains.kotlinx.binary-compatibility-validator")
18+
alias(libs.plugins.kover)
1719
}
1820

1921
group = "org.jetbrains.kotlinx"
@@ -107,6 +109,7 @@ tasks.compileTestKotlin {
107109
tasks.withType<Test>().configureEach {
108110
systemProperty("overwrite.output", System.getProperty("overwrite.output", "false"))
109111
systemProperty("testCasesClassesDirs", sourceSets.test.get().output.classesDirs.asPath)
112+
systemProperty("kover.enabled", project.findProperty("kover.enabled")?.toString().toBoolean())
110113
jvmArgs("-ea")
111114
}
112115

@@ -195,3 +198,25 @@ testing {
195198
tasks.withType<Sign>().configureEach {
196199
onlyIf("only sign if signatory is present") { signatory?.keyId != null }
197200
}
201+
202+
kover {
203+
koverReport {
204+
filters {
205+
excludes {
206+
packages("kotlinx.validation.test")
207+
}
208+
}
209+
verify {
210+
rule {
211+
minBound(80, MetricType.BRANCH)
212+
minBound(90, MetricType.LINE)
213+
}
214+
}
215+
}
216+
// Unfortunately, we can't test both configuration cache use and the test coverage
217+
// simultaneously, so the coverage collection should be enabled explicitly (and that
218+
// will disable configuration cache).
219+
if (!project.findProperty("kover.enabled")?.toString().toBoolean()) {
220+
disable()
221+
}
222+
}

gradle/libs.versions.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ gradlePlugin-pluginPublishing = { module = "com.gradle.publish:plugin-publish-pl
3939
gradlePlugin-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
4040
gradlePlugin-android = { module = "com.android.tools.build:gradle", version.ref = "androidGradlePlugin" }
4141
## endregion
42+
43+
[plugins]
44+
45+
kover = { id = "org.jetbrains.kotlinx.kover", version = "0.7.5" }

src/functionalTest/kotlin/kotlinx/validation/api/TestDsl.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import org.intellij.lang.annotations.Language
1212

1313
public val API_DIR: String = ApiValidationExtension().apiDumpDirectory
1414

15+
private val koverEnabled: Boolean = System.getProperty("kover.enabled").toBoolean()
16+
1517
internal fun BaseKotlinGradleTest.test(
1618
gradleVersion: String = "8.5",
1719
injectPluginClasspath: Boolean = true,
@@ -38,14 +40,19 @@ internal fun BaseKotlinGradleTest.test(
3840
.withPluginClasspath()
3941
.withArguments(baseKotlinScope.runner.arguments)
4042
.withGradleVersion(gradleVersion)
43+
44+
if (koverEnabled) {
45+
// In debug mode, tests will be running inside the same JVM.
46+
// That will allow collection coverage info by the Kover.
47+
runner.withDebug(true)
48+
}
49+
4150
if (injectPluginClasspath) {
4251
// The hack dating back to https://docs.gradle.org/6.0/userguide/test_kit.html#sub:test-kit-classpath-injection
4352
// Currently, some tests won't work without it because some classes are missing on the classpath.
4453
runner.addPluginTestRuntimeClasspath()
4554
}
4655
return runner
47-
// disabled because of: https://github.com/gradle/gradle/issues/6862
48-
// .withDebug(baseKotlinScope.runner.debug)
4956
}
5057

5158
/**
@@ -165,7 +172,13 @@ internal class AppendableScope(val filePath: String) {
165172
}
166173

167174
internal class Runner {
168-
val arguments: MutableList<String> = mutableListOf("--configuration-cache")
175+
val arguments: MutableList<String> = mutableListOf<String>().apply {
176+
if (!koverEnabled) {
177+
// Configuration cache is incompatible with javaagents being enabled for Gradle
178+
// See https://github.com/gradle/gradle/issues/25979
179+
add("--configuration-cache")
180+
}
181+
}
169182
}
170183

171184
internal fun readFileList(@Language("file-reference") fileName: String): String {

src/functionalTest/kotlin/kotlinx/validation/test/MixedMarkersTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MixedMarkersTest : BaseKotlinGradleTest() {
3131
}
3232
}
3333

34-
runner.withDebug(true).build().apply {
34+
runner.build().apply {
3535
assertTaskSuccess(":apiCheck")
3636
}
3737
}

src/functionalTest/kotlin/kotlinx/validation/test/PublicMarkersTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class PublicMarkersTest : BaseKotlinGradleTest() {
4141
}
4242
}
4343

44-
runner.withDebug(true).build().apply {
44+
runner.build().apply {
4545
assertTaskSuccess(":apiCheck")
4646
}
4747
}

0 commit comments

Comments
 (0)