-
Notifications
You must be signed in to change notification settings - Fork 47
/
JacocoReports.kt
149 lines (135 loc) · 4.62 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
/*
* Copyright 2017-2021 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.Report
import org.gradle.api.*
import org.gradle.api.file.*
import org.gradle.internal.reflect.*
import java.io.*
import java.math.*
import java.util.*
private fun Task.callJacocoAntReportTask(
report: Report,
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()
report.projects.forEach { projectInfo ->
sources.addAll(projectInfo.sources)
outputs.addAll(projectInfo.outputs)
}
builder.invokeWithBody("jacocoReport") {
invokeWithBody("executiondata") {
project.files(report.files).addToAntBuilder(this, "resources")
}
invokeWithBody("structure", mapOf("name" to project.path)) {
invokeWithBody("sourcefiles") {
project.files(sources).addToAntBuilder(this, "resources")
}
invokeWithBody("classfiles") {
project.files(outputs).addToAntBuilder(this, "resources")
}
}
block()
}
}
internal fun Task.jacocoReport(
report: Report,
xmlFile: File?,
htmlDir: File?,
classpath: FileCollection
) {
callJacocoAntReportTask(report, 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(
report: Report,
rules: Iterable<VerificationRule>,
classpath: FileCollection
) {
callJacocoAntReportTask(report, classpath) {
invokeWithBody("check", mapOf("failonviolation" to "false", "violationsproperty" to "jacocoErrors")) {
rules.forEach {
invokeWithBody("rule", mapOf("element" to "BUNDLE")) {
it.bounds.forEach { b ->
val limitArgs = mutableMapOf("counter" to "LINE")
var min: BigDecimal? = b.minValue?.toBigDecimal()
var max: BigDecimal? = b.maxValue?.toBigDecimal()
when (b.valueType) {
VerificationValueType.COVERED_LINES_COUNT -> {
limitArgs["value"] = "COVEREDCOUNT"
}
VerificationValueType.MISSED_LINES_COUNT -> {
limitArgs["value"] = "MISSEDCOUNT"
}
VerificationValueType.COVERED_LINES_PERCENTAGE -> {
limitArgs["value"] = "COVEREDRATIO"
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)
}
}
}
}
}
ant.violations?.let { throw GradleException(it) }
}
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?
}
@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
}
}
)
)
}