Skip to content

Commit 70a6478

Browse files
authored
Add: Code coverage to the plugin tests (#62)
1 parent d7c74ff commit 70a6478

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

.github/workflows/build.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ jobs:
1616
uses: reactivecircus/android-emulator-runner@v2
1717
with:
1818
api-level: 28
19-
script: ./gradlew clean test --stacktrace
19+
script: ./gradlew clean test --stacktrace
20+
- name: upload coverage
21+
uses: codecov/codecov-action@v2
22+
with:
23+
files: ./plugin/build/reports/jacoco/test/jacocoTestReport.xml

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[![Gradle Plugin Portal](https://img.shields.io/maven-metadata/v/https/plugins.gradle.org/m2/nl.neotech.plugin/android-root-coverage-plugin/maven-metadata.xml.svg?label=Gradle%20Plugin%20Portal)](https://plugins.gradle.org/plugin/nl.neotech.plugin.rootcoverage)
22
[![Maven Central](https://img.shields.io/maven-central/v/nl.neotech.plugin/android-root-coverage-plugin?label=Maven%20Central)](https://search.maven.org/artifact/nl.neotech.plugin/android-root-coverage-plugin)
33
[![Build](https://github.com/NeoTech-Software/Android-Root-Coverage-Plugin/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/NeoTech-Software/Android-Root-Coverage-Plugin/actions/workflows/build.yml)
4+
[![Coverage](https://img.shields.io/codecov/c/gh/NeoTech-Software/Android-Root-Coverage-Plugin/branch/master?token=4I3MXF2WLX)](https://app.codecov.io/gh/NeoTech-Software/Android-Root-Coverage-Plugin/branch/master)
45

56
# Android-Root-Coverage-Plugin
67
**Automatically configures Jacoco code coverage tasks for both combined and per module coverage reports.**

plugin/build.gradle

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
// Core Gradle plugins
33
id "java-gradle-plugin"
4+
id "jacoco"
45

56
// Third-party plugins
67
alias(libs.plugins.kotlinJvm)
@@ -23,6 +24,13 @@ java {
2324
targetCompatibility = JavaVersion.VERSION_1_8
2425
}
2526

27+
jacocoTestReport {
28+
reports {
29+
xml.required = true
30+
csv.required = false
31+
}
32+
}
33+
2634
plugins.withId("com.vanniktech.maven.publish") {
2735
mavenPublish {
2836
sonatypeHost = "S01"
@@ -76,3 +84,47 @@ dependencies {
7684
testImplementation gradleTestKit()
7785
testImplementation libs.mockk
7886
}
87+
88+
// Setup Jacoco for Gradle TestKit
89+
pluginManager.withPlugin("jacoco") {
90+
91+
// Extracts the JaCoCo runtime jar from the configured JaCoCo Agent
92+
def extractJacocoRuntimeTask = tasks.register("extractJacocoTestKitRuntime", Copy) {
93+
from {
94+
zipTree(project.configurations.getByName(JacocoPlugin.AGENT_CONFIGURATION_NAME).asPath).matching { include 'jacocoagent.jar' }.singleFile
95+
}
96+
into file("${buildDir}/testkit")
97+
}
98+
99+
def javaTestTask = tasks.named(JavaPlugin.TEST_TASK_NAME)
100+
101+
def destinationFileProvider = javaTestTask.map {
102+
(it.extensions.getByType(JacocoTaskExtension.class) as JacocoTaskExtension).destinationFile
103+
}
104+
105+
// Generates a gradle.properties file that contains the correct JVM arguments to run with the extracted JaCoCo agent
106+
def generateGradlePropertiesTask = tasks.register("generateJacocoTestKitGradleProperties", WriteProperties.class) {
107+
it.group = "reporting"
108+
it.description = "Generates testkit-gradle.properties file which can be read and added to a TestKit build as gradle.properties"
109+
it.dependsOn(extractJacocoRuntimeTask)
110+
it.outputFile = new File(buildDir, "testkit/${javaTestTask.name}/gradle.properties")
111+
it.property("org.gradle.jvmargs", "\"-javaagent:${buildDir}/testkit/jacocoagent.jar=destfile=${destinationFileProvider.get()}\"")
112+
}
113+
114+
// Make the generated gradle.properties file available on the test classpath as resource
115+
dependencies.add(JavaPlugin.TEST_RUNTIME_ONLY_CONFIGURATION_NAME, files(new File(buildDir, "testkit/${javaTestTask.name}")))
116+
117+
// "Fixes": https://github.com/gradle/gradle/issues/16603
118+
def jacocoTestReport = tasks.named("jacocoTestReport")
119+
jacocoTestReport.configure {
120+
it.doFirst {
121+
sleep(1000)
122+
}
123+
}
124+
125+
// Setup task dependencies
126+
javaTestTask.configure {
127+
it.dependsOn(generateGradlePropertiesTask)
128+
it.finalizedBy(jacocoTestReport)
129+
}
130+
}

plugin/src/test/kotlin/org/neotech/plugin/rootcoverage/IntegrationTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.junit.runners.Parameterized
1010
import org.neotech.plugin.rootcoverage.util.SystemOutputWriter
1111
import org.neotech.plugin.rootcoverage.util.createGradlePropertiesFile
1212
import org.neotech.plugin.rootcoverage.util.createLocalPropertiesFile
13+
import org.neotech.plugin.rootcoverage.util.put
1314
import java.io.File
1415
import java.util.Properties
1516
import kotlin.test.assertEquals
@@ -27,6 +28,10 @@ class IntegrationTest(
2728
createLocalPropertiesFile(projectRoot)
2829
createGradlePropertiesFile(projectRoot, properties = Properties().apply {
2930
put("android.useAndroidX", "true")
31+
put(Properties().apply {
32+
val resource = GradleRunner::class.java.classLoader.getResourceAsStream("gradle.properties")
33+
load(resource)
34+
})
3035
})
3136

3237
val runner = GradleRunner.create()

plugin/src/test/kotlin/org/neotech/plugin/rootcoverage/util/ProjectGeneration.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ internal fun createGradlePropertiesFile(root: File, properties: Properties) {
2020
File(root, "gradle.properties").outputStream().use { properties.store(it, null) }
2121
}
2222

23+
internal fun Properties.put(properties: Properties) {
24+
properties.forEach {
25+
put(it.key, it.value)
26+
}
27+
}
28+
29+
internal fun File.getProperties(): Properties = inputStream().use {
30+
Properties().apply {
31+
load(it)
32+
}
33+
}
34+
2335
/**
2436
* Tries to resolve the Android SDK directory. This function first tries the ANDROID_HOME system
2537
* environment variable if this variable does not exist it tries to find and parse the

0 commit comments

Comments
 (0)