|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import json |
| 4 | +import subprocess # nosec B404 |
| 5 | +import sys |
| 6 | + |
| 7 | +# Parse command-line arguments. |
| 8 | +parser = argparse.ArgumentParser() |
| 9 | +parser.add_argument("--commits", nargs="+", default=[]) |
| 10 | +parser.add_argument("file", metavar="<coverage.json>", action="store") |
| 11 | +args = parser.parse_args(sys.argv[1:]) |
| 12 | + |
| 13 | +# Read the coverage information into an object. |
| 14 | +with open(args.file, "rb") as f: |
| 15 | + coverage = json.load(f) |
| 16 | + |
| 17 | +# For each file: |
| 18 | +# - Determine which lines were not covered; |
| 19 | +# - Check when the lines were last modified; |
| 20 | +# - Print details of new, uncovered, lines. |
| 21 | +report = {} |
| 22 | +for filename, info in coverage["files"].items(): |
| 23 | + if not isinstance(filename, str): |
| 24 | + raise TypeError("filename must be a string") |
| 25 | + |
| 26 | + missing = info["missing_lines"] |
| 27 | + if not missing: |
| 28 | + continue |
| 29 | + |
| 30 | + for lineno in missing: |
| 31 | + if not isinstance(lineno, int): |
| 32 | + raise TypeError("line numbers must be integers") |
| 33 | + cmd = [ |
| 34 | + "git", |
| 35 | + "blame", |
| 36 | + filename, |
| 37 | + "-L", |
| 38 | + f"{lineno},{lineno}", |
| 39 | + "--no-abbrev", |
| 40 | + ] |
| 41 | + completed = subprocess.run(cmd, capture_output=True) |
| 42 | + commit = completed.stdout.decode().split()[0].strip() |
| 43 | + |
| 44 | + if commit in args.commits: |
| 45 | + if filename not in report: |
| 46 | + report[filename] = [] |
| 47 | + report[filename].append(str(lineno)) |
| 48 | + |
| 49 | +for filename in report: |
| 50 | + n = len(report[filename]) |
| 51 | + print(f'{n} uncovered lines in {filename}: {",".join(report[filename])}') |
| 52 | + |
| 53 | +# Use the exit code to communicate failure to GitHub. |
| 54 | +if len(report) != 0: |
| 55 | + sys.exit(1) |
| 56 | +else: |
| 57 | + sys.exit(0) |
0 commit comments