From 8d89326264366cd076eab612f01153a1fd6fa6fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Mendelski?= Date: Sat, 7 Dec 2024 11:38:16 +0100 Subject: [PATCH] Support jacoco report task run separately --- .../gradle/integration/CommandLineTest.kt | 7 +++++++ .../gradle/integration/JUnitBasicTest.kt | 7 +++++++ .../gradle/integration/JUnitClasspathTest.kt | 6 ++++++ .../gradle/integration/JacocoBasedTest.kt | 19 +++++++++++++++++++ .../gradle/integration/KotestBasicTest.kt | 7 +++++++ .../gradle/integration/KotestClasspathTest.kt | 6 ++++++ .../integration/KotlinInternalScopeTest.kt | 6 ++++++ .../integration/LazyTaskRegisteringTest.kt | 6 ++++++ .../coditory/gradle/integration/LombokTest.kt | 6 ++++++ .../integration/PlatformDependencyTest.kt | 6 ++++++ .../gradle/integration/SpockBasicTest.kt | 7 +++++++ .../gradle/integration/SpockClasspathTest.kt | 6 ++++++ .../integration/JacocoTaskConfiguration.kt | 11 +++++++---- .../gradle/integration/base/TestProject.kt | 8 +++++--- 14 files changed, 101 insertions(+), 7 deletions(-) diff --git a/src/integration/kotlin/com/coditory/gradle/integration/CommandLineTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/CommandLineTest.kt index 58fff32..73363ab 100644 --- a/src/integration/kotlin/com/coditory/gradle/integration/CommandLineTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/CommandLineTest.kt @@ -6,6 +6,7 @@ import com.coditory.gradle.integration.base.TestProject import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AutoClose import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest @@ -81,6 +82,12 @@ class CommandLineTest { } } + @AfterEach + fun cleanProjects() { + project.clean() + failingProject.clean() + } + @ParameterizedTest(name = "should run unit tests and integration tests on check command for gradle {0}") @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { diff --git a/src/integration/kotlin/com/coditory/gradle/integration/JUnitBasicTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/JUnitBasicTest.kt index a57e846..80fe11e 100644 --- a/src/integration/kotlin/com/coditory/gradle/integration/JUnitBasicTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/JUnitBasicTest.kt @@ -6,6 +6,7 @@ import com.coditory.gradle.integration.base.TestProject import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AutoClose import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource @@ -80,6 +81,12 @@ class JUnitBasicTest { } } + @AfterEach + fun cleanProjects() { + project.clean() + failingProject.clean() + } + @ParameterizedTest(name = "should pass unit tests and integration tests on check command for gradle {0}") @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { diff --git a/src/integration/kotlin/com/coditory/gradle/integration/JUnitClasspathTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/JUnitClasspathTest.kt index 1d2356e..88cdc74 100644 --- a/src/integration/kotlin/com/coditory/gradle/integration/JUnitClasspathTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/JUnitClasspathTest.kt @@ -6,6 +6,7 @@ import com.coditory.gradle.integration.base.TestProject import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AutoClose import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource @@ -195,6 +196,11 @@ class JUnitClasspathTest { } } + @AfterEach + fun cleanProject() { + project.clean() + } + @ParameterizedTest(name = "should read files from classpath for gradle {0}") @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { diff --git a/src/integration/kotlin/com/coditory/gradle/integration/JacocoBasedTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/JacocoBasedTest.kt index 073d1d4..1f71e7a 100644 --- a/src/integration/kotlin/com/coditory/gradle/integration/JacocoBasedTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/JacocoBasedTest.kt @@ -5,6 +5,7 @@ import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPOR import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AutoClose import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource @@ -87,6 +88,11 @@ class JacocoBasedTest { .build() } + @AfterEach + fun cleanProject() { + project.clean() + } + @ParameterizedTest(name = "should aggregate coverage from unit and integration tests when using Jacoco {0}") @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should aggregate coverage from unit and integration tests when using Jacoco`(gradleVersion: String?) { @@ -99,4 +105,17 @@ class JacocoBasedTest { // missed method is the init .contains("") } + + @ParameterizedTest(name = "should aggregate coverage from unit and integration tests when using Jacoco after tests {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should aggregate coverage from unit and integration tests when using Jacoco after tests`(gradleVersion: String?) { + // given + project.runGradle(listOf("check"), gradleVersion) + // when + project.runGradle(listOf("jacocoTestReport"), gradleVersion) + // then + assertThat(project.readFileFromBuildDir("reports/jacoco/test/jacocoTestReport.xml")) + // missed method is the init + .contains("") + } } diff --git a/src/integration/kotlin/com/coditory/gradle/integration/KotestBasicTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/KotestBasicTest.kt index 6444a6c..9232d48 100644 --- a/src/integration/kotlin/com/coditory/gradle/integration/KotestBasicTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/KotestBasicTest.kt @@ -6,6 +6,7 @@ import com.coditory.gradle.integration.base.TestProject import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AutoClose import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource @@ -80,6 +81,12 @@ class KotestBasicTest { } } + @AfterEach + fun cleanProjects() { + project.clean() + failingProject.clean() + } + @ParameterizedTest(name = "should pass unit tests and integration tests on check command for gradle {0}") @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { diff --git a/src/integration/kotlin/com/coditory/gradle/integration/KotestClasspathTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/KotestClasspathTest.kt index cda2046..7fd442b 100644 --- a/src/integration/kotlin/com/coditory/gradle/integration/KotestClasspathTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/KotestClasspathTest.kt @@ -6,6 +6,7 @@ import com.coditory.gradle.integration.base.TestProject import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AutoClose import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource @@ -165,6 +166,11 @@ class KotestClasspathTest { } } + @AfterEach + fun cleanProject() { + project.clean() + } + @ParameterizedTest(name = "should read files from classpath for gradle {0}") @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { diff --git a/src/integration/kotlin/com/coditory/gradle/integration/KotlinInternalScopeTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/KotlinInternalScopeTest.kt index e970e5f..2f5b6fe 100644 --- a/src/integration/kotlin/com/coditory/gradle/integration/KotlinInternalScopeTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/KotlinInternalScopeTest.kt @@ -5,6 +5,7 @@ import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPOR import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AutoClose import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource @@ -96,6 +97,11 @@ class KotlinInternalScopeTest { .build() } + @AfterEach + fun cleanProject() { + project.clean() + } + @ParameterizedTest(name = "should make internal scope visible in integration tests for gradle {0}") @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { diff --git a/src/integration/kotlin/com/coditory/gradle/integration/LazyTaskRegisteringTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/LazyTaskRegisteringTest.kt index 556d07c..2462ca2 100644 --- a/src/integration/kotlin/com/coditory/gradle/integration/LazyTaskRegisteringTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/LazyTaskRegisteringTest.kt @@ -2,6 +2,7 @@ package com.coditory.gradle.integration import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AutoClose import org.junit.jupiter.api.Test @@ -36,6 +37,11 @@ class LazyTaskRegisteringTest { .build() } + @AfterEach + fun cleanProject() { + project.clean() + } + @Test fun `should register test tasks in a lazy manner`() { // when diff --git a/src/integration/kotlin/com/coditory/gradle/integration/LombokTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/LombokTest.kt index 9e03af0..10f120d 100644 --- a/src/integration/kotlin/com/coditory/gradle/integration/LombokTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/LombokTest.kt @@ -5,6 +5,7 @@ import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPOR import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AutoClose import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource @@ -125,6 +126,11 @@ class LombokTest { .build() } + @AfterEach + fun cleanProject() { + project.clean() + } + @ParameterizedTest(name = "should run unit tests and integration tests on check command for gradle {0}") @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should run unit tests and integration tests with lombok`(gradleVersion: String?) { diff --git a/src/integration/kotlin/com/coditory/gradle/integration/PlatformDependencyTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/PlatformDependencyTest.kt index c40137d..bb21722 100644 --- a/src/integration/kotlin/com/coditory/gradle/integration/PlatformDependencyTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/PlatformDependencyTest.kt @@ -5,6 +5,7 @@ import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPOR import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AutoClose import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource @@ -56,6 +57,11 @@ class PlatformDependencyTest { .build() } + @AfterEach + fun cleanProject() { + project.clean() + } + @ParameterizedTest(name = "should use dependency version from platform dependency for gradle {0}") @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { diff --git a/src/integration/kotlin/com/coditory/gradle/integration/SpockBasicTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/SpockBasicTest.kt index 2560b5d..ad09806 100644 --- a/src/integration/kotlin/com/coditory/gradle/integration/SpockBasicTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/SpockBasicTest.kt @@ -7,6 +7,7 @@ import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.TaskOutcome import org.gradle.testkit.runner.TaskOutcome.SUCCESS +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AutoClose import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource @@ -77,6 +78,12 @@ class SpockBasicTest { } } + @AfterEach + fun cleanProjects() { + project.clean() + failingProject.clean() + } + @ParameterizedTest(name = "should pass unit tests and integration tests on check command for gradle {0}") @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { diff --git a/src/integration/kotlin/com/coditory/gradle/integration/SpockClasspathTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/SpockClasspathTest.kt index a649332..082d91e 100644 --- a/src/integration/kotlin/com/coditory/gradle/integration/SpockClasspathTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/SpockClasspathTest.kt @@ -6,6 +6,7 @@ import com.coditory.gradle.integration.base.TestProject import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.TaskOutcome.SUCCESS +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AutoClose import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource @@ -166,6 +167,11 @@ class SpockClasspathTest { } } + @AfterEach + fun cleanProject() { + project.clean() + } + @ParameterizedTest(name = "should read files from classpath for gradle {0}") @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { diff --git a/src/main/kotlin/com/coditory/gradle/integration/JacocoTaskConfiguration.kt b/src/main/kotlin/com/coditory/gradle/integration/JacocoTaskConfiguration.kt index ac3aa5a..17d5fcd 100644 --- a/src/main/kotlin/com/coditory/gradle/integration/JacocoTaskConfiguration.kt +++ b/src/main/kotlin/com/coditory/gradle/integration/JacocoTaskConfiguration.kt @@ -7,21 +7,24 @@ import org.gradle.testing.jacoco.tasks.JacocoCoverageVerification import org.gradle.testing.jacoco.tasks.JacocoReport internal object JacocoTaskConfiguration { + private const val JACOCO_PLUGIN = "jacoco" + private const val JACOCO_REPORT_TASK = "jacocoTestReport" + fun apply(project: Project) { - if (!project.pluginManager.hasPlugin("jacoco")) return + if (!project.pluginManager.hasPlugin(JACOCO_PLUGIN)) return project.tasks.withType(JacocoCoverageVerification::class.java).configureEach { task -> task.mustRunAfter(INTEGRATION) } project.tasks.withType(JacocoReport::class.java).configureEach { task -> task.mustRunAfter(INTEGRATION) } - // execute only if integration test and jacoco are on the execution path + // execute only if integration tests or jacocoTestReport are on the execution path // to preserve lazy task configuration project.gradle.taskGraph.whenReady { val names = project.gradle.taskGraph.allTasks.map { it.name } - if (names.contains("jacocoTestReport") && names.contains(INTEGRATION)) { + if (names.contains(JACOCO_REPORT_TASK) || names.contains(INTEGRATION)) { project.tasks.withType(JacocoReport::class.java) - .named("jacocoTestReport") { reportTask -> + .named(JACOCO_REPORT_TASK) { reportTask -> val jacocoTaskExtension = project.tasks.getByName(INTEGRATION).extensions.getByType(JacocoTaskExtension::class.java) val dstFile = jacocoTaskExtension.destinationFile?.path diff --git a/src/test/kotlin/com/coditory/gradle/integration/base/TestProject.kt b/src/test/kotlin/com/coditory/gradle/integration/base/TestProject.kt index 357c89b..e005c96 100644 --- a/src/test/kotlin/com/coditory/gradle/integration/base/TestProject.kt +++ b/src/test/kotlin/com/coditory/gradle/integration/base/TestProject.kt @@ -29,9 +29,11 @@ class TestProject(private val project: Project) : Project by project { this.projectDir.deleteRecursively() } - private fun gradleRunner(project: Project, arguments: List, gradleVersion: String? = null): GradleRunner { - // clean is required so tasks are not cached - val args = if (arguments.contains("clean")) arguments else listOf("clean") + arguments + fun clean() { + this.runGradle(listOf("clean")) + } + + private fun gradleRunner(project: Project, args: List, gradleVersion: String? = null): GradleRunner { val builder = GradleRunner.create() .withProjectDir(project.projectDir) .withArguments(args)