-
Notifications
You must be signed in to change notification settings - Fork 37
460 lines (386 loc) · 18.8 KB
/
check-rust-examples.yml
File metadata and controls
460 lines (386 loc) · 18.8 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# SPDX-License-Identifier: MIT OR Apache-2.0
# SPDX-FileCopyrightText: The Coding Guidelines Subcommittee Contributors
name: Check Rust Examples
# This workflow is designed to be called from build-guidelines.yml
# It does not have its own triggers to avoid duplicate runs
on:
workflow_call:
jobs:
# Test Rust examples across multiple toolchains (tests/ directory only)
test-examples:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
# Test with stable (latest) - all examples except nightly-only should pass
- rust: stable
name: Stable (latest)
# Test with an older version to verify version skipping works correctly
- rust: '1.75.0'
name: '1.75.0'
# Test with nightly for channel-specific examples
- rust: nightly
name: Nightly
name: Test Examples (${{ matrix.name }})
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust toolchain (${{ matrix.name }})
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ matrix.rust }}
- name: Display Rust version
run: |
rustc --version
cargo --version
- name: Install uv
uses: astral-sh/setup-uv@v6
- name: Extract and test Rust examples
id: test-examples
run: |
mkdir -p build/examples
# Run combined extract and test - only test the test examples, not src/
set -o pipefail # Ensure pipe failures are captured
uv run python scripts/extract_rust_examples.py \
--test \
--src-dir tests/rust-examples \
--prelude src/examples_prelude.rs \
--json build/examples/results.json \
--fail-on-error \
--verbose \
2>&1 | tee build/examples/test_output.log
EXIT_CODE=${PIPESTATUS[0]}
# Generate summary
echo "## 🧪 Rust Examples Test Results (${{ matrix.name }})" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Rust version:** \`$(rustc --version)\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ -f build/examples/results.json ]; then
TOTAL=$(jq '.total' build/examples/results.json)
PASSED=$(jq '.passed' build/examples/results.json)
FAILED=$(jq '.failed' build/examples/results.json)
SKIPPED=$(jq '.skipped // 0' build/examples/results.json)
if [ "$FAILED" -eq 0 ]; then
if [ "$SKIPPED" -gt 0 ]; then
echo "✅ **$PASSED of $TOTAL examples passed, $SKIPPED skipped**" >> $GITHUB_STEP_SUMMARY
else
echo "✅ **All $TOTAL examples passed!**" >> $GITHUB_STEP_SUMMARY
fi
else
echo "❌ **$FAILED of $TOTAL examples failed** ($SKIPPED skipped)" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Failed Examples" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
jq -r '.results[] | select(.passed == false) | "- **\(.example.source_file):\(.example.line_number)**: \(.error_message)"' \
build/examples/results.json >> $GITHUB_STEP_SUMMARY
fi
# Show skipped examples if any
if [ "$SKIPPED" -gt 0 ]; then
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Skipped Examples" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
jq -r '.results[] | select(.skipped == true) | "- **\(.example.source_file):\(.example.line_number)**: \(.skip_reason)"' \
build/examples/results.json >> $GITHUB_STEP_SUMMARY 2>/dev/null || true
fi
fi
exit $EXIT_CODE
- name: Compare against expected results
if: always()
run: |
echo "## 🔍 Expected Results Comparison (${{ matrix.name }})" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ ! -f build/examples/results.json ] || [ ! -f tests/rust-examples/expected-results.json ]; then
echo "⚠️ Missing results files for comparison" >> $GITHUB_STEP_SUMMARY
exit 0
fi
# Determine which expected key to use based on matrix
case "${{ matrix.rust }}" in
stable) EXPECTED_KEY="stable" ;;
nightly) EXPECTED_KEY="nightly" ;;
*) EXPECTED_KEY="${{ matrix.rust }}" ;;
esac
MISMATCHES=0
# Compare each example against expected results
while read -r expected_line; do
LINE=$(echo "$expected_line" | jq -r '.line')
NAME=$(echo "$expected_line" | jq -r '.name')
EXPECTED=$(echo "$expected_line" | jq -r ".expected[\"$EXPECTED_KEY\"] // \"pass\"")
# Find actual result for this line
ACTUAL_RESULT=$(jq -r --argjson line "$LINE" '.results[] | select(.example.line_number == $line)' build/examples/results.json)
if [ -z "$ACTUAL_RESULT" ]; then
echo "⚠️ Line $LINE ($NAME): Not found in results" >> $GITHUB_STEP_SUMMARY
MISMATCHES=$((MISMATCHES + 1))
continue
fi
PASSED=$(echo "$ACTUAL_RESULT" | jq -r '.passed')
SKIPPED=$(echo "$ACTUAL_RESULT" | jq -r '.skipped // false')
if [ "$SKIPPED" = "true" ]; then
ACTUAL="skip"
elif [ "$PASSED" = "true" ]; then
ACTUAL="pass"
else
ACTUAL="fail"
fi
if [ "$ACTUAL" != "$EXPECTED" ]; then
echo "❌ Line $LINE ($NAME): Expected **$EXPECTED**, got **$ACTUAL**" >> $GITHUB_STEP_SUMMARY
MISMATCHES=$((MISMATCHES + 1))
fi
done < <(jq -c '.examples[]' tests/rust-examples/expected-results.json)
if [ "$MISMATCHES" -eq 0 ]; then
echo "✅ All results match expected values" >> $GITHUB_STEP_SUMMARY
else
echo "" >> $GITHUB_STEP_SUMMARY
echo "**$MISMATCHES mismatches found**" >> $GITHUB_STEP_SUMMARY
exit 1
fi
- name: Upload example test artifacts
uses: actions/upload-artifact@v4
if: always()
with:
name: rust-examples-results-${{ matrix.rust }}
path: |
build/examples/results.json
build/examples/test_output.log
build/examples/examples.json
retention-days: 7
- name: Annotate failures in PR
if: failure() && github.event_name == 'pull_request'
run: |
if [ -f build/examples/results.json ]; then
jq -r '.results[] | select(.passed == false) |
"::error file=\(.example.source_file),line=\(.example.line_number)::\(.error_message)"' \
build/examples/results.json
fi
# Analyze what toolchains are needed for guideline examples
analyze-requirements:
runs-on: ubuntu-latest
outputs:
needs_nightly: ${{ steps.analyze.outputs.needs_nightly }}
nightly_count: ${{ steps.analyze.outputs.nightly_count }}
version_requirements: ${{ steps.analyze.outputs.version_requirements }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v6
- name: Analyze toolchain requirements
id: analyze
run: |
# Get requirements as JSON (script outputs clean JSON to stdout)
REQUIREMENTS=$(uv run python scripts/extract_rust_examples.py \
--list-requirements \
--src-dir src/coding-guidelines)
# Extract summary info
NEEDS_NIGHTLY=$(echo "$REQUIREMENTS" | jq '.summary.needs_nightly > 0')
NIGHTLY_COUNT=$(echo "$REQUIREMENTS" | jq '.summary.needs_nightly')
VERSION_REQS=$(echo "$REQUIREMENTS" | jq -c '.versions | keys')
echo "needs_nightly=$NEEDS_NIGHTLY" >> $GITHUB_OUTPUT
echo "nightly_count=$NIGHTLY_COUNT" >> $GITHUB_OUTPUT
echo "version_requirements=$VERSION_REQS" >> $GITHUB_OUTPUT
# Add to summary
echo "## 📊 Toolchain Requirements Analysis" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "$REQUIREMENTS" | jq -r '"- **Total examples:** \(.summary.total)"' >> $GITHUB_STEP_SUMMARY
echo "$REQUIREMENTS" | jq -r '"- **Default (stable, no version req):** \(.summary.default_only)"' >> $GITHUB_STEP_SUMMARY
echo "$REQUIREMENTS" | jq -r '"- **Needs nightly:** \(.summary.needs_nightly)"' >> $GITHUB_STEP_SUMMARY
echo "$REQUIREMENTS" | jq -r '"- **Needs specific version:** \(.summary.needs_specific_version)"' >> $GITHUB_STEP_SUMMARY
if [ "$NIGHTLY_COUNT" -gt 0 ]; then
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Nightly Examples" >> $GITHUB_STEP_SUMMARY
echo "$REQUIREMENTS" | jq -r '.channels.nightly[] | "- \(.file):\(.line)"' >> $GITHUB_STEP_SUMMARY
fi
# Test guideline examples that work on stable with no version requirements
test-guidelines-stable:
runs-on: ubuntu-latest
needs: [test-examples, analyze-requirements]
# Always run so that this job explicitly fails when dependencies fail,
# rather than being skipped. Skipped jobs don't block merge queue.
if: always()
name: Test Guidelines (Stable)
steps:
- name: Fail if dependencies failed
if: needs.test-examples.result != 'success' || needs.analyze-requirements.result != 'success'
run: |
echo "::error::Dependency failed - test-examples: ${{ needs.test-examples.result }}, analyze-requirements: ${{ needs.analyze-requirements.result }}"
exit 1
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust toolchain (stable)
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- name: Display Rust version
run: |
rustc --version
cargo --version
- name: Install uv
uses: astral-sh/setup-uv@v6
- name: Test guideline examples (stable)
id: test-guidelines
run: |
mkdir -p build/examples
set -o pipefail
# Test all examples - those requiring newer versions or nightly will be skipped
uv run python scripts/extract_rust_examples.py \
--test \
--src-dir src/coding-guidelines \
--prelude src/examples_prelude.rs \
--json build/examples/guideline-results.json \
--fail-on-error \
--verbose \
2>&1 | tee build/examples/guideline_test_output.log
EXIT_CODE=${PIPESTATUS[0]}
# Generate summary
echo "## 📚 Guideline Examples Test Results (Stable)" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Rust version:** \`$(rustc --version)\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ -f build/examples/guideline-results.json ]; then
TOTAL=$(jq '.total' build/examples/guideline-results.json)
PASSED=$(jq '.passed' build/examples/guideline-results.json)
FAILED=$(jq '.failed' build/examples/guideline-results.json)
SKIPPED=$(jq '.skipped // 0' build/examples/guideline-results.json)
if [ "$FAILED" -eq 0 ]; then
if [ "$SKIPPED" -gt 0 ]; then
echo "✅ **$PASSED of $TOTAL guideline examples passed, $SKIPPED skipped**" >> $GITHUB_STEP_SUMMARY
else
echo "✅ **All $TOTAL guideline examples passed!**" >> $GITHUB_STEP_SUMMARY
fi
else
echo "❌ **$FAILED of $TOTAL guideline examples failed** ($SKIPPED skipped)" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Failed Examples" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
jq -r '.results[] | select(.passed == false) | "- **\(.example.source_file):\(.example.line_number)**: \(.error_message)"' \
build/examples/guideline-results.json >> $GITHUB_STEP_SUMMARY
fi
if [ "$SKIPPED" -gt 0 ]; then
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Skipped Examples" >> $GITHUB_STEP_SUMMARY
echo "_These require nightly or a specific Rust version and are tested separately._" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
jq -r '.results[] | select(.skipped == true) | "- **\(.example.source_file):\(.example.line_number)**: \(.skip_reason)"' \
build/examples/guideline-results.json >> $GITHUB_STEP_SUMMARY 2>/dev/null || true
fi
fi
exit $EXIT_CODE
- name: Upload guideline test artifacts
uses: actions/upload-artifact@v4
if: always()
with:
name: guideline-examples-results-stable
path: |
build/examples/guideline-results.json
build/examples/guideline_test_output.log
retention-days: 7
- name: Annotate failures in PR
if: failure() && github.event_name == 'pull_request'
run: |
if [ -f build/examples/guideline-results.json ]; then
jq -r '.results[] | select(.passed == false) |
"::error file=\(.example.source_file),line=\(.example.line_number)::\(.error_message)"' \
build/examples/guideline-results.json
fi
# Test guideline examples that require nightly (only if there are any)
test-guidelines-nightly:
runs-on: ubuntu-latest
needs: [test-examples, analyze-requirements]
# Always run (when nightly is needed) so that this job explicitly fails
# when dependencies fail, rather than being skipped.
if: always() && needs.analyze-requirements.outputs.needs_nightly == 'true'
name: Test Guidelines (Nightly)
steps:
- name: Fail if dependencies failed
if: needs.test-examples.result != 'success' || needs.analyze-requirements.result != 'success'
run: |
echo "::error::Dependency failed - test-examples: ${{ needs.test-examples.result }}, analyze-requirements: ${{ needs.analyze-requirements.result }}"
exit 1
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust toolchain (nightly)
uses: dtolnay/rust-toolchain@stable
with:
toolchain: nightly
- name: Display Rust version
run: |
rustc --version
cargo --version
- name: Install uv
uses: astral-sh/setup-uv@v6
- name: Test nightly-only guideline examples
id: test-guidelines-nightly
run: |
mkdir -p build/examples
set -o pipefail
# Only test examples that require nightly
uv run python scripts/extract_rust_examples.py \
--test \
--src-dir src/coding-guidelines \
--prelude src/examples_prelude.rs \
--filter-channel nightly \
--json build/examples/guideline-nightly-results.json \
--fail-on-error \
--verbose \
2>&1 | tee build/examples/guideline_nightly_test_output.log
EXIT_CODE=${PIPESTATUS[0]}
# Generate summary
echo "## 📚 Guideline Examples Test Results (Nightly)" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Rust version:** \`$(rustc --version)\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ -f build/examples/guideline-nightly-results.json ]; then
TOTAL=$(jq '.total' build/examples/guideline-nightly-results.json)
PASSED=$(jq '.passed' build/examples/guideline-nightly-results.json)
FAILED=$(jq '.failed' build/examples/guideline-nightly-results.json)
if [ "$FAILED" -eq 0 ]; then
echo "✅ **All $TOTAL nightly examples passed!**" >> $GITHUB_STEP_SUMMARY
else
echo "❌ **$FAILED of $TOTAL nightly examples failed**" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Failed Examples" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
jq -r '.results[] | select(.passed == false) | "- **\(.example.source_file):\(.example.line_number)**: \(.error_message)"' \
build/examples/guideline-nightly-results.json >> $GITHUB_STEP_SUMMARY
fi
fi
exit $EXIT_CODE
- name: Upload nightly test artifacts
uses: actions/upload-artifact@v4
if: always()
with:
name: guideline-examples-results-nightly
path: |
build/examples/guideline-nightly-results.json
build/examples/guideline_nightly_test_output.log
retention-days: 7
# Validate rustdoc attribute consistency
check-rustdoc-format:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Check for legacy code blocks
run: |
echo "## 📋 Rustdoc Format Check" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
LEGACY_COUNT=0
LEGACY_FILES=""
# Check both .rst and .rst.inc files (per-guideline structure uses .rst.inc)
for file in $(find src/coding-guidelines \( -name "*.rst" -o -name "*.rst.inc" \) 2>/dev/null); do
COUNT=$(grep -c "^\s*\.\. code-block:: rust" "$file" 2>/dev/null || echo 0)
if [ "$COUNT" -gt 0 ]; then
LEGACY_FILES="$LEGACY_FILES\n- $file: $COUNT occurrences"
LEGACY_COUNT=$((LEGACY_COUNT + COUNT))
fi
done
if [ "$LEGACY_COUNT" -eq 0 ]; then
echo "✅ All Rust code examples use the rust-example:: directive" >> $GITHUB_STEP_SUMMARY
else
echo "⚠️ **Found $LEGACY_COUNT code blocks using legacy format**" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "Files with legacy code-block:: rust:" >> $GITHUB_STEP_SUMMARY
echo -e "$LEGACY_FILES" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "Consider migrating with: \`uv run python scripts/migrate_rust_examples.py\`" >> $GITHUB_STEP_SUMMARY
fi