summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2023-05-13 12:57:14 -0400
committerNed Batchelder <ned@nedbatchelder.com>2023-05-13 12:57:14 -0400
commit5d149f14b45e6d9e5874b91d7063aa9ed3368a14 (patch)
tree4f48afe55e3752fedc292bc18856c323cc03cbdb
parent3137ebdb31ece9ab926ac8c0760f2f2e2b67600c (diff)
downloadpython-coveragepy-git-5d149f14b45e6d9e5874b91d7063aa9ed3368a14.tar.gz
style: a bit of refactoring for lcov code
-rw-r--r--coverage/lcovreport.py22
-rw-r--r--tests/test_lcov.py34
2 files changed, 29 insertions, 27 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")
diff --git a/tests/test_lcov.py b/tests/test_lcov.py
index 6d50b62b..b7533c89 100644
--- a/tests/test_lcov.py
+++ b/tests/test_lcov.py
@@ -52,8 +52,8 @@ class LcovTest(CoverageTest):
return file.read()
def test_lone_file(self) -> None:
- """For a single file with a couple of functions, the lcov should cover
- the function definitions themselves, but not the returns."""
+ # For a single file with a couple of functions, the lcov should cover
+ # the function definitions themselves, but not the returns.
self.make_file("main_file.py", """\
#!/usr/bin/env python3
@@ -83,8 +83,8 @@ class LcovTest(CoverageTest):
assert expected_result == actual_result
def test_simple_line_coverage_two_files(self) -> None:
- """Test that line coverage is created when coverage is run,
- and matches the output of the file below."""
+ # Test that line coverage is created when coverage is run,
+ # and matches the output of the file below.
self.create_initial_files()
self.assert_doesnt_exist(".coverage")
self.make_file(".coveragerc", "[lcov]\noutput = data.lcov\n")
@@ -121,7 +121,7 @@ class LcovTest(CoverageTest):
assert expected_result == actual_result
def test_branch_coverage_one_file(self) -> None:
- """Test that the reporter produces valid branch coverage."""
+ # Test that the reporter produces valid branch coverage.
self.make_file("main_file.py", """\
#!/usr/bin/env python3
@@ -156,8 +156,8 @@ class LcovTest(CoverageTest):
assert expected_result == actual_result
def test_branch_coverage_two_files(self) -> None:
- """Test that valid branch coverage is generated
- in the case of two files."""
+ # Test that valid branch coverage is generated
+ # in the case of two files.
self.make_file("main_file.py", """\
#!/usr/bin/env python3
@@ -217,9 +217,8 @@ class LcovTest(CoverageTest):
assert actual_result == expected_result
def test_half_covered_branch(self) -> None:
- """Test that for a given branch that is only half covered,
- the block numbers remain the same, and produces valid lcov.
- """
+ # Test that for a given branch that is only half covered,
+ # the block numbers remain the same, and produces valid lcov.
self.make_file("main_file.py", """\
something = True
@@ -253,14 +252,13 @@ class LcovTest(CoverageTest):
assert actual_result == expected_result
def test_empty_init_files(self) -> None:
- """Test that in the case of an empty __init__.py file, the lcov
- reporter will note that the file is there, and will note the empty
- line. It will also note the lack of branches, and the checksum for
- the line.
-
- Although there are no lines found, it will note one line as hit in
- old Pythons, and no lines hit in newer Pythons.
- """
+ # Test that in the case of an empty __init__.py file, the lcov
+ # reporter will note that the file is there, and will note the empty
+ # line. It will also note the lack of branches, and the checksum for
+ # the line.
+ #
+ # Although there are no lines found, it will note one line as hit in
+ # old Pythons, and no lines hit in newer Pythons.
self.make_file("__init__.py", "")
self.assert_doesnt_exist(".coverage")