-
-
Notifications
You must be signed in to change notification settings - Fork 49
101 lines (85 loc) Β· 3.07 KB
/
coverage.yml
File metadata and controls
101 lines (85 loc) Β· 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
name: Coverage Tests
on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:
permissions:
contents: write
jobs:
test-and-badge:
name: Run Tests & Update Badge
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v5
- name: Setup Node 22
uses: actions/setup-node@v6
with:
node-version: 22
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests with JSON reporter
id: test
run: |
# Run tests and capture output
npm run test -- --reporter=json --reporter=default --outputFile=test-results.json || true
# Check if test results file exists
if [ ! -f test-results.json ]; then
echo "Test results file not found, creating fallback"
echo '{"numTotalTests": 0, "numPassedTests": 0}' > test-results.json
fi
# Parse test results
TOTAL=$(node -p "try { const r = require('./test-results.json'); r.numTotalTests || 0 } catch(e) { 0 }")
PASSED=$(node -p "try { const r = require('./test-results.json'); r.numPassedTests || 0 } catch(e) { 0 }")
echo "total=$TOTAL" >> $GITHUB_OUTPUT
echo "passed=$PASSED" >> $GITHUB_OUTPUT
# Determine color based on pass rate
if [ "$TOTAL" -eq 0 ]; then
COLOR="lightgrey"
elif [ "$PASSED" -eq "$TOTAL" ]; then
COLOR="brightgreen"
elif [ "$PASSED" -ge $((TOTAL * 80 / 100)) ]; then
COLOR="green"
elif [ "$PASSED" -ge $((TOTAL * 60 / 100)) ]; then
COLOR="yellow"
else
COLOR="red"
fi
echo "color=$COLOR" >> $GITHUB_OUTPUT
echo "### π§ͺ Test Results" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Passed:** $PASSED / $TOTAL" >> $GITHUB_STEP_SUMMARY
echo "**Status:** $COLOR" >> $GITHUB_STEP_SUMMARY
- name: Generate badge data
run: |
mkdir -p .github/badges
cat > .github/badges/coverage-tests.json << EOF
{
"schemaVersion": 1,
"label": "Coverage",
"message": "${{ steps.test.outputs.passed }} / ${{ steps.test.outputs.total }} tests",
"color": "${{ steps.test.outputs.color }}"
}
EOF
- name: Commit badge data
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add .github/badges/coverage-tests.json
git diff --staged --quiet || git commit -m "Update test badge [skip ci]"
git push
- name: Upload test results
if: always()
uses: actions/upload-artifact@v5
with:
name: test-results
path: |
test-results.json
.github/badges/coverage-tests.json
retention-days: 30