-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjacoco.config.gradle
220 lines (195 loc) · 8.23 KB
/
jacoco.config.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// filter of files that shouldn't be part of the report
def androidFileFilter =
[ //jdk
'jdk.internal.*',
// data binding
'**/databinding/*.class',
'**/BR.*',
// android
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
// kotlin
'**/META-INF/*',
'**/*MapperImpl*.*',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/BuildConfig.*',
'**/*Component*.*',
'**/*BR*.*',
'**/Manifest*.*',
'**/*$Lambda$*.*',
'**/*Companion*.*',
'**/*Module*.*',
'**/*Dagger*.*',
'**/*Hilt*.*',
'**/*MembersInjector*.*',
'**/*_MembersInjector.class',
'**/*_Factory*.*',
'**/*_Provide*Factory*.*',
'**/*Extensions*.*',
// sealed and data classes
'**/*$Result.*',
'**/*$Result$*.*',
// adapters generated by moshi
'**/*JsonAdapter.*',
// room
'**/*_Impl.class',
'**/*_Impl*.*',
// hilt
'**/hilt_aggregated_deps/*'
]
/ **
* Setup Jacoco for Android module.
* - applies Plugin to the module.
* - sets up the "debug" build type for jacoco
* - creates tasks:
* - if androidTest folder exists creates a jacocoAndroidTestReport alias for the jacoco coverageReport, otherwise an empty task.
* - if test folder exists creates a jacocoUnitTestReport alias for the jacoco coverageReport, otherwise an empty task.
* - creates jacocoTestReport which runs both alias tasks created before.
*
* Note: "jacocoTestReport" is the task name which is default for Java Modules.
* /
def setupAndroidJacoco(Project module, ArrayList<String> fileFilter, String jacocoVersion) {
configure(module) {
apply plugin: "jacoco"
module.android.testOptions.unitTests.all {
jacoco.includeNoLocationClasses = true
jacoco.excludes = fileFilter
}
// on API 21 enableAndroidTestCoverage makes the tests crash with resource not found issue
def disableAndroidTestCoverage = findProperty("disableAndroidTestCoverage") ?: false
android.buildTypes.debug.enableAndroidTestCoverage = !disableAndroidTestCoverage
android.buildTypes.debug.enableUnitTestCoverage = true
jacoco.toolVersion = "$jacocoVersion"
def hasAndroidTests = new File("${module.projectDir}/src/androidTest").exists()
def hasUnitTests = new File("${module.projectDir}/src/test").exists()
if (hasUnitTests) {
task jacocoUnitTestReport(dependsOn: ["createDebugUnitTestCoverageReport"]) {
group = "verification"
}
} else {
task jacocoUnitTestReport() {
group = "verification"
}
}
if (hasAndroidTests) {
task jacocoAndroidTestReport(dependsOn: ["createDebugAndroidTestCoverageReport"]) {
group = "verification"
}
} else {
task jacocoAndroidTestReport() {
group = "verification"
}
}
task jacocoTestReport(dependsOn: ["jacocoUnitTestReport", "jacocoAndroidTestReport"]) {
group = "verification"
}
}
}
/ **
* Setup Jacoco for Java module.
* - applies Plugin to the module.
* - updates the `jacocoTestReport` task to ignore the `androidFileFilter`
* - ensures tests run before `jacocoTestReport`
* /
def setupJavaJacoco(Project module, ArrayList<String> fileFilter) {
configure(module) {
apply plugin: "jacoco"
jacocoTestReport {
dependsOn test // tests are required to run before generating the report
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it, exclude: fileFilter)
}))
}
}
}
}
/ **
* Setup Jacoco for submodules based on their android or java module type
*/
subprojects { module ->
plugins.withType(JavaPlugin).whenPluginAdded {
setupJavaJacoco(module, androidFileFilter)
}
plugins.withId("com.android.application") {
setupAndroidJacoco(module, androidFileFilter, "$jacoco_version")
}
plugins.withId("com.android.library") {
setupAndroidJacoco(module, androidFileFilter, "$jacoco_version")
}
}
/ **
* Setup Aggregation tasks for Jacoco.
* - jacocoRootReport: can be used to generate the report after submodules `jacocoTestReport` has been ran at least once.
* - runTestAndJacocoRootReport: calls the `jacocoTestReport` of each submodule then calls `jacocoRootReport` for aggregation.
*
* Context, how the aggregated report works:
* The jacoco tasks created by the plugin generate .ec and .exec Execution-Data files in specific locations.
* - These Execution-Data files are all pulled into one `JacocoReport` task (`executionData.from`).
* - All the source files from all the submodules are pulled into the same `JacocoReport` task (`sourceDirectories.from`)
* - All the class files from all the submodules are pulled into the same `JacocoReport` task (`classDirectories.from`)
* Then finally the report is configured to be generated at root `build\coverage-report`
* /
configure(rootProject) {
apply plugin: "jacoco"
def testTypeName = "debug"
task runTestAndJacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
description = 'Run Tests and Generates report from all subprojects'
// add all non empty subProjects `jacocoTestReport` task as a dependency.
// note: these tasks are default Jacoco Task for Java and have been added above for Android modules.
def codeProjects = subprojects.findAll({ it.subprojects.isEmpty() })
codeProjects.forEach {
dependsOn += ["$it.path:jacocoTestReport"]
}
finalizedBy("jacocoRootReport")
}
task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
description = 'Generates report from all subprojects'
def codeProjects = subprojects.findAll({ it.subprojects.isEmpty() })
sourceDirectories.from = files(codeProjects.collect { "${it.projectDir}/src/main/java" })
def classFileTrees = codeProjects.collect {
def javaClassFilesInJavaModuleTree = fileTree(
dir: "${it.buildDir}/classes/java/main",
excludes: androidFileFilter
)
def kotlinClassFilesInJavaModuleTree = fileTree(
dir: "${it.buildDir}/classes/kotlin/main",
excludes: androidFileFilter
)
def javaClassFilesInAndroidModuleTree = fileTree(
dir: "${it.buildDir}/intermediates/javac/${testTypeName}/classes",
excludes: androidFileFilter
)
def kotlinClassFilesInAndroidModuleTree = fileTree(
dir: "${it.buildDir}/tmp/kotlin-classes/${testTypeName}",
excludes: androidFileFilter
)
files([javaClassFilesInJavaModuleTree, kotlinClassFilesInJavaModuleTree, javaClassFilesInAndroidModuleTree, kotlinClassFilesInAndroidModuleTree])
}.flatten()
classDirectories.from = files(classFileTrees)
def executionDataFiles = codeProjects.collect {
def androidTestExecutionData = fileTree(
dir: "${it.buildDir}/outputs/code_coverage/${testTypeName}AndroidTest/connected/",
includes: ["**/*.ec", "**/*.exec"]
)
def androidUnitTestExecutionData = fileTree(
dir: "${it.buildDir}/outputs/unit_test_code_coverage/",
includes: ["**/*.ec", "**/*.exec"]
)
def javaUnitTestExecutionData = "${it.buildDir}/jacoco/test.exec"
[androidTestExecutionData, androidUnitTestExecutionData, javaUnitTestExecutionData]
}.flatten()
executionData.from = files(executionDataFiles)
reports {
html {
required = true
destination file("${buildDir}/coverage-report")
}
}
}
}