Skip to content

Commit a876052

Browse files
committed
test: a general helper for iterating over our own source files
1 parent 82cff3e commit a876052

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

tests/helpers.py

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import flaky
2828

29+
import coverage
2930
from coverage import env
3031
from coverage.debug import DebugControl
3132
from coverage.exceptions import CoverageWarning
@@ -373,3 +374,16 @@ def flaky_method(max_runs: int) -> Callable[[TestMethod], TestMethod]:
373374
def _decorator(fn: TestMethod) -> TestMethod:
374375
return cast(TestMethod, flaky.flaky(max_runs)(fn))
375376
return _decorator
377+
378+
379+
def all_our_source_files() -> Iterator[tuple[Path, str]]:
380+
"""Iterate over all of our own source files.
381+
382+
Produces a stream of (filename, file contents) tuples.
383+
"""
384+
cov_dir = Path(coverage.__file__).parent.parent
385+
# To run against all the files in the tox venvs:
386+
# for source_file in cov_dir.rglob("*.py"):
387+
for sub in [".", "benchmark", "ci", "coverage", "lab", "tests"]:
388+
for source_file in (cov_dir / sub).glob("*.py"):
389+
yield (source_file, source_file.read_text(encoding="utf-8"))

tests/test_regions.py

+16-18
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
import pytest
1313

14-
import coverage
1514
from coverage.plugin import CodeRegion
1615
from coverage.regions import code_regions
1716

17+
from tests.helpers import all_our_source_files
18+
1819

1920
def test_code_regions() -> None:
2021
regions = code_regions(textwrap.dedent("""\
@@ -90,24 +91,21 @@ def test_real_code_regions() -> None:
9091
# Run code_regions on most of the coverage source code, checking that it
9192
# succeeds and there are no overlaps.
9293

93-
cov_dir = Path(coverage.__file__).parent.parent
9494
any_fails = False
95-
# To run against all the files in the tox venvs:
96-
# for source_file in cov_dir.rglob("*.py"):
97-
for sub in [".", "ci", "coverage", "lab", "tests"]:
98-
for source_file in (cov_dir / sub).glob("*.py"):
99-
regions = code_regions(source_file.read_text(encoding="utf-8"))
100-
for kind in ["function", "class"]:
101-
kind_regions = [reg for reg in regions if reg.kind == kind]
102-
line_counts = collections.Counter(
103-
lno for reg in kind_regions for lno in reg.lines
95+
for source_file, source in all_our_source_files():
96+
regions = code_regions(source)
97+
for kind in ["function", "class"]:
98+
kind_regions = [reg for reg in regions if reg.kind == kind]
99+
line_counts = collections.Counter(
100+
lno for reg in kind_regions for lno in reg.lines
101+
)
102+
overlaps = [line for line, count in line_counts.items() if count > 1]
103+
if overlaps: # pragma: only failure
104+
print(
105+
f"{kind.title()} overlaps in {source_file.relative_to(Path.cwd())}: "
106+
+ f"{overlaps}"
104107
)
105-
overlaps = [line for line, count in line_counts.items() if count > 1]
106-
if overlaps: # pragma: only failure
107-
print(
108-
f"{kind.title()} overlaps in {source_file.relative_to(Path.cwd())}: "
109-
+ f"{overlaps}"
110-
)
111-
any_fails = True
108+
any_fails = True
109+
112110
if any_fails:
113111
pytest.fail("Overlaps were found") # pragma: only failure

tests/test_testing.py

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from tests.coveragetest import CoverageTest
2323
from tests.helpers import (
2424
CheckUniqueFilenames, FailingProxy,
25+
all_our_source_files,
2526
arcz_to_arcs, assert_count_equal, assert_coverage_warnings,
2627
re_lines, re_lines_text, re_line,
2728
)
@@ -450,3 +451,13 @@ def subtract(self, a, b): # type: ignore[no-untyped-def]
450451
proxy.add(3, 4)
451452
# then add starts working
452453
assert proxy.add(5, 6) == 11
454+
455+
456+
def test_all_our_source_files() -> None:
457+
# Twas brillig and the slithy toves
458+
i = 0
459+
for i, (source_file, source) in enumerate(all_our_source_files(), start=1):
460+
has_toves = (source_file.name == "test_testing.py")
461+
assert (("# Twas brillig " + "and the slithy toves") in source) == has_toves
462+
assert len(source) > 190 # tests/__init__.py is shortest at 196
463+
assert 120 < i < 200 # currently 125 files

0 commit comments

Comments
 (0)