Skip to content

Commit e19a7e3

Browse files
committed
tweak script
1 parent ccfd402 commit e19a7e3

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

tests/wrong_coverage.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@
77
from pydantic import BaseModel, Field
88
from rich.console import Console
99

10-
EXCLUDE_COMMENT = 'pragma: no cover'
1110

12-
13-
def main() -> int:
11+
def main(exclude_comment: str = 'pragma: no cover') -> int:
1412
with NamedTemporaryFile(suffix='.json') as coverage_json:
1513
with NamedTemporaryFile(mode='w', suffix='.toml') as config_file:
16-
config_file.write(f"[tool.coverage.report]\nexclude_lines = ['{EXCLUDE_COMMENT}']\n")
14+
config_file.write(f"[tool.coverage.report]\nexclude_lines = ['{exclude_comment}']\n")
1715
config_file.flush()
1816
p = subprocess.run(
1917
['uv', 'run', 'coverage', 'json', f'--rcfile={config_file.name}', '-o', coverage_json.name],
2018
stdout=subprocess.PIPE,
19+
stderr=subprocess.STDOUT,
2120
)
2221
if p.returncode != 0:
23-
print(f'Error running coverage: {p.stderr.decode()}', file=sys.stderr)
22+
print(f'Error running coverage:\n{p.stdout.decode()}', file=sys.stderr)
2423
return p.returncode
2524

2625
r = CoverageReport.model_validate_json(coverage_json.read())
@@ -64,12 +63,12 @@ def add_block(start: int, end: int):
6463

6564
console = Console()
6665
if blocks:
67-
console.print(f"❎ {total_lines} lines marked with '{EXCLUDE_COMMENT}' and covered")
66+
console.print(f"❎ {total_lines} lines marked with '{exclude_comment}' and covered")
6867
for block in blocks:
6968
console.print(block)
7069
return 1
7170
else:
72-
console.print(f"✅ No lines wrongly marked with '{EXCLUDE_COMMENT}'")
71+
console.print(f"✅ No lines wrongly marked with '{exclude_comment}'")
7372
return 0
7473

7574

@@ -101,14 +100,14 @@ class CoverageReport(BaseModel):
101100

102101
# python expressions that can open blocks so can have the `# pragma: no cover` comment on them
103102
# even though they're covered
104-
BLOCK_OPENINGS = re.compile(r'\s*(?:def|async def|@|class|if|elif|else)')
103+
BLOCK_OPENINGS = re.compile(rb'\s*(?:def|async def|@|class|if|elif|else)')
105104

106105

107106
class CodeAnalyzer:
108107
def __init__(self, file_path: str) -> None:
109-
with open(file_path) as f:
108+
with open(file_path, 'rb') as f:
110109
content = f.read()
111-
self.lines: dict[int, str] = dict(enumerate(content.splitlines(), start=1))
110+
self.lines: dict[int, bytes] = dict(enumerate(content.splitlines(), start=1))
112111

113112
def all_block_openings(self, start: int, end: int) -> bool:
114113
return all(self._is_block_opening(line_no) for line_no in range(start, end + 1))

0 commit comments

Comments
 (0)