Kotlin / kotlinx-kover

Apache License 2.0
1.33k stars 52 forks source link

disableTestTask configures the task but should not #615

Open hfhbd opened 3 months ago

hfhbd commented 3 months ago

Describe the bug I use the jvm-test-suite plugin and add an integrationTest but I don't want to include the task/test results in kover.

Errors ./gradlew build =>

Could not determine the dependencies of task ':koverGenerateArtifactJvm'.
> Could not create task ':integrationTest'.
   > Cannot query the value of this provider because it has no value available.

Expected behavior ./gradlew build => no error

Reproducer

plugins {
    kotlin("jvm") version "2.0.0"
    id("jvm-test-suite")
    id("org.jetbrains.kotlinx.kover") version "0.8.0"
}

kotlin.jvmToolchain(21)

repositories {
    mavenCentral()
}

testing.suites {
    named("test", JvmTestSuite::class) {
        useKotlinTest()
    }
    register("integrationTest", JvmTestSuite::class) {
        useKotlinTest()
        targets.configureEach {
            testTask {
                environment("foo", providers.gradleProperty("NOT-SET").get())
            }
        }
    }
}

kover.currentProject {
    instrumentation {
        disabledForTestTasks.add("integrationTest")
    }
    sources {
        excludedSourceSets.add("integrationTest")
    }
}

Reports If applicable, report files or screenshots.

Environment

hfhbd commented 3 months ago

Alternatively, only depend on tasks.test ("unit-test" type when using jvm-test-suite plugin) by default and you need to opt-in to depend on other test tasks. This also allows you to remove some filtering hacks in kover:

kover.currentProject {
    sources {
      srcDirs("integrationTest") // same behavior like builtin sourceSets
    }
    instrumentation {
      include(tests.integrationTest)
     }
}
shanshin commented 3 months ago

Hi, this is because Kover Gradle Plugin use the matching function to locate test tasks but it is not lazy - it causes the creation of an instance of the task. For this reason, even if you exclude a task by name using matching, it is still created.

This can be circumvented by using the new named function, however it will only work in Gradle starting with version 8.6

hfhbd commented 3 months ago

But what about using opt-in of other tasks instead to not configure the tasks by default?

shanshin commented 3 months ago

But what about using opt-in of other tasks instead to not configure the tasks by default?

This can be done, but in this case it is necessary to significantly redesign the workflow for the plugin.

Since you are using Gradle version 8.7, we will implement usage of the new named function, and in this case the integrationTest task instance will not be created when writing

kover.currentProject {
    instrumentation {
        disabledForTestTasks.add("integrationTest")
    }
}
shanshin commented 3 months ago

Unfortunately, the named {} function in Gradle still contains a similar error, and it causes the creation of all tasks (see example). Therefore, at the moment it is difficult to write a search for test tasks by name without configuring all test tasks.

The use of named("name") is unreliable, because the task can be created after, and the use of TaskProviders requires significant changing of the DSL.

We are waiting for the fix to be released in Gradle.

hfhbd commented 1 month ago

I found a solution (for my use-case): I switched to the new settings plugin and everything works now without configuring the integration test tasks, nice!

But I have one question left: How do I setup rules in settings plugin?

shanshin commented 1 month ago

But I have one question left: How do I setup rules in settings plugin?

There is no verify task in settings plugin right now, but I think we will add it in the next release

shanshin commented 1 day ago

@hfhbd, verification was added in 0.9.0-RC