forked from alisonthemonster/Presently
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoveragereporting.gradle
145 lines (126 loc) · 4.24 KB
/
coveragereporting.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
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'jacoco'
jacoco {
toolVersion '0.8.2'
}
// Excluded files
def fileFilter = ['**/R.class',
'**/R$*.class',
'**/*BR*.*',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
'**/settings/*.*',
'**/SingleLiveEvent*',
'**/CustomTypefaceSpan*',
'**/*Adapter*',
'**/model/*.*',
'**/room/*.*',
'**/di/*.*',
'**/dropbox/*.*',
'**/reminders/*.*',
'**/backups/Dropbox/*.*',
'**/backups/UploadToCloudWorker*',
'**/*Binding*.*',
'**/*adapter*.*',
'**/*Activity*.*',
'**/AppLockFragment*',
'**/LocaleHelper*',
'**/TimelineFragment*',
'**/calendar/*.*',]
task jacocoTestReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {
description = 'Generate JaCoCo coverage reports'
group = 'Reporting'
def mainSrc = android.sourceSets.main.java.srcDirs
def debugTree = fileTree(
dir: "$buildDir/intermediates/classes/debug",
excludes: fileFilter
) + fileTree(
dir: "$buildDir/tmp/kotlin-classes/debug",
excludes: fileFilter
)
def testTree = fileTree(
dir: "$buildDir",
include: ['jacoco/testDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec']
)
getSourceDirectories().setFrom(mainSrc)
getClassDirectories().setFrom(debugTree)
getExecutionData().setFrom(testTree)
doFirst {
files('build/intermediates/classes/debug').getFiles().each { file ->
if (file.name.contains('$$')) {
file.renameTo(file.path.replace('$$', '$'))
}
}
}
doLast {
printReport("jacocoTestReport")
}
reports {
xml.enabled = true
html.enabled = true
}
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}
def printReport(task) {
def report = file("$buildDir/reports/${task}/${task}.xml")
logger.lifecycle("Checking reports from ${report}")
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 = {
if (it == null) {
100
} else {
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') }),
'line' : percentage(counters.find { it.'@type'.equals('LINE') }),
'method' : percentage(counters.find { it.'@type'.equals('METHOD') }),
'class' : percentage(counters.find { it.'@type'.equals('CLASS') })
]
def failures = []
def passes = []
// Fail the task if the coverage is below these numbers
def minTestCoverage = [
'instruction': 80,
'line' : 80,
'method' : 80,
'class' : 80
]
metrics.each {
def limit = minTestCoverage[it.key]
if (it.value < limit) {
failures.add("- ${it.key} coverage rate is ${it.value}%, the minimum is ${limit}")
} else {
passes.add("- ${it.key} coverage rate is ${it.value}%")
}
}
if (failures) {
logger.log(LogLevel.ERROR, "\n\n CODE COVERAGE FAILED!!!")
failures.forEach {
logger.quiet(it)
}
passes.each {
logger.quiet(it)
}
throw new GradleException("Code coverage failed")
} else {
logger.quiet("\n\n Code coverage passed!")
passes.each {
logger.quiet(it)
}
}
}