forked from meganz/android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoverage_report.py
121 lines (100 loc) · 3.66 KB
/
coverage_report.py
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
'''
Usage:
This command must have 1 parameter of csv format coverage report generated by JaCoCO
python3 coverage_report.py ${WORKSPACE}/domain/build/reports/jacoco/test/jacocoTestReport.csv
Output example:
1.20%=2079/173673
'''
import os
import sys
import re
# below class or package name pattern should be excluded from coverage calculation
exclude_patterns = [
# data binding
'android/databinding/.+/.+\.class',
'.*/android/databinding/.*Binding\.class',
'.*/android/databinding/.*',
'.*/androidx/databinding/.*',
'.*/BR\..*',
# android
'.*/R\.class',
'.*/R\$*\.class',
'.*/BuildConfig\..*',
'.*/Manifest.*\..*',
'.*/.*Test.*\..*',
'android/.+/.*\..*',
# dagger
'.*/.*_MembersInjector\.class',
'.*/Dagger.*Component\.class',
'.*/Dagger.*Component\$Builder\.class',
'.*/Dagger.*Subcomponent.*\.class',
'.*/.*Subcomponent\$Builder\.class',
'.*/.*Module_.*Factory\.class',
'.*/di/module/.*',
'.*/.*_Factory.*\..*',
'.*/.*Module.*\..*',
'.*/.*Dagger.*\..*',
'.*/.*Hilt.*\..*',
# kotlin
'.*/.*MapperImpl.*\..*',
'.*/.*\$ViewInjector.*\..*',
'.*/.*\$ViewBinder.*\..*',
'.*/BuildConfig\..*',
'.*/.*Component.*\..*',
'.*/.*BR.*\..*',
'.*/Manifest.*\..*',
'.*/*$Lambda$.*\..*',
# '.*/.*Companion.*\..*',
'.*/.*Module.*\..*',
'.*/.*Dagger.*\..*',
'.*/.*Hilt.*\..*',
'.*/.*MembersInjector.*\..*',
'.*/.*_MembersInjector\.class',
'.*/.*_Factory.*\..*',
'.*/.*_Provide*Factory.*\..*',
# '.*/.*Extensions.*\..*',
# sealed and data classes
'.*/.*\$Result.*',
'.*/.*\$Result\$.*\..*',
# adapters generated by moshi
'.*/.*JsonAdapter\..*',
#entity in domain layer
'.*/domain/entity/.*',
# model in data layer
'.*/data/model/.*',
]
def exclude(package_name, class_name):
full_class_name = package_name + "/" + class_name
for pattern in exclude_patterns:
# print(pattern)
if re.match(pattern, full_class_name):
# print("exclude " + full_class_name)
return True
return False
if len(sys.argv) < 2:
print("[ERROR] Coverage parameter missing. Coverage report is needed!")
exit(0)
coverage_csv_file = sys.argv[1]
if not os.path.exists(coverage_csv_file):
print("Internal error, Coverage Report file does not exist!")
exit(0)
is_first_line = True
file_handle = open(coverage_csv_file, 'r')
lines = file_handle.readlines()
total_covered_lines = 0
total_lines = 0
for line in lines:
if is_first_line:
is_first_line = False
continue
values = line.split(",")
package_name = values[1]
class_name = values[2]
line_missed = values[7]
line_covered = values[8]
if not exclude(package_name, class_name):
total_covered_lines += int(line_covered)
total_lines += (int(line_covered) + int(line_missed))
# This outputs a String in this format:
# 1.20%=2079/173673
print("{:.2f}%={}/{}".format(float(total_covered_lines)/float(total_lines) * 100, total_covered_lines, total_lines))