-
Notifications
You must be signed in to change notification settings - Fork 47
/
JacocoReports.kt
197 lines (178 loc) · 6.82 KB
/
JacocoReports.kt
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
/*
* Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.kover.engines.jacoco
import groovy.lang.*
import kotlinx.kover.api.*
import kotlinx.kover.engines.commons.*
import kotlinx.kover.engines.commons.ONE_HUNDRED
import kotlinx.kover.engines.commons.ReportVerificationRule
import kotlinx.kover.tasks.*
import org.gradle.api.*
import org.gradle.api.file.*
import org.gradle.internal.reflect.*
import java.io.*
import java.math.*
import java.util.*
internal fun Task.jacocoReport(
projectFiles: Map<String, ProjectFiles>,
filters: KoverClassFilter,
xmlFile: File?,
htmlDir: File?,
classpath: FileCollection
) {
callJacocoAntReportTask(projectFiles, filters, classpath) {
if (xmlFile != null) {
xmlFile.parentFile.mkdirs()
invokeMethod("xml", mapOf("destfile" to xmlFile))
}
if (htmlDir != null) {
htmlDir.mkdirs()
invokeMethod("html", mapOf("destdir" to htmlDir))
}
}
}
internal fun Task.jacocoVerification(
projectFiles: Map<String, ProjectFiles>,
filters: KoverClassFilter,
rules: List<ReportVerificationRule>,
classpath: FileCollection
): String? {
callJacocoAntReportTask(projectFiles, filters, classpath) {
invokeWithBody("check", mapOf("failonviolation" to "false", "violationsproperty" to "jacocoErrors")) {
rules.forEach {
val entityType = when(it.target) {
VerificationTarget.ALL -> "BUNDLE"
VerificationTarget.CLASS -> "CLASS"
VerificationTarget.PACKAGE -> "PACKAGE"
}
invokeWithBody("rule", mapOf("element" to entityType)) {
it.bounds.forEach { b ->
val limitArgs = mutableMapOf<String, String>()
limitArgs["counter"] = when(b.metric) {
CounterType.LINE -> "LINE"
CounterType.INSTRUCTION -> "INSTRUCTION"
CounterType.BRANCH -> "BRANCH"
}
var min: BigDecimal? = b.minValue
var max: BigDecimal? = b.maxValue
when (b.valueType) {
VerificationValueType.COVERED_COUNT -> {
limitArgs["value"] = "COVEREDCOUNT"
}
VerificationValueType.MISSED_COUNT -> {
limitArgs["value"] = "MISSEDCOUNT"
}
VerificationValueType.COVERED_PERCENTAGE -> {
limitArgs["value"] = "COVEREDRATIO"
min = min?.divide(ONE_HUNDRED)
max = max?.divide(ONE_HUNDRED)
}
VerificationValueType.MISSED_PERCENTAGE -> {
limitArgs["value"] = "MISSEDRATIO"
min = min?.divide(ONE_HUNDRED)
max = max?.divide(ONE_HUNDRED)
}
}
if (min != null) {
limitArgs["minimum"] = min.toPlainString()
}
if (max != null) {
limitArgs["maximum"] = max.toPlainString()
}
invokeMethod("limit", limitArgs)
}
}
}
}
}
return ant.violations?.orderViolations()
}
private fun Task.callJacocoAntReportTask(
projectFiles: Map<String, ProjectFiles>,
filters: KoverClassFilter,
classpath: FileCollection,
block: GroovyObject.() -> Unit
) {
val builder = ant
builder.invokeMethod(
"taskdef",
mapOf(
"name" to "jacocoReport",
"classname" to "org.jacoco.ant.ReportTask",
"classpath" to classpath.asPath
)
)
val sources: MutableList<File> = mutableListOf()
val outputs: MutableList<File> = mutableListOf()
val binaries: MutableList<File> = mutableListOf()
projectFiles.forEach { pf ->
binaries += pf.value.binaryReportFiles
sources += pf.value.sources
outputs += pf.value.outputs
}
val filteredOutput = if (filters.excludes.isNotEmpty() || filters.includes.isNotEmpty()) {
val excludeRegexes = filters.excludes.map { Regex(it.wildcardsToClassFileRegex()) }
val includeRegexes = filters.includes.map { Regex(it.wildcardsToClassFileRegex()) }
val trees = outputs.map {
project.fileTree(it).filter { file ->
// the `canonicalPath` is used because a `File.separatorChar` was used to construct the class-file regex
val path = file.canonicalPath
// if the inclusion rules are declared, then the file must fit at least one of them
(includeRegexes.isEmpty() || includeRegexes.any { regex -> path.matches(regex) })
// if the exclusion rules are declared, then the file should not fit any of them
&& excludeRegexes.none { regex -> path.matches(regex) }
}
}
project.files(trees)
} else {
project.files(outputs)
}
builder.invokeWithBody("jacocoReport") {
invokeWithBody("executiondata") {
project.files(binaries).addToAntBuilder(this, "resources")
}
invokeWithBody("structure", mapOf("name" to project.path)) {
invokeWithBody("sourcefiles") {
project.files(sources).addToAntBuilder(this, "resources")
}
invokeWithBody("classfiles") {
filteredOutput.addToAntBuilder(this, "resources")
}
}
block()
}
}
private val GroovyObject.violations: String?
get() {
val project = getProperty("project")
val properties = JavaMethod.of(
project,
Hashtable::class.java, "getProperties"
).invoke(project, *arrayOfNulls(0))
return properties["jacocoErrors"] as String?
}
private fun String.orderViolations(): String {
val treeSet = TreeSet<String>()
this.lineSequence().forEach { treeSet += it }
return treeSet.joinToString("\n")
}
@Suppress("UNUSED_PARAMETER")
private inline fun GroovyObject.invokeWithBody(
name: String,
args: Map<String, String> = emptyMap(),
crossinline body: GroovyObject.() -> Unit
) {
invokeMethod(
name,
listOf(
args,
object : Closure<Any?>(this) {
fun doCall(ignore: Any?): Any? {
this@invokeWithBody.body()
return null
}
}
)
)
}