-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjacoco_xml_to_lcov.py
88 lines (87 loc) · 3.33 KB
/
jacoco_xml_to_lcov.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
#
# Copyright (C) 2022-2023 Red Hat, Inc. (https://github.com/Commonjava/indy-tracking-service)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Converts Jacoco XML to LCOV.
- Reads Jacoco XML from stdin
- Resolves classes to sourcefiles
- Writes LCOV tracefile to stdout
"""
import itertools as IT
import os
import sys
import xml.etree.ElementTree as ET
def main():
test_name = sys.argv[1]
eligible_list_file = sys.argv[2]
# we're using a filename -> directory path because it makes resolving to
# sourcefiles very fast
filetree = {}
with open(eligible_list_file) as fh:
for p in fh:
print(p)
p = p.strip()
name = os.path.basename(p)
dirpath = os.path.dirname(p)
print(f'name={name}')
print(f'dirpath={dirpath}')
if name in filetree:
filetree[name].add(dirpath)
else:
filetree[name] = set([dirpath])
root = ET.parse(sys.stdin).getroot()
# we're going to build a nest map like package -> (file -> path/coverage info)
# this makes it easy to do package or file level sorting for output
data = {}
for pkg in root.iter('package'):
pkg_name = pkg.get('name')
for sfile in pkg.iter('sourcefile'):
sfile_name = sfile.get('name')
if sfile_name in filetree:
# if there's a path with the package/class as a suffix, then that's the
# real sourcefile
matches = list(
filter(lambda path: path.endswith(pkg_name),
filetree[sfile_name]))
if matches:
# the directory path upto the package portion
path = matches[0][0:matches[0].find(pkg_name)]
# the list of instrumented line numbers
instrumented = [int(line.get('nr')) for line in sfile.iter('line')]
# the set of covered line numbers
covered = set(
int(line.get('nr'))
for line in sfile.iter('line')
if line.get('ci') != '0')
if pkg_name not in data:
data[pkg_name] = {}
data[pkg_name][sfile_name] = {
'path': path,
'instrumented': instrumented,
'covered': covered,
}
for pkg_name in data:
for sfile_name in data[pkg_name]:
filepath = os.path.join(data[pkg_name][sfile_name]['path'], pkg_name,
sfile_name)
sys.stdout.write('TN:{}\n'.format(test_name))
sys.stdout.write('SF:{}\n'.format(filepath))
for line_num in data[pkg_name][sfile_name]['instrumented']:
# format is DA:{line number},{number of hits}, but we don't care about
# detailed hit numbers so just use 1 for covered lines
sys.stdout.write('DA:{},{}\n'.format(
line_num, int(line_num in data[pkg_name][sfile_name]['covered'])))
sys.stdout.write('end_of_record\n')
if __name__ == '__main__':
main()