-
Notifications
You must be signed in to change notification settings - Fork 1
/
coverage.gradle
55 lines (51 loc) · 1.63 KB
/
coverage.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
apply plugin: 'jacoco'
jacoco {
toolVersion = '0.8.1'
}
ext.limits = [
'line' : 80,
'complexity' : 67
]
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 << [
'line' : percentage(counters.find { it.'@type' == 'LINE' }),
'complexity' : percentage(counters.find { it.'@type' == 'COMPLEXITY' }),
]
def failures = []
metrics.each {
def limitProperty = "limits.$it.key"
def limit = project.hasProperty(limitProperty) ? Double.valueOf(project.property(limitProperty)) : 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