@@ -20,7 +20,7 @@ def __init__(self, coverage):
20
20
self .config = self .coverage .config
21
21
22
22
def report (self , morfs , outfile = None ):
23
- """Renders the full lcov report
23
+ """Renders the full lcov report.
24
24
25
25
'morfs' is a list of modules or filenames
26
26
@@ -34,41 +34,42 @@ def report(self, morfs, outfile=None):
34
34
self .get_lcov (fr , analysis , outfile )
35
35
36
36
def get_lcov (self , fr , analysis , outfile = None ):
37
- """Produces the lcov data for a single file
37
+ """Produces the lcov data for a single file.
38
38
39
- get_lcov currently supports both line and branch coverage,
39
+ This currently supports both line and branch coverage,
40
40
however function coverage is not supported.
41
-
42
41
"""
43
-
44
42
outfile .write ("TN:\n " )
45
43
outfile .write (f"SF:{ fr .relative_filename ()} \n " )
46
44
source_lines = fr .source ().splitlines ()
45
+
47
46
for covered in sorted (analysis .executed ):
48
- # Note: Coveragepy currently only supports checking *if* a line has
49
- # been executed, not how many times, so we set this to 1 for nice
50
- # output even if it's technically incorrect
51
-
52
- # The lines below calculate a 64 bit encoded md5 hash of the line
53
- # corresponding to the DA lines in the lcov file,
54
- # for either case of the line being covered or missed in Coveragepy
55
- # The final two characters of the encoding ("==") are removed from
56
- # the hash to allow genhtml to run on the resulting lcov file
47
+ # Note: Coverage.py currently only supports checking *if* a line
48
+ # has been executed, not how many times, so we set this to 1 for
49
+ # nice output even if it's technically incorrect.
50
+
51
+ # The lines below calculate a 64- bit encoded md5 hash of the line
52
+ # corresponding to the DA lines in the lcov file, for either case
53
+ # of the line being covered or missed in coverage.py. The final two
54
+ # characters of the encoding ("==") are removed from the hash to
55
+ # allow genhtml to run on the resulting lcov file.
57
56
if source_lines :
58
- line = source_lines [covered - 1 ].encode ("utf-8" )
57
+ line = source_lines [covered - 1 ].encode ("utf-8" )
59
58
else :
60
59
line = b""
61
- hashed = str ( base64 .b64encode (md5 (line ).digest ())[: - 2 ], encoding = "utf-8 " )
60
+ hashed = base64 .b64encode (md5 (line ).digest ()). decode (). rstrip ( "= " )
62
61
outfile .write (f"DA:{ covered } ,1,{ hashed } \n " )
62
+
63
63
for missed in sorted (analysis .missing ):
64
64
assert source_lines
65
65
line = source_lines [missed - 1 ].encode ("utf-8" )
66
- hashed = str ( base64 .b64encode (md5 (line ).digest ())[: - 2 ], encoding = "utf-8 " )
66
+ hashed = base64 .b64encode (md5 (line ).digest ()). decode (). rstrip ( "= " )
67
67
outfile .write (f"DA:{ missed } ,0,{ hashed } \n " )
68
+
68
69
outfile .write (f"LF:{ len (analysis .statements )} \n " )
69
70
outfile .write (f"LH:{ len (analysis .executed )} \n " )
70
71
71
- # More information dense branch coverage data
72
+ # More information dense branch coverage data.
72
73
missing_arcs = analysis .missing_branch_arcs ()
73
74
executed_arcs = analysis .executed_branch_arcs ()
74
75
for block_number , block_line_number in enumerate (
@@ -78,22 +79,23 @@ def get_lcov(self, fr, analysis, outfile=None):
78
79
sorted (missing_arcs [block_line_number ])
79
80
):
80
81
# The exit branches have a negative line number,
81
- # this will not produce valid lcov, and so setting
82
+ # this will not produce valid lcov. Setting
82
83
# the line number of the exit branch to 0 will allow
83
- # for valid lcov, while preserving the data
84
+ # for valid lcov, while preserving the data.
84
85
line_number = max (line_number , 0 )
85
86
outfile .write (f"BRDA:{ line_number } ,{ block_number } ,{ branch_number } ,-\n " )
87
+
86
88
# The start value below allows for the block number to be
87
89
# preserved between these two for loops (stopping the loop from
88
- # resetting the value of the block number to 0)
90
+ # resetting the value of the block number to 0).
89
91
for branch_number , line_number in enumerate (
90
92
sorted (executed_arcs [block_line_number ]),
91
93
start = len (missing_arcs [block_line_number ]),
92
94
):
93
95
line_number = max (line_number , 0 )
94
96
outfile .write (f"BRDA:{ line_number } ,{ block_number } ,{ branch_number } ,1\n " )
95
97
96
- # Summary of the branch coverage
98
+ # Summary of the branch coverage.
97
99
if analysis .has_arcs ():
98
100
branch_stats = analysis .branch_stats ()
99
101
brf = sum (t for t , k in branch_stats .values ())
0 commit comments