Skip to content

Commit a2ccee6

Browse files
authored
Feature/gradle legacy support (#9)
* Add support for the same minimum Gradle version (4.10.1) as supported by Android Gradle plugin version 3.3.0 * Fix double new lines caused by using `forwardOutput()` * Some minor small other improvements such as code formatting
1 parent 3ecbe7d commit a2ccee6

File tree

13 files changed

+137
-47
lines changed

13 files changed

+137
-47
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ https://www.jetbrains.com/help/idea/jetgradle-tool-window.html) or from the term
5757
- **Terminal:** Execute the task using `gradlew rootCodeCoverageReport`.
5858

5959

60+
# Compatibility
61+
| Version | Android Gradle plugin version | Gradle version |
62+
| --------- | ----------------------------- | -------------- |
63+
| **1.1.0** | 3.3 | 5+ |
64+
| **1.0.2** | 3.2 | 4.6+ |
65+
66+
*Note: The Android Gradle Plugin requires a minimum Gradle version, for more information please refer to:*
67+
https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
68+
6069
# Configuration
6170
By default the plugin generates code coverage reports using the build variant `debug` for every
6271
module. However in some cases different build variants per module might be required, especially if
@@ -99,4 +108,4 @@ with a plugin module)*:
99108

100109

101110
# Author note
102-
Many thanks to [Hans van Dam](https://github.com/hansvdam) for helping with testing and the initial idea.
111+
Many thanks to [Hans van Dam](https://github.com/hansvdam) for helping with testing and the initial idea.

gradle/dependencies.gradle

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ ext {
88
projectDependency = [
99

1010
// Gradle Plugins
11-
kotlinPlugin : "org.jetbrains.kotlin:kotlin-gradle-plugin:${projectVersion.kotlin}",
12-
gradlePlugin : "com.android.tools.build:gradle:3.3.0",
11+
kotlinPlugin : "org.jetbrains.kotlin:kotlin-gradle-plugin:${projectVersion.kotlin}",
12+
androidGradlePlugin : "com.android.tools.build:gradle:3.3.0",
1313

1414
// Dependencies
15-
kotlinStdlibJdk7 : "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${projectVersion.kotlin}",
16-
appCompat : "com.android.support:appcompat-v7:28.0.0",
15+
kotlinStdlibJdk7 : "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${projectVersion.kotlin}",
16+
appCompat : "com.android.support:appcompat-v7:28.0.0",
1717

1818
// Test dependencies
19-
junit : "junit:junit:4.12",
20-
truth : "com.google.truth:truth:0.42",
21-
supportTestRunner : "com.android.support.test:runner:1.0.2",
22-
espressoCore : "com.android.support.test.espresso:espresso-core:3.0.2",
23-
commonsCsv : "org.apache.commons:commons-csv:1.6",
24-
kotlinTest : "org.jetbrains.kotlin:kotlin-test:${projectVersion.kotlin}"
19+
junit : "junit:junit:4.12",
20+
truth : "com.google.truth:truth:0.42",
21+
supportTestRunner : "com.android.support.test:runner:1.0.2",
22+
espressoCore : "com.android.support.test.espresso:espresso-core:3.0.2",
23+
commonsCsv : "org.apache.commons:commons-csv:1.6",
24+
kotlinTest : "org.jetbrains.kotlin:kotlin-test:${projectVersion.kotlin}"
2525
]
2626

2727
// Used by the plugin-version-handler.gradle

gradle/plugin-version-handler.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright (C) 2018 Rolf Smit
22
//
3+
// Version 1
4+
//
35
// This file can be used as a hack to make it possible to use variable/dynamic version numbers for
46
// plugins in the new Gradle Plugins Block:
57
// https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block
@@ -37,4 +39,4 @@ resolutionStrategy {
3739
useVersion version
3840
}
3941
}
40-
}
42+
}

