Skip to content

Commit 220f5b8

Browse files
authored
Fix Jacoco misconfiguration by changing the destination DSL (#347)
This includes making a distinction between the report types that output to a single file (CSV, XML) and those that output to a directory (HTML) (this is in line with the API provided by the Jacoco plugin itself). In the process of doing this, fix the integration test of Jacoco which didn't catch this failure...
1 parent 2645e59 commit 220f5b8

File tree

9 files changed

+225
-146
lines changed

9 files changed

+225
-146
lines changed

plugin/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Change Log
33

44
## Unreleased
55
- JUnit 5.10.3
6+
- Updates to the `jacocoOptions` DSL
7+
- Change the return type of each report type to match Jacoco expectations (html -> Directory; csv & xml -> File)
8+
- Turn off generation of csv & xml reports by default, matching Jacoco default configuration
69

710
## 1.10.2.0 (2024-07-25)
811
- JUnit 5.10.2

plugin/android-junit5/api/android-junit5.api

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public final class de/mannodermaus/gradle/plugins/junit5/AndroidJUnitPlatformPlu
55
}
66

77
public abstract class de/mannodermaus/gradle/plugins/junit5/dsl/AndroidJUnitPlatformExtension : groovy/lang/GroovyObjectSupport {
8-
public fun <init> (Lorg/gradle/api/model/ObjectFactory;)V
8+
public fun <init> (Lorg/gradle/api/Project;Lorg/gradle/api/model/ObjectFactory;)V
99
public final fun configurationParameter (Ljava/lang/String;Ljava/lang/String;)V
1010
public final fun configurationParameters (Ljava/util/Map;)V
1111
public final fun filters (Ljava/lang/String;Lorg/gradle/api/Action;)V
@@ -40,21 +40,31 @@ public abstract class de/mannodermaus/gradle/plugins/junit5/dsl/InstrumentationT
4040

4141
public abstract class de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions {
4242
public fun <init> (Lorg/gradle/api/model/ObjectFactory;)V
43-
public final fun getCsv ()Lde/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$Report;
43+
public final fun getCsv ()Lde/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$FileReport;
4444
public abstract fun getExcludedClasses ()Lorg/gradle/api/provider/ListProperty;
45-
public final fun getHtml ()Lde/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$Report;
45+
public final fun getHtml ()Lde/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$DirectoryReport;
4646
public abstract fun getOnlyGenerateTasksForVariants ()Lorg/gradle/api/provider/SetProperty;
4747
public abstract fun getTaskGenerationEnabled ()Lorg/gradle/api/provider/Property;
48-
public final fun getXml ()Lde/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$Report;
48+
public final fun getXml ()Lde/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$FileReport;
4949
}
5050

51-
public abstract class de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$Report {
51+
public abstract class de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$DirectoryReport : de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$Report {
52+
public fun <init> ()V
53+
public abstract fun getDestination ()Lorg/gradle/api/file/DirectoryProperty;
54+
public final fun invoke (Lkotlin/jvm/functions/Function1;)V
55+
}
56+
57+
public abstract class de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$FileReport : de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$Report {
5258
public fun <init> ()V
5359
public abstract fun getDestination ()Lorg/gradle/api/file/RegularFileProperty;
54-
public abstract fun getEnabled ()Lorg/gradle/api/provider/Property;
5560
public final fun invoke (Lkotlin/jvm/functions/Function1;)V
5661
}
5762

63+
public abstract class de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions$Report {
64+
public abstract fun getDestination ()Lorg/gradle/api/file/FileSystemLocationProperty;
65+
public abstract fun getEnabled ()Lorg/gradle/api/provider/Property;
66+
}
67+
5868
public abstract class de/mannodermaus/gradle/plugins/junit5/tasks/AndroidJUnit5JacocoReport : org/gradle/testing/jacoco/tasks/JacocoReport {
5969
public fun <init> ()V
6070
}

plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/AndroidJUnitPlatformExtension.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import java.io.File
1414
import javax.inject.Inject
1515

1616
public abstract class AndroidJUnitPlatformExtension @Inject constructor(
17+
project: Project,
1718
private val objects: ObjectFactory
1819
) : GroovyObjectSupport() {
1920

@@ -110,18 +111,23 @@ public abstract class AndroidJUnitPlatformExtension @Inject constructor(
110111
/**
111112
* Options for controlling Jacoco reporting
112113
*/
113-
@Suppress("CAST_NEVER_SUCCEEDS")
114114
public val jacocoOptions: JacocoOptions =
115115
objects.newInstance(JacocoOptions::class.java).apply {
116116
taskGenerationEnabled.convention(true)
117117
onlyGenerateTasksForVariants.convention(emptySet())
118118
excludedClasses.set(listOf("**/R.class", "**/R$*.class", "**/BuildConfig.*"))
119+
120+
// Just like Jacoco itself, enable only HTML by default.
121+
// We have to supply an output location for all reports though,
122+
// as keeping this unset would lead to issues
123+
// (ref. https://github.com/mannodermaus/android-junit5/issues/346)
124+
val defaultReportDir = project.layout.buildDirectory.dir("reports/jacoco")
119125
html.enabled.convention(true)
120-
html.destination.set(null as? File)
121-
csv.enabled.convention(true)
122-
csv.destination.set(null as? File)
123-
xml.enabled.convention(true)
124-
xml.destination.set(null as? File)
126+
html.destination.convention(defaultReportDir.map { it.dir("html") })
127+
csv.enabled.convention(false)
128+
csv.destination.convention(defaultReportDir.map { it.file("jacoco.csv") })
129+
xml.enabled.convention(false)
130+
xml.destination.convention(defaultReportDir.map { it.file("jacoco.xml") })
125131
}
126132

127133
public fun jacocoOptions(action: Action<JacocoOptions>) {

plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/JacocoOptions.kt

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package de.mannodermaus.gradle.plugins.junit5.dsl
22

3+
import org.gradle.api.file.DirectoryProperty
4+
import org.gradle.api.file.FileSystemLocation
5+
import org.gradle.api.file.FileSystemLocationProperty
36
import org.gradle.api.file.RegularFileProperty
47
import org.gradle.api.model.ObjectFactory
58
import org.gradle.api.provider.ListProperty
69
import org.gradle.api.provider.Property
710
import org.gradle.api.provider.SetProperty
11+
import org.gradle.api.reporting.Report.OutputType
812
import org.gradle.api.tasks.Input
913
import org.gradle.internal.enterprise.test.FileProperty
1014
import javax.inject.Inject
@@ -35,17 +39,17 @@ public abstract class JacocoOptions @Inject constructor(
3539
/**
3640
* Options for controlling the HTML Report generated by Jacoco
3741
*/
38-
public val html: Report = objects.newInstance(Report::class.java)
42+
public val html: DirectoryReport = objects.newInstance(DirectoryReport::class.java)
3943

4044
/**
4145
* Options for controlling the CSV Report generated by Jacoco
4246
*/
43-
public val csv: Report = objects.newInstance(Report::class.java)
47+
public val csv: FileReport = objects.newInstance(FileReport::class.java)
4448

4549
/**
4650
* Options for controlling the XML Report generated by Jacoco
4751
*/
48-
public val xml: Report = objects.newInstance(Report::class.java)
52+
public val xml: FileReport = objects.newInstance(FileReport::class.java)
4953

5054
/**
5155
* List of class name patterns that should be excluded from being processed by Jacoco.
@@ -54,24 +58,36 @@ public abstract class JacocoOptions @Inject constructor(
5458
@get:Input
5559
public abstract val excludedClasses: ListProperty<String>
5660

57-
public abstract class Report {
58-
59-
public operator fun invoke(config: Report.() -> Unit) {
60-
this.config()
61-
}
62-
61+
public sealed class Report {
6362
/**
6463
* Whether this report should be generated
6564
*/
6665
@get:Input
6766
public abstract val enabled: Property<Boolean>
6867

6968
/**
70-
* Name of the file to be generated; note that
69+
* Name of the file/directory to be generated; note that
7170
* due to the variant-aware nature of the plugin,
7271
* each variant will be assigned a distinct folder if necessary
7372
*/
73+
public abstract val destination: FileSystemLocationProperty<out FileSystemLocation>
74+
}
75+
76+
public abstract class DirectoryReport : Report() {
7477
@get:Input
75-
public abstract val destination: RegularFileProperty
78+
public abstract override val destination: DirectoryProperty
79+
80+
public operator fun invoke(config: DirectoryReport.() -> Unit) {
81+
this.config()
82+
}
83+
}
84+
85+
public abstract class FileReport : Report() {
86+
@get:Input
87+
public abstract override val destination: RegularFileProperty
88+
89+
public operator fun invoke(config: FileReport.() -> Unit) {
90+
this.config()
91+
}
7692
}
7793
}

0 commit comments

Comments
 (0)