|
| 1 | +# Copyright 2025 The Circuit Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import argparse |
| 16 | +import sys |
| 17 | +import os |
| 18 | +import json |
| 19 | + |
| 20 | +# cargo llvm-cov --all-features --workspace --json |
| 21 | +if __name__ == "__main__": |
| 22 | + parser = argparse.ArgumentParser() |
| 23 | + parser.add_argument( |
| 24 | + "-p", |
| 25 | + "--percent", |
| 26 | + dest="percent", |
| 27 | + required=False, |
| 28 | + help="Minimum code coverage per line per source file", |
| 29 | + type=float, |
| 30 | + default=80.0, |
| 31 | + ) |
| 32 | + parser.add_argument( |
| 33 | + "-w", |
| 34 | + "--whitelist", |
| 35 | + dest="whitelist", |
| 36 | + nargs="+", |
| 37 | + help="Files to whitelist from coverage checks", |
| 38 | + type=str, |
| 39 | + default=["main.rs"], |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + "input", nargs="?", type=argparse.FileType("r"), default=sys.stdin |
| 43 | + ) |
| 44 | + parser.add_argument( |
| 45 | + "output", nargs="?", type=argparse.FileType("w"), default=sys.stdout |
| 46 | + ) |
| 47 | + args = parser.parse_args() |
| 48 | + |
| 49 | + whitelisted = set(args.whitelist) |
| 50 | + data = json.load(args.input) |
| 51 | + data = data["data"] |
| 52 | + percent = args.percent |
| 53 | + passed = True |
| 54 | + |
| 55 | + print(f"### Code Coverage Summary ({percent:.2f}%)", file=args.output) |
| 56 | + |
| 57 | + for datum in data: |
| 58 | + files = datum["files"] |
| 59 | + for record in files: |
| 60 | + filePassed = True |
| 61 | + name = record["filename"] |
| 62 | + stem = ( |
| 63 | + os.path.basename(os.path.dirname(name)) + "/" + os.path.basename(name) |
| 64 | + ) |
| 65 | + if stem in whitelisted: |
| 66 | + continue |
| 67 | + lineCoverage = record["summary"]["lines"]["percent"] |
| 68 | + if lineCoverage < percent: |
| 69 | + print( |
| 70 | + f"#### {name}: Only {lineCoverage:.2f}% by line", file=args.output |
| 71 | + ) |
| 72 | + print(f"```rust", file=args.output) |
| 73 | + passed = False |
| 74 | + with open(name, "r") as f: |
| 75 | + lines = f.readlines() |
| 76 | + covered = set() |
| 77 | + startSeg = True |
| 78 | + lastLine = None |
| 79 | + for segment in record["segments"]: |
| 80 | + line = max(segment[0] - 1, 0) |
| 81 | + |
| 82 | + if ( |
| 83 | + lastLine is not None |
| 84 | + and line not in covered |
| 85 | + and line != lastLine + 1 |
| 86 | + ): |
| 87 | + startSeg = True |
| 88 | + |
| 89 | + if startSeg: |
| 90 | + print( |
| 91 | + f"// {stem}:{line}", |
| 92 | + file=args.output, |
| 93 | + ) |
| 94 | + startSeg = False |
| 95 | + |
| 96 | + executed = segment[2] != 0 |
| 97 | + if not executed: |
| 98 | + txt = lines[line].strip("\n").removeprefix(" }") |
| 99 | + if ( |
| 100 | + line not in covered |
| 101 | + and len(lines[line].strip(" \n").removeprefix("}")) > 0 |
| 102 | + ): |
| 103 | + print( |
| 104 | + f"{txt}", |
| 105 | + file=args.output, |
| 106 | + ) |
| 107 | + |
| 108 | + covered.add(line) |
| 109 | + lastLine = line |
| 110 | + print(f"```", file=args.output) |
| 111 | + |
| 112 | + if passed: |
| 113 | + print("### All files passed", file=args.output) |
| 114 | + sys.exit(0 if passed else 1) |
0 commit comments