-
Notifications
You must be signed in to change notification settings - Fork 52
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
Added ability to specify inclusion filter for source sets #726
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright 2017-2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.kover.gradle.plugin.test.functional.cases | ||
|
||
import kotlinx.kover.gradle.plugin.test.functional.framework.checker.createCheckerContext | ||
import kotlinx.kover.gradle.plugin.test.functional.framework.runner.buildFromTemplate | ||
import kotlinx.kover.gradle.plugin.test.functional.framework.runner.runWithParams | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertTrue | ||
|
||
internal class SourceSetsTests { | ||
|
||
@Test | ||
fun testExclude() { | ||
val template = buildFromTemplate("sourcesets-multi") | ||
|
||
template.kover { | ||
currentProject { | ||
sources.excludedSourceSets.addAll("main", "foo") | ||
} | ||
} | ||
|
||
val gradleBuild = template.generate() | ||
val buildResult = gradleBuild.runWithParams(":koverXmlReport") | ||
|
||
gradleBuild.createCheckerContext(buildResult).xmlReport { | ||
assertTrue(buildResult.isSuccessful, "Build should be successful") | ||
classCounter("kotlinx.kover.examples.sourcesets.MainClass").assertAbsent() | ||
classCounter("kotlinx.kover.examples.sourcesets.FooClass").assertAbsent() | ||
classCounter("kotlinx.kover.examples.sourcesets.ExtraClass").assertPresent() | ||
} | ||
} | ||
|
||
@Test | ||
fun testInclude() { | ||
val template = buildFromTemplate("sourcesets-multi") | ||
|
||
template.kover { | ||
currentProject { | ||
sources.includedSourceSets.addAll("extra") | ||
} | ||
} | ||
|
||
val gradleBuild = template.generate() | ||
val buildResult = gradleBuild.runWithParams(":koverXmlReport") | ||
|
||
gradleBuild.createCheckerContext(buildResult).xmlReport { | ||
assertTrue(buildResult.isSuccessful, "Build should be successful") | ||
classCounter("kotlinx.kover.examples.sourcesets.MainClass").assertAbsent() | ||
classCounter("kotlinx.kover.examples.sourcesets.FooClass").assertAbsent() | ||
classCounter("kotlinx.kover.examples.sourcesets.ExtraClass").assertPresent() | ||
} | ||
} | ||
|
||
@Test | ||
fun testInclude2() { | ||
val template = buildFromTemplate("sourcesets-multi") | ||
|
||
template.kover { | ||
currentProject { | ||
sources.includedSourceSets.addAll("extra", "foo") | ||
} | ||
} | ||
|
||
val gradleBuild = template.generate() | ||
val buildResult = gradleBuild.runWithParams(":koverXmlReport") | ||
|
||
gradleBuild.createCheckerContext(buildResult).xmlReport { | ||
assertTrue(buildResult.isSuccessful, "Build should be successful") | ||
classCounter("kotlinx.kover.examples.sourcesets.MainClass").assertAbsent() | ||
classCounter("kotlinx.kover.examples.sourcesets.FooClass").assertPresent() | ||
classCounter("kotlinx.kover.examples.sourcesets.ExtraClass").assertPresent() | ||
} | ||
} | ||
|
||
@Test | ||
fun testMixed() { | ||
val template = buildFromTemplate("sourcesets-multi") | ||
|
||
template.kover { | ||
currentProject { | ||
sources { | ||
includedSourceSets.addAll("extra", "foo") | ||
excludedSourceSets.add("foo") | ||
} | ||
} | ||
} | ||
|
||
val gradleBuild = template.generate() | ||
val buildResult = gradleBuild.runWithParams(":koverXmlReport") | ||
|
||
gradleBuild.createCheckerContext(buildResult).xmlReport { | ||
assertTrue(buildResult.isSuccessful, "Build should be successful") | ||
classCounter("kotlinx.kover.examples.sourcesets.MainClass").assertAbsent() | ||
classCounter("kotlinx.kover.examples.sourcesets.FooClass").assertAbsent() | ||
classCounter("kotlinx.kover.examples.sourcesets.ExtraClass").assertPresent() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
} | ||
} | ||
|
||
@Test | ||
fun testTestSourceSet() { | ||
val template = buildFromTemplate("sourcesets-multi") | ||
|
||
template.kover { | ||
currentProject { | ||
sources { | ||
includedSourceSets.addAll("test") | ||
} | ||
} | ||
} | ||
|
||
val gradleBuild = template.generate() | ||
val buildResult = gradleBuild.runWithParams(":koverXmlReport") | ||
|
||
gradleBuild.createCheckerContext(buildResult).xmlReport { | ||
assertTrue(buildResult.isSuccessful, "Build should be successful") | ||
classCounter("kotlinx.kover.examples.sourcesets.MainClass").assertAbsent() | ||
classCounter("kotlinx.kover.examples.sourcesets.FooClass").assertAbsent() | ||
classCounter("kotlinx.kover.examples.sourcesets.ExtraClass").assertAbsent() | ||
classCounter("kotlinx.kover.examples.sourcesets.TestClasses").assertPresent() | ||
} | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,3 +183,17 @@ internal fun File.patchKoverDependency(koverVersion: String) { | |
} | ||
} | ||
} | ||
|
||
internal fun File.addKoverBlocks(koverBlocks: MutableList<(ScriptLanguage, String) -> String>) { | ||
if (koverBlocks.isEmpty()) return | ||
|
||
val language = if (name.endsWith(".kts")) ScriptLanguage.KTS else ScriptLanguage.GROOVY | ||
|
||
val builder = StringBuilder() | ||
koverBlocks.forEach { block -> | ||
builder.appendLine() | ||
builder.append(block(language, "8.12")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is 8.12 a Gradle version? Shouldn't it be some constant? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, any version of Gradle with support for assign operator redefinition is needed here. |
||
builder.appendLine() | ||
} | ||
appendText(builder.toString()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
plugins { | ||
kotlin("jvm") version "2.1.0" | ||
id("org.jetbrains.kotlinx.kover") version "0.7.0" | ||
} | ||
|
||
sourceSets.create("extra") | ||
sourceSets.create("foo") | ||
|
||
dependencies { | ||
testImplementation(kotlin("test")) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
pluginManagement { | ||
repositories { | ||
gradlePluginPortal() | ||
mavenCentral() | ||
} | ||
} | ||
rootProject.name = "example-source-multi" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package kotlinx.kover.examples.sourcesets | ||
|
||
class ExtraClass { | ||
fun function() { | ||
println("Example") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package kotlinx.kover.examples.sourcesets | ||
|
||
class FooClass { | ||
fun function() { | ||
println("Example") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package kotlinx.kover.examples.sourcesets | ||
|
||
class MainClass { | ||
fun function() { | ||
println("Example") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package kotlinx.kover.examples.sourcesets | ||
|
||
import kotlin.test.Test | ||
|
||
class TestClasses { | ||
@Test | ||
fun test() { | ||
// no-tests | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import kotlinx.kover.gradle.plugin.commons.VariantOriginAttr | |
import kotlinx.kover.gradle.plugin.dsl.internal.KoverVariantConfigImpl | ||
import kotlinx.kover.gradle.plugin.appliers.origin.JvmVariantOrigin | ||
import kotlinx.kover.gradle.plugin.commons.JVM_VARIANT_NAME | ||
import kotlinx.kover.gradle.plugin.dsl.KoverVariantSources | ||
import kotlinx.kover.gradle.plugin.dsl.internal.KoverProjectExtensionImpl | ||
import kotlinx.kover.gradle.plugin.tools.CoverageTool | ||
import org.gradle.api.Project | ||
|
@@ -48,9 +49,25 @@ internal class JvmVariantArtifacts( | |
} | ||
|
||
fromOrigin(variantOrigin) { compilationName -> | ||
compilationName !in variantConfig.sources.excludedSourceSets.get() | ||
&& compilationName != "test" | ||
compilationIsExcluded(compilationName, variantConfig.sources) | ||
} | ||
} | ||
|
||
private fun compilationIsExcluded(compilationName: String, variant: KoverVariantSources): Boolean { | ||
if (compilationName in variant.excludedSourceSets.get()) { | ||
return false | ||
} | ||
|
||
val included = variant.includedSourceSets.get() | ||
|
||
if (included.isEmpty() && compilationName == "test") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are tests excluded by default? Is it mentioned somewhere in the documentation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, |
||
return false | ||
} | ||
if (included.isNotEmpty() && compilationName !in included) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this
includedSourceSets
then?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or at least
excludeEverySourceSetExcept
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to get away from naming
included
. On the other hand, such things are now calledincluded
in the entire DSL.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, let it be
included
for nowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid we can move from this naming only for all functions at once, when we'll migrate whole Kover plugin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I took this into account