plugin/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test {
5757

5858
dependencies {
5959
compileOnly gradleApi()
60-
implementation projectDependency.gradlePlugin
60+
implementation projectDependency.androidGradlePlugin
6161
implementation projectDependency.kotlinStdlibJdk7
6262

6363
testImplementation projectDependency.kotlinStdlibJdk7
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.neotech.plugin.rootcoverage
2+
3+
import org.gradle.api.file.FileCollection
4+
import org.gradle.testing.jacoco.tasks.JacocoReport
5+
import org.gradle.util.GradleVersion
6+
7+
open class JacocoReportCompat : JacocoReport() {
8+
9+
private var classDirectoriesCompat: FileCollection = project.files()
10+
private var sourceDirectoriesCompat: FileCollection = project.files()
11+
12+
fun classDirectoriesFromCompat(fileCollection: FileCollection) {
13+
if (GradleVersion.current() >= GradleVersion.version("5.0.0")) {
14+
classDirectories.from(fileCollection)
15+
} else {
16+
classDirectoriesCompat = classDirectoriesCompat.plus(fileCollection)
17+
@Suppress("DEPRECATION")
18+
setClassDirectories(classDirectoriesCompat)
19+
}
20+
}
21+
22+
fun sourceDirectoriesFromCompat(fileCollection: FileCollection) {
23+
if (GradleVersion.current() >= GradleVersion.version("5.0.0")) {
24+
sourceDirectories.from(fileCollection)
25+
} else {
26+
sourceDirectoriesCompat = sourceDirectoriesCompat.plus(fileCollection)
27+
@Suppress("DEPRECATION")
28+
setSourceDirectories(sourceDirectoriesCompat)
29+
}
30+
}
31+
32+
fun executionDataFromCompat(fileCollection: FileCollection) {
33+
if (GradleVersion.current() >= GradleVersion.version("5.0.0")) {
34+
executionData.from(fileCollection)
35+
} else {
36+
executionData(fileCollection)
37+
}
38+
}
39+
}

plugin/src/main/kotlin/org/neotech/plugin/rootcoverage/RootCoverageModuleTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ open class RootCoverageModuleTask : DefaultTask() {
2424
@InputFiles
2525
var classDirectories: FileCollection = project.files()
2626

27-
}
27+
}

plugin/src/main/kotlin/org/neotech/plugin/rootcoverage/RootCoveragePlugin.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import org.gradle.api.GradleException
1010
import org.gradle.api.Plugin
1111
import org.gradle.api.Project
1212
import org.gradle.testing.jacoco.plugins.JacocoPlugin
13-
import org.gradle.testing.jacoco.tasks.JacocoReport
1413

1514
@Suppress("unused")
1615
class RootCoveragePlugin : Plugin<Project> {
@@ -91,7 +90,7 @@ class RootCoveragePlugin : Plugin<Project> {
9190
// Aggregates jacoco results from the app sub-project and bankingright sub-project and generates a report.
9291
// The report can be found at the root of the project in /build/reports/jacoco, so don't look in
9392
// /app/build/reports/jacoco you will only find the app sub-project report there.
94-
val task = project.tasks.create("rootCodeCoverageReport", JacocoReport::class.java)
93+
val task = project.tasks.create("rootCodeCoverageReport", JacocoReportCompat::class.java)
9594
task.group = "reporting"
9695
task.description = "Generates a Jacoco report with combined results from all the subprojects."
9796

@@ -125,7 +124,7 @@ class RootCoveragePlugin : Plugin<Project> {
125124
}
126125
}
127126

128-
private fun createCoverageTaskForSubProject(subProject: Project, task: JacocoReport) {
127+
private fun createCoverageTaskForSubProject(subProject: Project, task: JacocoReportCompat) {
129128
// Only Android Application and Android Library modules are supported for now.
130129
val extension = subProject.extensions.findByName("android")
131130
if (extension == null) {
@@ -195,8 +194,8 @@ class RootCoveragePlugin : Plugin<Project> {
195194
}
196195

197196
// Collect the class files based on the Java Compiler output
198-
val javaClassOutputs = variant.javaCompileProvider.get().outputs
199-
val javaClassTrees = javaClassOutputs.files.map { file ->
197+
val javaClassOutput = variant.javaCompileProvider.get().outputs
198+
val javaClassTrees = javaClassOutput.files.map { file ->
200199
project.fileTree(file, excludes = getFileFilterPatterns()).excludeNonClassFiles()
201200
}
202201

@@ -218,14 +217,13 @@ class RootCoveragePlugin : Plugin<Project> {
218217
return codeCoverageReportTask.get()
219218
}
220219

221-
private fun addSubTaskDependencyToRootTask(rootTask: JacocoReport, subModuleTask: RootCoverageModuleTask) {
220+
private fun addSubTaskDependencyToRootTask(rootTask: JacocoReportCompat, subModuleTask: RootCoverageModuleTask) {
222221

223222
// Make the root task depend on the sub-project code coverage task
224223
rootTask.dependsOn(subModuleTask)
225224

226-
// Add the sub-task class directories, source directories and executionData to the root task
227-
rootTask.classDirectories.from(subModuleTask.classDirectories)
228-
rootTask.sourceDirectories.from(subModuleTask.sourceDirectories)
229-
rootTask.executionData.from(subModuleTask.executionData)
225+
rootTask.classDirectoriesFromCompat(subModuleTask.classDirectories)
226+
rootTask.sourceDirectoriesFromCompat(subModuleTask.sourceDirectories)
227+
rootTask.executionDataFromCompat(subModuleTask.executionData)
230228
}
231229
}

plugin/src/main/kotlin/org/neotech/plugin/rootcoverage/RootCoveragePluginExtension.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ open class RootCoveragePluginExtension {
1212
var excludes: List<String> = mutableListOf()
1313
var skipTestExecution: Boolean = false
1414
var testTypes: List<TestVariantBuildOutput.TestType> = mutableListOf(TestVariantBuildOutput.TestType.ANDROID_TEST, TestVariantBuildOutput.TestType.UNIT)
15-
}
15+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ class CoverageReport private constructor(
3939
it.records)
4040
}
4141
}
42-
}
42+
}

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,30 @@ import org.gradle.testkit.runner.TaskOutcome
66
import org.junit.Test
77
import org.junit.runner.RunWith
88
import org.junit.runners.Parameterized
9+
import org.neotech.plugin.rootcoverage.util.SystemOutputWriter
10+
import org.neotech.plugin.rootcoverage.util.createLocalPropertiesFile
911
import java.io.File
1012
import kotlin.test.assertEquals
1113

1214
@RunWith(Parameterized::class)
1315
class IntegrationTest(
14-
private val projectRoot: File,
1516
// Used by Junit as the test name, see @Parameters
16-
@Suppress("unused") private val name: String) {
17+
@Suppress("unused") private val name: String,
18+
private val projectRoot: File,
19+
private val gradleVersion: String) {
1720

1821
@Test
1922
fun execute() {
2023
createLocalPropertiesFile(projectRoot)
2124

2225
val runner = GradleRunner.create()
2326
.withProjectDir(projectRoot)
27+
.withGradleVersion(gradleVersion)
2428
.withPluginClasspath()
25-
// Without forwardOutput travis CI could timeout because not output will be reported
26-
// for a long time.
27-
.forwardOutput()
29+
// Without forwardOutput Travis CI could timeout (which happens when Travis receives
30+
// no output for more than 10 minutes)
31+
.forwardStdOutput(SystemOutputWriter.out())
32+
.forwardStdError(SystemOutputWriter.err())
2833
.withArguments("clean", "rootCodeCoverageReport", "--stacktrace")
2934

3035
// Expect no failure
@@ -41,17 +46,19 @@ class IntegrationTest(
4146

4247
companion object {
4348

44-
// This method is used by the JVM (Parameterized JUnit Runner)
45-
@Suppress("unused")
46-
@Parameterized.Parameters(name = "{1}")
49+
@Suppress("unused") // This method is used by the JVM (Parameterized JUnit Runner)
50+
@Parameterized.Parameters(name = "{0}")
4751
@JvmStatic
4852
fun parameters(): List<Array<Any>> {
49-
return File("src/test/test-fixtures")
50-
.listFiles()
51-
.filter { it.isDirectory }
52-
.map {
53-
arrayOf(it, it.name)
54-
}
53+
54+
val testFixtures = File("src/test/test-fixtures").listFiles().filter { it.isDirectory }
55+
val gradleVersions = arrayOf("4.10.1", "5.1.1")
56+
57+
return testFixtures.flatMap { file ->
58+
gradleVersions.map { gradleVersion ->
59+
arrayOf("${file.name}-$gradleVersion", file, gradleVersion)
60+
}
61+
}
5562
}
5663
}
57-
}
64+
}

0 commit comments

Comments
 (0)