Skip to content

Commit

Permalink
Addresses some Gradle 9 breaking changes (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzo authored Nov 29, 2024
1 parent 0c41c8b commit 42ce019
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion demo-project/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ tasks.jacocoAggregatedReport {
}

val reportsSpec = copySpec {
val tookRegEx = "\\b\\d+\\.\\d+s\\b".toRegex()
val tookRegEx = "\\b\\d+(?:\\.\\d+)?s\\b".toRegex()

from(tasks.jacocoAggregatedReport) { include("**/*.csv") }
from(tasks.testAggregatedReport) {
Expand Down
1 change: 0 additions & 1 deletion demo-project/kmp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootExtension

plugins {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ <h2>Tests</h2>
<tr>
<td class="success">testKMPObject</td>
<td class="success">testKMPObject</td>
<td class="success">0s</td>
<td class="success">0.100s</td>
<td class="success">passed</td>
</tr>
</table>
Expand Down
2 changes: 1 addition & 1 deletion demo-project/ui-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {
minSdk = libs.versions.android.minSDK.get().toInt()
}

targetProjectPath = projects.demoProject.app.dependencyProject.path
targetProjectPath = projects.demoProject.app.path

defaultConfig {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package io.github.gmazzo.android.test.aggregation

import com.android.build.api.dsl.BuildType
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.ExtensionAware
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.android.build.api.artifact.ScopedArtifact
import com.android.build.api.variant.HasUnitTest
import com.android.build.api.variant.ScopedArtifacts
import com.android.build.api.variant.Variant
import com.android.build.gradle.tasks.factory.AndroidUnitTest
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.type.ArtifactTypeDefinition
Expand All @@ -17,9 +18,7 @@ import org.gradle.api.attributes.VerificationType
import org.gradle.api.file.Directory
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.file.RegularFile
import org.gradle.api.internal.provider.Providers
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Sync
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.testing.AbstractTestTask
Expand All @@ -35,9 +34,7 @@ import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.registering
import org.gradle.kotlin.dsl.the
import org.gradle.kotlin.dsl.invoke
import org.gradle.testing.jacoco.plugins.JacocoTaskExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation.Companion.MAIN_COMPILATION_NAME

abstract class AndroidTestCoverageAggregationPlugin : Plugin<Project> {
Expand Down Expand Up @@ -201,6 +198,11 @@ abstract class AndroidTestCoverageAggregationPlugin : Plugin<Project> {
}

private val TaskProvider<AbstractTestTask>.execData
get() = map { it.the<JacocoTaskExtension>().destinationFile!! }
get() = map {
when (it) {
is AndroidUnitTest -> it.jacocoCoverageOutputFile
else -> it.the<JacocoTaskExtension>().destinationFile
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.github.gmazzo.android.test.aggregation
import com.android.build.api.variant.HasUnitTest
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.attributes.Category
import org.gradle.api.attributes.TestSuiteType
import org.gradle.api.attributes.Usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ import com.android.build.gradle.BaseExtension
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.tasks.testing.AbstractTestTask
import org.gradle.configurationcache.extensions.capitalized
import org.gradle.kotlin.dsl.aggregateTestCoverage
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.withType
import org.gradle.kotlin.dsl.getByName
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.testAggregation
import org.gradle.kotlin.dsl.the
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetsContainer
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMetadataTarget
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget

internal val Project.android
Expand Down Expand Up @@ -64,7 +61,7 @@ internal fun TestAggregationExtension.aggregateProject(project: Project, config:
config.dependencies.add(project.dependencies.testAggregation(project))

private fun TestAggregationExtension.Modules.includes(project: Project) =
(includes.get().isEmpty() || project in includes.get()) && project !in excludes.get()
(includes.get().isEmpty() || project.path in includes.get()) && project.path !in excludes.get()

internal fun Project.unitTestTaskOf(variant: Variant) = (variant as? HasUnitTest)
?.unitTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,37 @@ abstract class TestAggregationExtension {
*/
abstract class Modules {

abstract val includes: SetProperty<Project>
/**
* List of [Project.getPath] that should be included in the aggregation. If empty, all projects are included
*/
abstract val includes: SetProperty<String>

abstract val excludes: SetProperty<Project>
/**
* List of [Project.getPath] that should be excluded from the aggregation
*/
abstract val excludes: SetProperty<String>

fun include(vararg includes: Project) = apply {
this.includes.addAll(*includes)
this.includes.addAll(includes.map { it.path })
}

fun include(includes: Iterable<Project>) = apply {
this.includes.addAll(includes)
this.includes.addAll(includes.map { it.path })
}

fun include(vararg includes: ProjectDependency) =
include(includes.map { it.dependencyProject })
this.includes.addAll(includes.map { it.path })

fun exclude(vararg excludes: Project) = apply {
this.excludes.addAll(*excludes)
this.excludes.addAll(excludes.map { it.path })
}

fun exclude(excludes: Iterable<Project>) = apply {
this.excludes.addAll(excludes)
this.excludes.addAll(excludes.map { it.path })
}

fun exclude(vararg excludes: ProjectDependency) =
exclude(excludes.map { it.dependencyProject })
this.excludes.addAll(excludes.map { it.path })

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.api.attributes.Usage
import org.gradle.api.internal.artifacts.dependencies.AbstractModuleDependency
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.provider.Property
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget

const val USAGE_TEST_AGGREGATION = "test-aggregation"

fun DependencyHandler.testAggregation(dependency: Any): Dependency = create(dependency).also {
(it as? ProjectDependency)?.apply {
UsageTestAggregationCompatibilityRule.bind(dependencyProject)
val objects = (this as AbstractModuleDependency).objectFactory

UsageTestAggregationCompatibilityRule.bind(attributesSchema)

attributes {
attribute(Usage.USAGE_ATTRIBUTE, dependencyProject.objects.named(USAGE_TEST_AGGREGATION))
attribute(Usage.USAGE_ATTRIBUTE, objects.named(USAGE_TEST_AGGREGATION))
}
}
}
Expand Down

0 comments on commit 42ce019

Please sign in to comment.