Skip to content

Commit

Permalink
Support jacoco report task run separately
Browse files Browse the repository at this point in the history
  • Loading branch information
pmendelski authored and ogesaku committed Dec 7, 2024
1 parent 32ae924 commit ebb2f58
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand All @@ -99,4 +105,17 @@ class JacocoBasedTest {
// missed method is the init
.contains("<counter type=\"METHOD\" missed=\"1\" covered=\"2\"/>")
}

@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("<counter type=\"METHOD\" missed=\"1\" covered=\"2\"/>")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -36,6 +37,11 @@ class LazyTaskRegisteringTest {
.build()
}

@AfterEach
fun cleanProject() {
project.clean()
}

@Test
fun `should register test tasks in a lazy manner`() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ class TestProject(private val project: Project) : Project by project {
this.projectDir.deleteRecursively()
}

private fun gradleRunner(project: Project, arguments: List<String>, 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<String>, gradleVersion: String? = null): GradleRunner {
val builder = GradleRunner.create()
.withProjectDir(project.projectDir)
.withArguments(args)
Expand Down

0 comments on commit ebb2f58

Please sign in to comment.