Skip to content

Commit

Permalink
(WIP) adapted code
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzo committed Nov 9, 2023
1 parent 3b3ba40 commit 81fbcfe
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 26 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ now promoted to dedicated Gradle plugins:
[io.github.gmazzo.test.aggregation.coverage](https://plugins.gradle.org/plugin/io.github.gmazzo.test.aggregation.coverage) and
[io.github.gmazzo.test.aggregation.results](https://plugins.gradle.org/plugin/io.github.gmazzo.test.aggregation.results)


## `UnitTest` and `AndroidTest` code coverage support
By default, the plugin will do `buildTypes["debug]".enableUnitTestCoverage = true` when applied, allowing coverage to be collected.

Coverage on `AndroidTest` variant is also support and will be automatically collected, if `buildType["debug"].enableAndroidTestCoverage = true` is also set (it applies for any `BuildType`)

## Filtering content
The plugins will automatically aggregate `android` modules and `java` modules that also apply `jacoco` plugin on the
`jacocoAggregation` and the `testReportAggregation` configurations.
Expand Down
5 changes: 3 additions & 2 deletions demo-project/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ android {

buildTypes {
debug {
enableUnitTestCoverage = true
// FIXME UI tests requires an emulator on CI
enableAndroidTestCoverage = true
}
release {
aggregateTestCoverage = false
Expand All @@ -39,7 +40,7 @@ android {
}
create("prod") {
dimension = "environment"
//aggregateTestCoverage.set(false)
aggregateTestCoverage.set(false)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.myapplication

object SomeSingleton {

fun doStuff() {
println("SomeSingleton")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ class ExampleInstrumentedTest {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.app.test", appContext.packageName)

SomeSingleton.doStuff() // to mark coverage of UI tests
}
}

2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
kotlin = "1.9.20"
agp = "8.1.2"
agp = "8.3.0-alpha12"
android-minSDK = "21"
android-compileSDK = "34"
androidx-lifecycle = "2.6.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package io.github.gmazzo.android.test.aggregation

import com.android.build.api.artifact.ScopedArtifact
import com.android.build.api.variant.HasAndroidTest
import com.android.build.api.variant.HasUnitTest
import com.android.build.api.variant.ScopedArtifacts
import com.android.build.api.variant.Variant
Expand All @@ -18,10 +19,13 @@ import org.gradle.api.file.Directory
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.file.RegularFile
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Sync
import org.gradle.api.tasks.testing.AbstractTestTask
import org.gradle.kotlin.dsl.USAGE_TEST_AGGREGATION
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.domainObjectSet
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.getAt
import org.gradle.kotlin.dsl.getValue
Expand All @@ -43,16 +47,21 @@ abstract class AndroidTestCoverageAggregationPlugin : Plugin<Project> {
// enables jacoco test coverage on `debug` build type by default
android.buildTypes["debug"].enableUnitTestCoverage = true

val jacocoVariants = objects.namedDomainObjectSet(Variant::class)
val codeCoverageExecData = objects.domainObjectSet(Provider::class)
val aggregatedVariants = objects.namedDomainObjectSet(Variant::class)

androidComponents.onVariants { variant ->
androidComponents.onVariants(androidComponents.selector().all()) { variant ->
val buildType = android.buildTypes[variant.buildType!!]

if ((variant as? HasUnitTest)?.unitTest != null &&
buildType.enableUnitTestCoverage &&
android.shouldAggregate(variant)
) {
jacocoVariants.add(variant)
if (android.shouldAggregate(variant)) {
if (buildType.enableUnitTestCoverage && (variant as? HasUnitTest)?.unitTest != null) {
aggregatedVariants.add(variant)
codeCoverageExecData.add(unitTestTaskOf(variant).execData)
}
if (buildType.enableAndroidTestCoverage && (variant as? HasAndroidTest)?.androidTest != null) {
aggregatedVariants.add(variant)
codeCoverageExecData.add(androidTestTaskOf(variant).execData)
}
}
}

Expand All @@ -73,11 +82,8 @@ abstract class AndroidTestCoverageAggregationPlugin : Plugin<Project> {
)
}
afterEvaluate {
jacocoVariants.all variant@{
val execData = unitTestTaskOf(this@variant)!!
.map { it.the<JacocoTaskExtension>().destinationFile!! }

outgoing.artifact(execData) {
codeCoverageExecData.all data@{
outgoing.artifact(this@data) {
type = ArtifactTypeDefinition.BINARY_DATA_TYPE
}
}
Expand All @@ -87,7 +93,7 @@ abstract class AndroidTestCoverageAggregationPlugin : Plugin<Project> {
val allVariantsSourcesForCoverageReport by tasks.registering(Sync::class) {
destinationDir = temporaryDir
duplicatesStrategy = DuplicatesStrategy.INCLUDE // in case of duplicated classes
jacocoVariants.all {
aggregatedVariants.all {
from(sources.java?.all, sources.kotlin?.all)
}
}
Expand Down Expand Up @@ -132,7 +138,7 @@ abstract class AndroidTestCoverageAggregationPlugin : Plugin<Project> {
)
}

jacocoVariants.all task@{
aggregatedVariants.all task@{
artifacts
.forScope(ScopedArtifacts.Scope.PROJECT)
.use(allVariantsClassesForCoverageReport)
Expand Down Expand Up @@ -180,4 +186,7 @@ abstract class AndroidTestCoverageAggregationPlugin : Plugin<Project> {
}
}

private val Provider<out AbstractTestTask>.execData
get() = map { it.the<JacocoTaskExtension>().destinationFile!! }

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.github.gmazzo.android.test.aggregation

import com.android.build.api.variant.HasAndroidTest
import com.android.build.api.variant.HasUnitTest
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.attributes.Category
import org.gradle.api.attributes.TestSuiteType
import org.gradle.api.attributes.Usage
import org.gradle.api.attributes.VerificationType
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.testing.AbstractTestTask
import org.gradle.kotlin.dsl.USAGE_TEST_AGGREGATION
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.named
Expand Down Expand Up @@ -34,13 +37,16 @@ abstract class AndroidTestResultsAggregationPlugin : Plugin<Project> {
}
}

fun aggregate(task: Provider<out AbstractTestTask>) = afterEvaluate {
testResultsElements.outgoing.artifact(task.flatMap { it.binaryResultsDirectory })
}

androidComponents.onVariants { variant ->
if ((variant as? HasUnitTest)?.unitTest != null && android.shouldAggregate(variant)) {
afterEvaluate {
val testTask = unitTestTaskOf(variant)!!

testResultsElements.outgoing.artifact(testTask.flatMap { it.binaryResultsDirectory })
}
//aggregate(unitTestTaskOf(variant))
}
if ((variant as? HasAndroidTest)?.androidTest != null && android.shouldAggregate(variant)) {
//aggregate(androidTestTaskOf(variant))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.gmazzo.android.test.aggregation

import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.api.variant.HasAndroidTest
import com.android.build.api.variant.HasUnitTest
import com.android.build.api.variant.Variant
import com.android.build.gradle.BaseExtension
Expand All @@ -13,7 +14,6 @@ import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.getByName
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.testAggregation
import org.gradle.kotlin.dsl.the

Expand Down Expand Up @@ -60,6 +60,10 @@ internal fun TestAggregationExtension.aggregateProject(project: Project, config:
private fun TestAggregationExtension.Modules.includes(project: Project) =
(includes.get().isEmpty() || project in includes.get()) && project !in excludes.get()

internal fun Project.unitTestTaskOf(variant: Variant) = (variant as? HasUnitTest)
?.unitTest
?.let { tasks.named<AbstractTestTask>("test${it.name.capitalized()}") }
internal fun <Type> Project.unitTestTaskOf(variant: Type) where Type : Variant, Type : HasUnitTest = provider {
tasks.getByName<AbstractTestTask>("test${(variant as HasUnitTest).unitTest!!.name.capitalized()}")
}

internal fun <Type> Project.androidTestTaskOf(variant: Type) where Type : Variant, Type : HasAndroidTest = provider {
tasks.getByName<AbstractTestTask>("test${variant.androidTest!!.name.capitalized()}")
}

0 comments on commit 81fbcfe

Please sign in to comment.