Last active
March 9, 2022 15:58
-
-
Save akaihola/2511fe7d2f29f219cb995649afd3d8d2 to your computer and use it in GitHub Desktop.
Script for converting coverage.py XML output into lint-style output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Script for converting ``.coverage`` SQLite output into lint-style output | |
The output is suitable for consumption using Darker. | |
See https://github.com/akaihola/darker | |
Example usage, pointing out modified code in a feature branch not covered by | |
the test suite:: | |
pip install pytest-cov darker | |
curl -O $HOME/.local/bin/cov_to_lint.py \ | |
https://gist.githubusercontent.com/akaihola/2511fe7d2f29f219cb995649afd3d8d2/raw/ | |
cd $HOME/mypackage | |
git checkout my-feature-branch | |
pytest --cov=mypackage src | |
ls coverage.xml | |
darker --revision master --lint cov_to_lint.py src | |
""" | |
from typing import List, TextIO | |
from coverage import Coverage | |
from coverage.report import get_analysis_to_report, render_report | |
class LintReporter: | |
@staticmethod | |
def report(morfs: List[str], outfile: TextIO) -> None: | |
cov = Coverage(include="./***") | |
cov.load() | |
for fr, analysis in get_analysis_to_report(cov, morfs): | |
longest_location = 0 | |
shortest_indent = 16 | |
lines = [] | |
for linenum in sorted(analysis.missing): | |
line = fr.parser.lines[linenum - 1] | |
lines.append((linenum, line)) | |
longest_location = max( | |
longest_location, len(f"{fr.filename}:{linenum}:") | |
) | |
indent = len(line) - len(line.lstrip()) | |
shortest_indent = min(shortest_indent, indent) | |
prev_linenum = 0 | |
for linenum, line in lines: | |
if linenum > prev_linenum + 1: | |
outfile.write("\n") | |
prev_linenum = linenum | |
location = f"{fr.filename}:{linenum}:" | |
outfile.write( | |
f"{location:{longest_location}} no coverage:" | |
f" {line[shortest_indent:]}\n" | |
) | |
def main() -> None: | |
render_report("-", LintReporter, morfs=[]) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment