Skip to content

Commit 2e8c191

Browse files
committed
style: cleanups after lcov, though more than just lcov
1 parent 3f221e0 commit 2e8c191

File tree

4 files changed

+172
-202
lines changed

4 files changed

+172
-202
lines changed

CHANGES.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Unreleased
2323
----------
2424

2525
- Feature: Added the `lcov` command to generate reports in LCOV format.
26-
Thanks, Bradley Burns.
26+
Thanks, Bradley Burns. Closes `issue 587`_ and `issue 626`_.
2727

2828
- Dropped support for Python 3.6, which reached end-of-life on 2021-12-23.
2929

@@ -39,6 +39,8 @@ Unreleased
3939

4040
- Releases now have MacOS arm64 wheels for Apple Silicon (fixes `issue 1288`_).
4141

42+
.. _issue 587: https://github.com/nedbat/coveragepy/issues/587
43+
.. _issue 626: https://github.com/nedbat/coveragepy/issues/626
4244
.. _issue 883: https://github.com/nedbat/coveragepy/issues/883
4345
.. _issue 1288: https://github.com/nedbat/coveragepy/issues/1288
4446
.. _issue 1294: https://github.com/nedbat/coveragepy/issues/1294

coverage/cmdline.py

+24-25
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ class Opts:
123123
metavar="OUTFILE",
124124
help="Write the JSON report to this file. Defaults to 'coverage.json'",
125125
)
126+
output_lcov = optparse.make_option(
127+
'-o', '', action='store', dest='outfile',
128+
metavar="OUTFILE",
129+
help="Write the LCOV report to this file. Defaults to 'coverage.lcov'",
130+
)
126131
json_pretty_print = optparse.make_option(
127132
'', '--pretty-print', action='store_true',
128133
help="Format the JSON for human readers.",
129134
)
130-
lcov = optparse.make_option(
131-
'-o', '', action='store', dest='outfile',
132-
metavar="OUTFILE",
133-
help="Write the LCOV report to this file. Defaults to 'coverage.lcov'"
134-
)
135135
parallel_mode = optparse.make_option(
136136
'-p', '--parallel-mode', action='store_true',
137137
help=(
@@ -423,7 +423,21 @@ def get_prog_name(self):
423423
Opts.show_contexts,
424424
] + GLOBAL_ARGS,
425425
usage="[options] [modules]",
426-
description="Generate a JSON report of coverage results."
426+
description="Generate a JSON report of coverage results.",
427+
),
428+
429+
'lcov': CmdOptionParser(
430+
"lcov",
431+
[
432+
Opts.fail_under,
433+
Opts.ignore_errors,
434+
Opts.include,
435+
Opts.output_lcov,
436+
Opts.omit,
437+
Opts.quiet,
438+
] + GLOBAL_ARGS,
439+
usage="[options] [modules]",
440+
description="Generate an LCOV report of coverage results.",
427441
),
428442

429443
'report': CmdOptionParser(
@@ -442,7 +456,7 @@ def get_prog_name(self):
442456
Opts.skip_empty,
443457
] + GLOBAL_ARGS,
444458
usage="[options] [modules]",
445-
description="Report coverage statistics on modules."
459+
description="Report coverage statistics on modules.",
446460
),
447461

448462
'run': CmdOptionParser(
@@ -461,7 +475,7 @@ def get_prog_name(self):
461475
Opts.timid,
462476
] + GLOBAL_ARGS,
463477
usage="[options] <pyfile> [program options]",
464-
description="Run a Python program, measuring code execution."
478+
description="Run a Python program, measuring code execution.",
465479
),
466480

