diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2023-05-13 12:57:14 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2023-05-13 12:57:14 -0400 |
commit | 5d149f14b45e6d9e5874b91d7063aa9ed3368a14 (patch) | |
tree | 4f48afe55e3752fedc292bc18856c323cc03cbdb /coverage/lcovreport.py | |
parent | 3137ebdb31ece9ab926ac8c0760f2f2e2b67600c (diff) | |
download | python-coveragepy-git-5d149f14b45e6d9e5874b91d7063aa9ed3368a14.tar.gz |
style: a bit of refactoring for lcov code
Diffstat (limited to 'coverage/lcovreport.py')
-rw-r--r-- | coverage/lcovreport.py | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/coverage/lcovreport.py b/coverage/lcovreport.py index a4ad9cd9..8e3a5768 100644 --- a/coverage/lcovreport.py +++ b/coverage/lcovreport.py @@ -5,9 +5,9 @@ from __future__ import annotations -import sys import base64 -from hashlib import md5 +import hashlib +import sys from typing import IO, Iterable, Optional, TYPE_CHECKING @@ -20,6 +20,12 @@ if TYPE_CHECKING: from coverage import Coverage +def line_hash(line: str) -> str: + """Produce a hash of a source line for use in the LCOV file.""" + hashed = hashlib.md5(line.encode("utf-8")).digest() + return base64.b64encode(hashed).decode("ascii").rstrip("=") + + class LcovReporter: """A reporter for writing LCOV coverage reports.""" @@ -68,17 +74,15 @@ class LcovReporter: # characters of the encoding ("==") are removed from the hash to # allow genhtml to run on the resulting lcov file. if source_lines: - line = source_lines[covered-1].encode("utf-8") + line = source_lines[covered-1] else: - line = b"" - hashed = base64.b64encode(md5(line).digest()).decode().rstrip("=") - outfile.write(f"DA:{covered},1,{hashed}\n") + line = "" + outfile.write(f"DA:{covered},1,{line_hash(line)}\n") for missed in sorted(analysis.missing): assert source_lines - line = source_lines[missed-1].encode("utf-8") - hashed = base64.b64encode(md5(line).digest()).decode().rstrip("=") - outfile.write(f"DA:{missed},0,{hashed}\n") + line = source_lines[missed-1] + outfile.write(f"DA:{missed},0,{line_hash(line)}\n") outfile.write(f"LF:{analysis.numbers.n_statements}\n") outfile.write(f"LH:{analysis.numbers.n_executed}\n") |