Repository navigation
262 lines (240 loc) · 10.3 KB
/
Copy pathrun-tests.yml
File metadata and controls
262 lines (240 loc) · 10.3 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
name: Backend Tests
on:
workflow_dispatch:
workflow_call:
inputs:
send_notification:
description: "Whether to send Discord notification"
type: boolean
default: true
secrets:
WEBHOOK_URL:
description: "Discord webhook URL"
required: false
outputs:
success:
description: "Whether tests passed"
value: ${{ jobs.test.outputs.success }}
total:
description: "Total number of tests"
value: ${{ jobs.test.outputs.total }}
passed:
description: "Number of passed tests"
value: ${{ jobs.test.outputs.passed }}
failed:
description: "Number of failed tests"
value: ${{ jobs.test.outputs.failed }}
duration:
description: "Test duration in seconds"
value: ${{ jobs.test.outputs.duration }}
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
outputs:
success: ${{ steps.test-results.outputs.success }}
total: ${{ steps.test-results.outputs.total }}
passed: ${{ steps.test-results.outputs.passed }}
failed: ${{ steps.test-results.outputs.failed }}
duration: ${{ steps.test-results.outputs.duration }}
test_files: ${{ steps.test-results.outputs.test_files }}
test_files_passed: ${{ steps.test-results.outputs.test_files_passed }}
test_files_failed: ${{ steps.test-results.outputs.test_files_failed }}
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Set up Node.js
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: '24'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests
id: run-tests
continue-on-error: true
run: |
START_TIME=$(date +%s)
npm test 2>&1 | tee test-output.txt
TEST_EXIT_CODE=${PIPESTATUS[0]}
END_TIME=$(date +%s)
echo "exit_code=$TEST_EXIT_CODE" >> $GITHUB_OUTPUT
echo "start_time=$(date -u +"%H:%M:%S")" >> $GITHUB_OUTPUT
echo "elapsed=$((END_TIME - START_TIME))" >> $GITHUB_OUTPUT
- name: Parse test results
id: test-results
if: always()
run: |
# Parse the test output from vitest's default reporter
if [ -f test-output.txt ]; then
# Strip ANSI color codes for reliable parsing
sed -i 's/\x1b\[[0-9;]*m//g' test-output.txt
# Debug: show relevant lines
echo "=== Relevant test output lines ==="
grep -E "(Test Files|Tests|Duration)" test-output.txt | tail -5 || true
echo "==================================="
# Extract test file count (e.g., "Test Files 81 passed (81)")
TEST_FILES_LINE=$(grep "Test Files" test-output.txt | tail -1 || echo "")
if [ -n "$TEST_FILES_LINE" ]; then
TEST_FILES_PASSED=$(echo "$TEST_FILES_LINE" | grep -oE "[0-9]+ passed" | grep -oE "[0-9]+" | head -1 || echo "0")
TEST_FILES_FAILED=$(echo "$TEST_FILES_LINE" | grep -oE "[0-9]+ failed" | grep -oE "[0-9]+" | head -1 || echo "0")
TEST_FILES=$(echo "$TEST_FILES_LINE" | grep -oE "\([0-9]+\)" | grep -oE "[0-9]+" | head -1 || echo "0")
else
TEST_FILES_PASSED=0
TEST_FILES_FAILED=0
TEST_FILES=0
fi
# Extract test count - look for line containing "Tests" followed by number, exclude "Test Files"
TESTS_LINE=$(grep -E "Tests\s+[0-9]+" test-output.txt | grep -v "Test Files" | tail -1 || echo "")
if [ -n "$TESTS_LINE" ]; then
PASSED=$(echo "$TESTS_LINE" | grep -oE "[0-9]+ passed" | grep -oE "[0-9]+" | head -1 || echo "0")
FAILED=$(echo "$TESTS_LINE" | grep -oE "[0-9]+ failed" | grep -oE "[0-9]+" | head -1 || echo "0")
TOTAL=$(echo "$TESTS_LINE" | grep -oE "\([0-9]+\)" | grep -oE "[0-9]+" | head -1 || echo "0")
else
PASSED=0
FAILED=0
TOTAL=0
fi
# Extract duration (e.g., "Duration 26.97s")
DURATION_LINE=$(grep -E "Duration\s+[0-9]+" test-output.txt | tail -1 || echo "")
if [ -n "$DURATION_LINE" ]; then
DURATION=$(echo "$DURATION_LINE" | grep -oE "[0-9]+\.[0-9]+s" | head -1 | sed 's/s//' || echo "0")
else
DURATION="${{ steps.run-tests.outputs.elapsed }}"
fi
# Determine success based on exit code and failed count
if [ "${{ steps.run-tests.outputs.exit_code }}" = "0" ] && [ "${FAILED:-0}" = "0" ]; then
SUCCESS="true"
else
SUCCESS="false"
fi
else
TOTAL=0
PASSED=0
FAILED=1
TEST_FILES=0
TEST_FILES_PASSED=0
TEST_FILES_FAILED=1
DURATION=0
SUCCESS="false"
fi
# Set defaults for empty values
TOTAL=${TOTAL:-0}
PASSED=${PASSED:-0}
FAILED=${FAILED:-0}
TEST_FILES=${TEST_FILES:-0}
TEST_FILES_PASSED=${TEST_FILES_PASSED:-0}
TEST_FILES_FAILED=${TEST_FILES_FAILED:-0}
DURATION=${DURATION:-0}
echo "=== Parsed values ==="
echo "TEST_FILES_PASSED=$TEST_FILES_PASSED"
echo "TEST_FILES=$TEST_FILES"
echo "PASSED=$PASSED"
echo "TOTAL=$TOTAL"
echo "DURATION=$DURATION"
echo "SUCCESS=$SUCCESS"
echo "===================="
echo "total=$TOTAL" >> $GITHUB_OUTPUT
echo "passed=$PASSED" >> $GITHUB_OUTPUT
echo "failed=$FAILED" >> $GITHUB_OUTPUT
echo "duration=$DURATION" >> $GITHUB_OUTPUT
echo "success=$SUCCESS" >> $GITHUB_OUTPUT
echo "test_files=$TEST_FILES" >> $GITHUB_OUTPUT
echo "test_files_passed=$TEST_FILES_PASSED" >> $GITHUB_OUTPUT
echo "test_files_failed=$TEST_FILES_FAILED" >> $GITHUB_OUTPUT
echo "## 🧪 Test Results" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ "$SUCCESS" = "true" ]; then
echo "### ✅ All tests passed!" >> $GITHUB_STEP_SUMMARY
else
echo "### ❌ Some tests failed" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Metric | Value |" >> $GITHUB_STEP_SUMMARY
echo "|--------|-------|" >> $GITHUB_STEP_SUMMARY
echo "| Test Files | $TEST_FILES_PASSED passed ($TEST_FILES) |" >> $GITHUB_STEP_SUMMARY
echo "| Tests | $PASSED passed ($TOTAL) |" >> $GITHUB_STEP_SUMMARY
echo "| Duration | ${DURATION}s |" >> $GITHUB_STEP_SUMMARY
- name: Send Discord notification
if: always() && (inputs.send_notification != false)
env:
WEBHOOK_URL: ${{ secrets.WEBHOOK_URL }}
REF_NAME: ${{ github.ref_name }}
run: |
if [ -z "$WEBHOOK_URL" ]; then
echo "No webhook URL provided, skipping notification"
exit 0
fi
SUCCESS="${{ steps.test-results.outputs.success }}"
TOTAL="${{ steps.test-results.outputs.total }}"
PASSED="${{ steps.test-results.outputs.passed }}"
FAILED="${{ steps.test-results.outputs.failed }}"
DURATION="${{ steps.test-results.outputs.duration }}"
TEST_FILES="${{ steps.test-results.outputs.test_files }}"
TEST_FILES_PASSED="${{ steps.test-results.outputs.test_files_passed }}"
TEST_FILES_FAILED="${{ steps.test-results.outputs.test_files_failed }}"
if [ "$SUCCESS" = "true" ]; then
COLOR=5763719
TITLE="✅ Tests Passed"
DESCRIPTION="All tests completed successfully for **ReadMeABook**"
TEST_FILES_VALUE="$TEST_FILES_PASSED passed ($TEST_FILES)"
TESTS_VALUE="$PASSED passed ($TOTAL)"
else
COLOR=15548997
TITLE="❌ Tests Failed"
DESCRIPTION="Some tests failed for **ReadMeABook**"
TEST_FILES_VALUE="$TEST_FILES_PASSED passed, $TEST_FILES_FAILED failed ($TEST_FILES)"
TESTS_VALUE="$PASSED passed, $FAILED failed ($TOTAL)"
fi
curl -H "Content-Type: application/json" \
-X POST \
-d "{
\"embeds\": [{
\"title\": \"$TITLE\",
\"description\": \"$DESCRIPTION\",
\"color\": $COLOR,
\"fields\": [
{
\"name\": \"📁 Test Files\",
\"value\": \"\`$TEST_FILES_VALUE\`\",
\"inline\": true
},
{
\"name\": \"🧪 Tests\",
\"value\": \"\`$TESTS_VALUE\`\",
\"inline\": true
},
{
\"name\": \"⏱️ Duration\",
\"value\": \"\`${DURATION}s\`\",
\"inline\": true
},
{
\"name\": \"🌿 Branch\",
\"value\": \"\`$REF_NAME\`\",
\"inline\": true
},
{
\"name\": \"📋 Commit\",
\"value\": \"[\`$(echo ${{ github.sha }} | cut -c1-7)\`](${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }})\",
\"inline\": true
},
{
\"name\": \"🔗 Workflow\",
\"value\": \"[View Run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})\",
\"inline\": true
}
],
\"footer\": {
\"text\": \"ReadMeABook CI/CD • Test Suite\"
},
\"timestamp\": \"$(date -u +"%Y-%m-%dT%H:%M:%SZ")\"
}]
}" \
"$WEBHOOK_URL"
- name: Fail if tests failed
if: steps.test-results.outputs.success != 'true'
run: exit 1