Skip to content

Commit d328041

Browse files
committed
wip
1 parent 8d71084 commit d328041

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

.github/scripts/coverage.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import subprocess
5+
import sys
6+
from datetime import datetime
7+
import xml.etree.ElementTree as xml
8+
import json
9+
10+
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
11+
REPO_DIR = os.path.realpath(os.path.join(SCRIPT_DIR, '../../'))
12+
13+
DIFF_COVER_REPORT = "target/diff-cover/report.json"
14+
MIN_COVERAGE = 90
15+
16+
17+
# ===== Utilities ==========================================
18+
19+
def log(msg):
20+
print(msg, file=sys.stderr)
21+
22+
# ===== Commands to execute ================================
23+
24+
def run_command(args, cwd=REPO_DIR):
25+
return subprocess.run(
26+
args,
27+
cwd=cwd,
28+
check=True,
29+
stdout=subprocess.PIPE,
30+
).stdout.decode('utf-8')
31+
32+
# ===== Commands ===========================================
33+
34+
def comment_report(pr_number, job_url):
35+
"""
36+
Generate a report that and post it as a comment on GitHub.
37+
"""
38+
39+
with open(DIFF_COVER_REPORT) as f:
40+
report_json = json.load(f)
41+
coverage_percent = report_json["total_percent_covered"]
42+
43+
if coverage_percent >= MIN_COVERAGE:
44+
# Coverage passed
45+
return
46+
47+
src_stats = report_json["src_stats"]
48+
49+
files_summary = "\n"
50+
for (filename, file_stats) in sorted(src_stats.items(), key=lambda e: (-e[1]["percent_covered"], e[0])):
51+
covered = file_stats["percent_covered"]
52+
files_summary += f"{covered:3.0f}%: {filename}\n"
53+
54+
details = f"""
55+
<details>
56+
<summary>Details</summary>
57+
58+
#### Summary
59+
60+
```{files_summary}```
61+
62+
#### How to fix it?
63+
64+
Add more tests! Currently coverage runs on SWF tests (that test compatibility with Flash Player).
65+
66+
Sometimes you'll see missing coverage on files you cannot cover (desktop, web code, etc.). \
67+
In this case you can ignore the warning for these files.
68+
</details>
69+
"""
70+
71+
report = f"""
72+
⚠️ Coverage check failed: [{coverage_percent}%]({job_url})
73+
74+
{details}
75+
"""
76+
77+
if pr_number:
78+
run_command([
79+
'gh', 'pr', 'comment', pr_number,
80+
'--body', report])
81+
else:
82+
print(report)
83+
84+
def main():
85+
cmd = sys.argv[1]
86+
log(f'Running command {cmd}')
87+
if cmd == 'comment_report':
88+
pr_number = sys.argv[2] if len(sys.argv) > 2 else ""
89+
job_url = sys.argv[3] if len(sys.argv) > 3 else ""
90+
comment_report(pr_number, job_url)
91+
92+
93+
if __name__ == '__main__':
94+
main()

.github/workflows/test_rust.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ env:
3333
# (Linux) Just to silence warnings about it missing
3434
XDG_RUNTIME_DIR: ''
3535

36+
COVERAGE_SCRIPT: ./.github/scripts/coverage.py
37+
3638
jobs:
3739
changes:
3840
name: Paths filter
@@ -259,6 +261,13 @@ jobs:
259261
if: always()
260262
run: cat target/diff-cover/report.md >> $GITHUB_STEP_SUMMARY
261263

264+
- name: Comment
265+
if: always()
266+
env:
267+
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
268+
run: |
269+
$COVERAGE_SCRIPT comment_report "${{ github.event.pull_request.number }}" "${RUN_URL}"
270+
262271
dependencies:
263272
needs: changes
264273
if: needs.changes.outputs.should_run == 'true'

0 commit comments

Comments
 (0)