467481
'xml': CmdOptionParser(
@@ -476,22 +490,8 @@ def get_prog_name(self):
476490
Opts.skip_empty,
477491
] + GLOBAL_ARGS,
478492
usage="[options] [modules]",
479-
description="Generate an XML report of coverage results."
493+
description="Generate an XML report of coverage results.",
480494
),
481-
482-
'lcov': CmdOptionParser(
483-
"lcov",
484-
[
485-
Opts.fail_under,
486-
Opts.ignore_errors,
487-
Opts.include,
488-
Opts.lcov,
489-
Opts.omit,
490-
Opts.quiet,
491-
] + GLOBAL_ARGS,
492-
usage="[options] [modules]",
493-
description="Generate an LCOV report of coverage results."
494-
)
495495
}
496496

497497

@@ -681,7 +681,6 @@ def command_line(self, argv):
681681
outfile=options.outfile,
682682
**report_args
683683
)
684-
685684
else:
686685
# There are no other possible actions.
687686
raise AssertionError
@@ -876,10 +875,10 @@ def unglob_args(args):
876875
help Get help on using coverage.py.
877876
html Create an HTML report.
878877
json Create a JSON report of coverage results.
878+
lcov Create an LCOV report of coverage results.
879879
report Report coverage stats on modules.
880880
run Run a Python program and measure code execution.
881881
xml Create an XML report of coverage results.
882-
lcov Create an LCOV report of coverage results.
883882
884883
Use "{program_name} help <command>" for detailed help on any command.
885884
""",

coverage/lcovreport.py

+24-22
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self, coverage):
2020
self.config = self.coverage.config
2121

2222
def report(self, morfs, outfile=None):
23-
"""Renders the full lcov report
23+
"""Renders the full lcov report.
2424
2525
'morfs' is a list of modules or filenames
2626
@@ -34,41 +34,42 @@ def report(self, morfs, outfile=None):
3434
self.get_lcov(fr, analysis, outfile)
3535

3636
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.
3838
39-
get_lcov currently supports both line and branch coverage,
39+
This currently supports both line and branch coverage,
4040
however function coverage is not supported.
41-
4241
"""
43-
4442
outfile.write("TN:\n")
4543
outfile.write(f"SF:{fr.relative_filename()}\n")
4644
source_lines = fr.source().splitlines()
45+
4746
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.
5756
if source_lines:
58-
line = source_lines[covered - 1].encode("utf-8")
57+
line = source_lines[covered-1].encode("utf-8")
5958
else:
6059
line = b""
61-
hashed = str(base64.b64encode(md5(line).digest())[:-2], encoding="utf-8")
60+
hashed = base64.b64encode(md5(line).digest()).decode().rstrip("=")
6261
outfile.write(f"DA:{covered},1,{hashed}\n")
62+
6363
for missed in sorted(analysis.missing):
6464
assert source_lines
6565
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("=")
6767
outfile.write(f"DA:{missed},0,{hashed}\n")
68+
6869
outfile.write(f"LF:{len(analysis.statements)}\n")
6970
outfile.write(f"LH:{len(analysis.executed)}\n")
7071

71-
# More information dense branch coverage data
72+
# More information dense branch coverage data.
7273
missing_arcs = analysis.missing_branch_arcs()
7374
executed_arcs = analysis.executed_branch_arcs()
7475
for block_number, block_line_number in enumerate(
@@ -78,22 +79,23 @@ def get_lcov(self, fr, analysis, outfile=None):
7879
sorted(missing_arcs[block_line_number])
7980
):
8081
# 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
8283
# 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.
8485
line_number = max(line_number, 0)
8586
outfile.write(f"BRDA:{line_number},{block_number},{branch_number},-\n")
87+
8688
# The start value below allows for the block number to be
8789
# 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).
8991
for branch_number, line_number in enumerate(
9092
sorted(executed_arcs[block_line_number]),
9193
start=len(missing_arcs[block_line_number]),
9294
):
9395
line_number = max(line_number, 0)
9496
outfile.write(f"BRDA:{line_number},{block_number},{branch_number},1\n")
9597

96-
# Summary of the branch coverage
98+
# Summary of the branch coverage.
9799
if analysis.has_arcs():
98100
branch_stats = analysis.branch_stats()
99101
brf = sum(t for t, k in branch_stats.values())

0 commit comments

Comments
 (0)