Skip to content

Commit

Permalink
Enable gradle configuration cache #151
Browse files Browse the repository at this point in the history
  • Loading branch information
pmendelski committed Nov 11, 2024
1 parent a0ce6dc commit 7ed8752
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Gradle
.gradle
.kotlin
build
!gradle/wrapper/gradle-wrapper.jar

Expand Down
8 changes: 4 additions & 4 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ Add plugin jar to the sample project (that uses the tested plugin):

```kt
buildscript {
dependencies {
classpath(files("<PLUGIN_PROJECT_PATH>/build/libs/integration-test-plugin.jar"))
}
dependencies {
classpath(files("<PLUGIN_PROJECT_PATH>/build/libs/integration-test-plugin.jar"))
}
}

apply(plugin = "com.coditory.build")
apply(plugin = "com.coditory.integration-test")
```

## Validating plugin module metadata
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
and [Kover](https://github.com/Kotlin/kotlinx-kover).
- Integrates with test frameworks like [JUnit5](https://junit.org/junit5/), [Spock](https://spockframework.org/) and
[Kotest](https://kotest.io/).
- Compatible with [gradle configuration cache](https://docs.gradle.org/current/userguide/configuration_cache.html).

## Using the plugin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class CommandLineTest {
// when
val result = project.runGradle(listOf("check", "-PskipIntegrationTest"))
// then
assertThat(result.task(":check")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SKIPPED)
}
Expand All @@ -171,4 +172,13 @@ class CommandLineTest {
assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SKIPPED)
assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}

@ParameterizedTest(name = "task {0} should be cacheable by gradle configuration cache")
@ValueSource(strings = ["test", "integrationTest", "testAll", "check"])
fun `should be cacheable by gradle configuration cache`(task: String?) {
// when
val result = project.runGradle(listOf("--configuration-cache", task!!))
// then
assertThat(result.task(":$task")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ open class IntegrationTestPlugin : Plugin<Project> {
if (!project.plugins.hasPlugin(JvmTestSuitePlugin::class.java)) {
project.plugins.apply(JvmTestSuitePlugin::class.java)
}
TestSuitesConfiguration.apply(project)
TestAllTaskConfiguration.apply(project)
val config = IntegrationTestPluginConfig.resolve(project)
TestSuitesConfiguration.apply(project, config)
TestAllTaskConfiguration.apply(project, config)
JacocoTaskConfiguration.apply(project)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.coditory.gradle.integration

import com.coditory.gradle.integration.IntegrationTestPlugin.Companion.INTEGRATION
import com.coditory.gradle.integration.IntegrationTestPlugin.Companion.INTEGRATION_TEST
import com.coditory.gradle.integration.IntegrationTestPlugin.Companion.SKIP_INTEGRATION_TEST_FLAG_NAME
import com.coditory.gradle.integration.IntegrationTestPlugin.Companion.SKIP_TEST_ALL_FLAG_NAME
import com.coditory.gradle.integration.IntegrationTestPlugin.Companion.SKIP_UNIT_TEST_FLAG_NAME
import org.gradle.api.Project

internal data class IntegrationTestPluginConfig(
val allTestTaskEnabled: Boolean,
val unitTestsEnabled: Boolean,
val integrationTestsEnabled: Boolean,
) {
companion object {
fun resolve(project: Project): IntegrationTestPluginConfig {
return IntegrationTestPluginConfig(
allTestTaskEnabled = !skipAllTests(project),
unitTestsEnabled = !skipUnitTests(project),
integrationTestsEnabled = !skipIntegrationTests(project),
)
}

private fun skipAllTests(project: Project): Boolean {
return hasTestAllFlag(project) ||
(hasSkipUnitTestFlag(project) && hasSkipIntegrationTestFlag(project))
}

private fun skipUnitTests(project: Project): Boolean {
return hasTestAllFlag(project) || hasSkipUnitTestFlag(project)
}

private fun skipIntegrationTests(project: Project): Boolean {
return hasTestAllFlag(project) || hasSkipIntegrationTestFlag(project)
}

private fun hasTestAllFlag(project: Project): Boolean {
return hasPropertyFlag(project, SKIP_TEST_ALL_FLAG_NAME)
}

private fun hasSkipUnitTestFlag(project: Project): Boolean {
return hasPropertyFlag(project, SKIP_UNIT_TEST_FLAG_NAME)
}

private fun hasSkipIntegrationTestFlag(project: Project): Boolean {
return hasPropertyFlag(project, SKIP_INTEGRATION_TEST_FLAG_NAME) ||
hasExcludeIntegrationTestTaskParam(
project,
)
}

private fun hasExcludeIntegrationTestTaskParam(project: Project): Boolean {
return hasExcludedTask(project, INTEGRATION_TEST) || hasExcludedTask(project, INTEGRATION)
}

private fun hasPropertyFlag(project: Project, name: String): Boolean {
if (project.properties.containsKey(name)) {
val value = project.properties[name]
return value == null || !value.toString().equals("false", true)
}
return false
}

private fun hasExcludedTask(project: Project, name: String): Boolean {
return project.gradle.startParameter.excludedTaskNames.contains(name)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package com.coditory.gradle.integration

import com.coditory.gradle.integration.IntegrationTestPlugin.Companion.INTEGRATION_TEST
import com.coditory.gradle.integration.IntegrationTestPlugin.Companion.TEST_ALL_TASK_NAME
import com.coditory.gradle.integration.TestSkippingConditions.skipTestAll
import org.gradle.api.Project
import org.gradle.api.tasks.testing.Test
import org.gradle.language.base.plugins.LifecycleBasePlugin

internal object TestAllTaskConfiguration {
fun apply(project: Project) {
fun apply(project: Project, config: IntegrationTestPluginConfig) {
val testAllTask = project.tasks.create(TEST_ALL_TASK_NAME)
testAllTask.description = "Runs all test suites."
testAllTask.group = LifecycleBasePlugin.VERIFICATION_GROUP
testAllTask.onlyIf { !skipTestAll(project) }
testAllTask.enabled = config.allTestTaskEnabled
project.tasks.withType(Test::class.java).forEach {
testAllTask.dependsOn(it.name)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.coditory.gradle.integration

import com.coditory.gradle.integration.IntegrationTestPlugin.Companion.INTEGRATION
import com.coditory.gradle.integration.IntegrationTestPlugin.Companion.INTEGRATION_TEST
import com.coditory.gradle.integration.TestSkippingConditions.skipIntegrationTest
import com.coditory.gradle.integration.TestSkippingConditions.skipUnitTest
import org.gradle.api.Project
import org.gradle.api.attributes.TestSuiteType
import org.gradle.api.plugins.JavaBasePlugin
Expand All @@ -24,28 +22,28 @@ internal object TestSuitesConfiguration {
}
}

fun apply(project: Project) {
setupTestSuite(project)
setupTestTask(project)
fun apply(project: Project, config: IntegrationTestPluginConfig) {
setupTestSuite(project, config)
setupTestTask(project, config)
if (isKotlinProject) {
configureKotlinCompilation(project)
}
}

private fun setupTestSuite(project: Project) {
private fun setupTestSuite(project: Project, config: IntegrationTestPluginConfig) {
val testing = project.extensions.getByType(TestingExtension::class.java)
val test = testing.suites.getByName("test") as JvmTestSuite
test.targets.all { target ->
target.testTask.configure { task ->
task.onlyIf { !skipUnitTest(project) }
task.enabled = config.unitTestsEnabled
}
}
testing.suites.register(INTEGRATION, JvmTestSuite::class.java) { testSuite ->
testSuite.testType.set(TestSuiteType.INTEGRATION_TEST)
testSuite.targets.all { target ->
target.testTask.configure { task ->
task.shouldRunAfter(test)
task.onlyIf { !skipIntegrationTest(project) }
task.enabled = config.integrationTestsEnabled
}
}
setupIntegrationSourceSet(project, testSuite)
Expand All @@ -71,17 +69,18 @@ internal object TestSuitesConfiguration {
integrationSourceSet.runtimeClasspath += testSourceSet.output + mainSourceSet.output
}

private fun setupTestTask(project: Project) {
private fun setupTestTask(project: Project, config: IntegrationTestPluginConfig) {
val integrationTestTask = project.tasks.create(INTEGRATION_TEST)
integrationTestTask.description = "Runs integration test suites."
integrationTestTask.group = LifecycleBasePlugin.VERIFICATION_GROUP
integrationTestTask.onlyIf { !skipIntegrationTest(project) }
integrationTestTask.enabled = config.integrationTestsEnabled
integrationTestTask.dependsOn(INTEGRATION)
project.tasks.getByName(JavaBasePlugin.CHECK_TASK_NAME)
.dependsOn(INTEGRATION_TEST)
project.tasks.getByName(JavaBasePlugin.CHECK_TASK_NAME)
.dependsOn(INTEGRATION)
project.tasks.getByName(INTEGRATION).onlyIf { !skipIntegrationTest(project) }
project.tasks.getByName(INTEGRATION)
.enabled = config.integrationTestsEnabled
}

private fun configureKotlinCompilation(project: Project) {
Expand Down

0 comments on commit 7ed8752

Please sign in to comment.