-
Notifications
You must be signed in to change notification settings - Fork 0
179 lines (152 loc) · 5.58 KB
/
tests.yml
File metadata and controls
179 lines (152 loc) · 5.58 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
name: Tests
on:
push:
branches: [main]
pull_request:
jobs:
unit:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
extensions: swoole, redis, sockets, pcov
tools: composer:v2
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Run tests with coverage
run: |
set +e
./vendor/bin/phpunit --testsuite Unit --coverage-clover=coverage/unit.xml --coverage-text 2>&1 | tee coverage/unit-summary.txt
EXIT=${PIPESTATUS[0]}
set -e
if [ "$EXIT" -eq 139 ] && [ -f coverage/unit.xml ]; then
echo "Swoole exit segfault (ignored — coverage report exists)"
exit 0
fi
exit $EXIT
- name: Upload coverage
uses: actions/upload-artifact@v4
with:
name: coverage-unit
path: coverage/unit.xml
integration:
name: Integration Tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
extensions: swoole, redis, sockets, pcov
tools: composer:v2
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Run tests with coverage
run: |
set +e
./vendor/bin/phpunit --testsuite Integration --coverage-clover=coverage/integration.xml --coverage-text 2>&1 | tee coverage/integration-summary.txt
EXIT=${PIPESTATUS[0]}
set -e
# Swoole can segfault on process exit after coverage generation;
# treat it as success if the clover report was written.
if [ "$EXIT" -eq 139 ] && [ -f coverage/integration.xml ]; then
echo "Swoole exit segfault (ignored — coverage report exists)"
exit 0
fi
exit $EXIT
- name: Upload coverage
uses: actions/upload-artifact@v4
with:
name: coverage-integration
path: coverage/integration.xml
coverage:
name: Coverage Check
runs-on: ubuntu-latest
needs: [unit, integration]
steps:
- name: Download unit coverage
uses: actions/download-artifact@v4
with:
name: coverage-unit
path: coverage
- name: Download integration coverage
uses: actions/download-artifact@v4
with:
name: coverage-integration
path: coverage
- name: Merge and check coverage
run: |
merge_clover() {
local total_stmts=0 covered_stmts=0
for f in "$@"; do
[ -f "$f" ] || continue
s=$(grep -oP 'statements="\K\d+' "$f" | paste -sd+ | bc)
c=$(grep -oP 'coveredstatements="\K\d+' "$f" | paste -sd+ | bc)
total_stmts=$((total_stmts + s))
covered_stmts=$((covered_stmts + c))
done
echo "$covered_stmts $total_stmts"
}
read COVERED TOTAL <<< $(merge_clover coverage/unit.xml coverage/integration.xml)
PCT=$(echo "scale=2; $COVERED * 100 / $TOTAL" | bc)
echo "Coverage: ${PCT}% ($COVERED / $TOTAL statements)"
- name: Upload merged baseline
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: |
echo "$COVERED $TOTAL" > coverage/baseline.txt
env:
COVERED: ${{ env.COVERED }}
TOTAL: ${{ env.TOTAL }}
- name: Save baseline artifact
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
uses: actions/upload-artifact@v4
with:
name: coverage-baseline
path: |
coverage/unit.xml
coverage/integration.xml
retention-days: 90
- name: Download baseline (PR only)
if: github.event_name == 'pull_request'
uses: dawidd6/action-download-artifact@v6
with:
name: coverage-baseline
path: coverage/baseline
branch: main
workflow: tests.yml
if_no_artifact_found: warn
- name: Check coverage diff (PR only)
if: github.event_name == 'pull_request'
run: |
if [ ! -f coverage/baseline/unit.xml ]; then
echo "No baseline found, skipping diff check"
exit 0
fi
merge_clover() {
local total_stmts=0 covered_stmts=0
for f in "$@"; do
[ -f "$f" ] || continue
s=$(grep -oP 'statements="\K\d+' "$f" | paste -sd+ | bc)
c=$(grep -oP 'coveredstatements="\K\d+' "$f" | paste -sd+ | bc)
total_stmts=$((total_stmts + s))
covered_stmts=$((covered_stmts + c))
done
echo "$covered_stmts $total_stmts"
}
read BASE_COV BASE_TOTAL <<< $(merge_clover coverage/baseline/unit.xml coverage/baseline/integration.xml)
BASE_PCT=$(echo "scale=2; $BASE_COV * 100 / $BASE_TOTAL" | bc)
read PR_COV PR_TOTAL <<< $(merge_clover coverage/unit.xml coverage/integration.xml)
PR_PCT=$(echo "scale=2; $PR_COV * 100 / $PR_TOTAL" | bc)
DIFF=$(echo "$PR_PCT - $BASE_PCT" | bc -l)
echo "Baseline: ${BASE_PCT}% → PR: ${PR_PCT}% (${DIFF}%)"
if [ "$(echo "$DIFF < -1" | bc -l)" -eq 1 ]; then
echo "::error::Coverage dropped ${DIFF}% (max allowed: -1%)"
exit 1
fi