-
Notifications
You must be signed in to change notification settings - Fork 5
/
jacoco.gradle
60 lines (57 loc) · 1.88 KB
/
jacoco.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
apply plugin: 'jacoco'
ext {
limits = [
'instruction': 80,
'branch' : 70,
'line' : 80,
'complexity' : 75,
'method' : 85,
'class' : 90 // TODO: 100%
]
}
jacocoTestReport {
dependsOn 'test'
reports {
xml.enabled true
}
doLast {
def report = file("${jacoco.reportsDir}/test/jacocoTestReport.xml")
def parser = new XmlParser()
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false)
def results = parser.parse(report)
def percentage = {
def covered = it.'@covered' as Double
def missed = it.'@missed' as Double
((covered / (covered + missed)) * 100).round(2)
}
def counters = results.counter
def metrics = [:]
metrics << [
'instruction': percentage(counters.find { it.'@type'.equals('INSTRUCTION') }),
'branch' : percentage(counters.find { it.'@type'.equals('BRANCH') }),
'line' : percentage(counters.find { it.'@type'.equals('LINE') }),
'complexity' : percentage(counters.find { it.'@type'.equals('COMPLEXITY') }),
'method' : percentage(counters.find { it.'@type'.equals('METHOD') }),
'class' : percentage(counters.find { it.'@type'.equals('CLASS') })
]
def failures = []
metrics.each {
def limit = limits[it.key]
if (it.value < limit) {
failures.add("- ${it.key} coverage rate is: ${it.value}%, minimum is ${limit}%")
}
}
if (failures) {
logger.error("------------------ Code Coverage Failed -----------------------")
failures.each {
logger.error(it)
}
logger.error("---------------------------------------------------------------")
throw new GradleException("Code coverage failed")
} else{
logger.debug("Passed Code Coverage Checks")
}
}
}
check.dependsOn jacocoTestReport