-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.yml
More file actions
517 lines (449 loc) · 21.8 KB
/
action.yml
File metadata and controls
517 lines (449 loc) · 21.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# ------------------------------------------------------------------------------------
# Test Failure Detection (Composite Action)
#
# Purpose: Define and provide reusable test failure detection functions for
# robust test output parsing across different formats and test types.
#
# This action provides sophisticated failure detection capabilities:
# - JSON-based test output parsing (fast single-pass)
# - Text-based failure detection with multiple patterns
# - Detailed error capture and context preservation
# - Fallback detection for edge cases
#
# Maintainer: @mrz1836
#
# ------------------------------------------------------------------------------------
name: "Test Failure Detection"
description: "Provides robust test failure detection functions for JSON and text output parsing"
inputs:
output-file:
description: "Test output file to analyze"
required: false
default: "test-output.log"
exit-code:
description: "Test command exit code"
required: false
default: "0"
mode:
description: "Detection mode (json or text)"
required: false
default: "text"
failures-file:
description: "Output file for detected failures"
required: false
default: "test-failures.txt"
outputs:
failure-count:
description: "Number of detected test failures"
value: ${{ steps.detect-failures.outputs.failure-count }}
has-failures:
description: "Boolean indicating if failures were detected"
value: ${{ steps.detect-failures.outputs.has-failures }}
detailed-failures-file:
description: "Path to detailed failures file"
value: ${{ steps.detect-failures.outputs.detailed-failures-file }}
runs:
using: "composite"
steps:
- name: 🔧 Define failure detection functions
shell: bash
run: |
# Define reusable function for robust test failure detection
cat > test-failure-functions.sh << 'DETECTION_FUNCTIONS_EOF'
#!/bin/bash
# Robust test failure detection function
detect_test_failures() {
local output_file="$1"
local exit_code="${2:-0}"
local mode="${3:-text}"
local failures_file="${4:-test-failures.txt}"
echo "🔍 Detecting test failures with exit code: $exit_code, mode: $mode"
# Primary check: exit code indicates failure
if [[ "$exit_code" -ne 0 ]]; then
echo "❌ Exit code $exit_code indicates test failure"
if [[ -f "$output_file" ]]; then
case "$mode" in
"json")
# Enhanced JSON-based detection
detect_failures_from_json "$output_file" "$failures_file"
;;
"text"|*)
# Enhanced text-based detection
detect_failures_from_text "$output_file" "$failures_file"
;;
esac
# Count detected failures
if [[ -f "$failures_file" ]]; then
DETECTED_FAILURES=$(wc -l < "$failures_file" 2>/dev/null || echo "0")
echo "📊 Detected $DETECTED_FAILURES specific failures"
return $DETECTED_FAILURES
fi
else
echo "⚠️ Output file '$output_file' not found, relying on exit code"
echo "Exit code indicates failure but no output file found" > "$failures_file"
return 1
fi
else
echo "✅ Exit code 0 indicates success"
touch "$failures_file" # Create empty failures file
return 0
fi
}
# Smart and efficient JSON failure detection with unique signatures
detect_failures_from_json() {
local json_file="$1"
local failures_file="$2"
local signatures_file="${failures_file%.txt}-signatures.json"
echo "🔍 Using smart JSON-based failure detection on $json_file"
# Quick JSON validation (< 0.1s) - check if file contains JSON test output
if ! grep -q '^{.*"Action"' "$json_file" 2>/dev/null; then
echo "⚠️ No JSON content detected, using text fallback"
detect_failures_from_text "$json_file" "$failures_file"
return
fi
echo "✅ JSON content detected, processing efficiently..."
# Initialize JSON array for structured failures with signatures
echo '[]' > "$signatures_file"
# Define common test validation filter to avoid duplication
local test_failure_filter='select(.Action == "fail" and .Test != null and .Test != "" and .Test != "null" and (.Test | test("^Test[A-Za-z]")))'
# Fast single-pass JSON extraction for test failures (< 1s for 10K lines)
# Filter JSON lines and parse in one pass - eliminates 2-minute hang
# Note: Line numbers aren't available in Go test JSON output
# IMPORTANT: Only detect actual test function failures, not package/suite completion events
grep '^{' "$json_file" 2>/dev/null | \
jq -r "$test_failure_filter"' |
"--- FAIL: \(.Test) (\(.Package))"' \
2>/dev/null > "$failures_file"
# Create structured test failure entries with unique signatures
if grep '^{' "$json_file" 2>/dev/null | jq -r "$test_failure_filter" 2>/dev/null | head -1 | grep -q .; then
echo "📝 Creating structured test failure entries with enhanced output..."
# First pass: Extract test failure basic info from Action == "fail" entries
local temp_failures
temp_failures=$(mktemp)
grep '^{' "$json_file" 2>/dev/null | \
jq -r "$test_failure_filter"' | {
type: "test",
package: .Package,
test: (if (.Test and .Test != null and .Test != "null") then .Test else "unknown" end),
signature: (.Package + ":" + (if (.Test and .Test != null and .Test != "null") then .Test else "unknown" end)),
unique_id: (.Package + ":" + (if (.Test and .Test != null and .Test != "null") then .Test else "unknown" end) | gsub("[^a-zA-Z0-9_/.-]"; "_"))
}' 2>/dev/null | jq -s '.' > "$temp_failures"
# Second pass: Extract failure output from Action == "output" entries containing "--- FAIL:"
local temp_outputs
temp_outputs=$(mktemp)
grep '^{' "$json_file" 2>/dev/null | \
jq -r 'select(.Action == "output" and (.Output // "") | contains("--- FAIL:")) | {
package: .Package,
test: (if (.Test and .Test != null and .Test != "null") then .Test else "unknown" end),
output: (.Output // ""),
signature: (.Package + ":" + (if (.Test and .Test != null and .Test != "null") then .Test else "unknown" end))
}' 2>/dev/null | jq -s '.' > "$temp_outputs"
# Third pass: Merge failure info with outputs using signature as key
jq -n --slurpfile failures "$temp_failures" --slurpfile outputs "$temp_outputs" '
($failures[0] // []) as $fail_list |
($outputs[0] // []) as $output_list |
($output_list | group_by(.signature) | map({key: .[0].signature, value: (map(.output) | join("\n"))})) as $output_map |
$fail_list | map(. + {
output: (($output_map | map(select(.key == .signature)) | .[0].value) // ""),
line_number: null
})' > "$signatures_file.tmp" 2>/dev/null && \
mv "$signatures_file.tmp" "$signatures_file" || echo '[]' > "$signatures_file"
# Cleanup temp files
rm -f "$temp_failures" "$temp_outputs"
fi
# Also check for build failures which have FailedBuild field but no Test field
local build_failures
build_failures=$(grep '^{' "$json_file" 2>/dev/null | \
jq -r 'select(.FailedBuild) | .Package // .ImportPath' 2>/dev/null | sort -u)
if [[ -n "$build_failures" ]]; then
echo "🔨 Processing build failures with signatures..."
# Create temporary array for build failures
echo '[]' > "${signatures_file}.build"
# Extract all build-output entries once before the loop for better performance
local all_build_outputs
all_build_outputs=$(grep '^{' "$json_file" 2>/dev/null | \
jq -r 'select(.Action == "build-output" and .ImportPath) |
(.ImportPath | split(" ")[0]) + "\t" + .Output' 2>/dev/null)
while IFS= read -r pkg; do
if [[ -n "$pkg" ]]; then
echo "--- BUILD FAILED: $pkg" >> "$failures_file"
# Extract build error messages for this package from pre-extracted data
local build_errors
build_errors=$(echo "$all_build_outputs" | grep "^$pkg " | cut -f2 | \
grep -E "^[^[:space:]]" | head -10) # Limit to first 10 error lines
local error_output=""
if [[ -n "$build_errors" ]]; then
echo "$build_errors" | sed 's/^/ /' >> "$failures_file"
error_output="$build_errors"
else
echo " Build failed (no detailed error available)" >> "$failures_file"
error_output="Build failed (no detailed error available)"
fi
# Create structured build failure entry
jq -n --arg pkg "$pkg" --arg errors "$error_output" '{
type: "build",
package: $pkg,
test: "build_compilation",
output: $errors,
signature: ($pkg + ":build_compilation"),
unique_id: ($pkg + ":build_compilation" | gsub("[^a-zA-Z0-9_/.-]"; "_"))
}' >> "${signatures_file}.build" 2>/dev/null
fi
done <<< "$build_failures"
# Merge build failures into main signatures file
if [[ -s "${signatures_file}.build" ]]; then
jq -s 'add' "$signatures_file" "${signatures_file}.build" > "${signatures_file}.tmp" 2>/dev/null && \
mv "${signatures_file}.tmp" "$signatures_file" || echo '[]' > "$signatures_file"
fi
rm -f "${signatures_file}.build"
fi
local failure_count
failure_count=$(wc -l < "$failures_file" 2>/dev/null | tr -d '\n\r' | xargs)
[[ "$failure_count" =~ ^[0-9]+$ ]] || failure_count=0
# Count distinct failures (test failures + build failures)
local test_failure_count build_failure_count
test_failure_count=$(grep -c "^--- FAIL:" "$failures_file" 2>/dev/null || echo "0")
build_failure_count=$(grep -c "^--- BUILD FAILED:" "$failures_file" 2>/dev/null || echo "0")
# Validate signatures file
local unique_failure_count=0
if [[ -s "$signatures_file" ]]; then
unique_failure_count=$(jq 'length' "$signatures_file" 2>/dev/null || echo "0")
echo "📊 Generated $unique_failure_count unique failure signatures"
fi
if [[ $test_failure_count -gt 0 ]] || [[ $build_failure_count -gt 0 ]]; then
echo "✅ Found $test_failure_count test failures and $build_failure_count build failures in JSON output"
echo "🔍 Created $unique_failure_count unique signatures for deduplication"
return 0
else
echo "ℹ️ No failures detected in JSON output"
return 0
fi
}
# Enhanced text-based failure detection with signatures
detect_failures_from_text() {
local text_file="$1"
local failures_file="$2"
local detailed_failures_file="${failures_file%.txt}-detailed.txt"
local signatures_file="${failures_file%.txt}-signatures.json"
echo "🔍 Using enhanced text-based failure detection on $text_file"
# Initialize JSON array for structured failures with signatures
echo '[]' > "$signatures_file"
# Enhanced pattern matching for various failure formats
# Patterns handle: FAIL, --- FAIL, --FAIL, [FAIL], FAIL:, etc.
local patterns=(
'^FAIL[[:space:]:]'
'^---[[:space:]]*FAIL'
'^--[[:space:]]*FAIL'
'^\[?FAIL\]?[[:space:]:.]'
'--- FAIL:'
'FAIL[[:space:]]*:'
'^[[:space:]]*FAIL[[:space:]]'
)
local temp_failures=$(mktemp)
local temp_detailed=$(mktemp)
local found_any=false
# First pass: Find all failure lines and capture context
for pattern in "${patterns[@]}"; do
if grep -E -A 15 "$pattern" "$text_file" >> "$temp_detailed" 2>/dev/null; then
found_any=true
# Also capture just the failure line for the summary
grep -E "$pattern" "$text_file" >> "$temp_failures" 2>/dev/null || true
fi
done
if [[ "$found_any" == "true" ]]; then
# Process the detailed failures to create structured output
echo "🔍 Processing detailed failure output..."
# Create a structured detailed failures file with error messages
awk '
BEGIN {
current_test = ""
capture_output = 0
output_buffer = ""
}
/^(FAIL|---.*FAIL|--.*FAIL|\[?FAIL\]?)/ {
# If we were capturing output, save it
if (current_test != "" && output_buffer != "") {
print "TEST:" current_test
print "ERROR:" output_buffer
print "---SEPARATOR---"
}
# Start new test capture
current_test = $0
output_buffer = ""
capture_output = 1
next
}
capture_output == 1 && /^[[:space:]]*$/ {
# Empty line might end the error context
if (length(output_buffer) > 100) capture_output = 0
next
}
capture_output == 1 && !/^(PASS|ok |FAIL|---.*FAIL|--.*FAIL)/ {
# Capture error output lines
if (output_buffer == "") {
output_buffer = $0
} else {
output_buffer = output_buffer "\n" $0
}
# Stop if we have captured enough context
if (length(output_buffer) > 1500) capture_output = 0
}
/^(PASS|ok )/ && capture_output == 1 {
# Another test started, stop capturing
capture_output = 0
}
END {
# Save the last test if we were capturing
if (current_test != "" && output_buffer != "") {
print "TEST:" current_test
print "ERROR:" output_buffer
print "---SEPARATOR---"
}
}
' "$temp_detailed" > "$detailed_failures_file" 2>/dev/null || true
# Remove duplicates and sort for the summary file
sort -u "$temp_failures" > "$failures_file"
# Generate signatures from text failures
echo "📝 Generating failure signatures from text output..."
local temp_signatures=$(mktemp)
echo '[]' > "$temp_signatures"
while IFS= read -r failure_line; do
if [[ -n "$failure_line" && "$failure_line" != *"Generic test failure"* ]]; then
# Extract package and test info from failure line
# Pattern: --- FAIL: TestName (package.name)
if [[ "$failure_line" =~ "FAIL:" ]]; then
local test_name=$(echo "$failure_line" | sed -E 's/^.*FAIL: ([^ ]+).*$/\1/' | head -c 200)
local package_name=$(echo "$failure_line" | sed -E 's/^.*\(([^):]+)[^)]*\).*$/\1/' | head -c 200)
# Handle build failures
if [[ "$failure_line" =~ "BUILD FAILED:" ]]; then
package_name=$(echo "$failure_line" | sed 's/^--- BUILD FAILED: //' | head -c 200)
test_name="build_compilation"
jq -n --arg pkg "$package_name" --arg test "$test_name" --arg output "$failure_line" '{
type: "build",
package: $pkg,
test: $test,
output: $output,
line_number: null,
signature: ($pkg + ":" + $test),
unique_id: (($pkg + ":" + $test) | gsub("[^a-zA-Z0-9_/.-]"; "_"))
}' >> "$temp_signatures.items"
else
# Regular test failure
if [[ -n "$test_name" && -n "$package_name" && "$test_name" != "$package_name" ]]; then
jq -n --arg pkg "$package_name" --arg test "$test_name" --arg output "$failure_line" '{
type: "test",
package: $pkg,
test: $test,
output: $output,
line_number: null,
signature: ($pkg + ":" + $test),
unique_id: (($pkg + ":" + $test) | gsub("[^a-zA-Z0-9_/.-]"; "_"))
}' >> "$temp_signatures.items"
fi
fi
fi
fi
done < "$failures_file"
# Combine signatures into array
if [[ -f "$temp_signatures.items" ]]; then
jq -s '.' "$temp_signatures.items" > "$signatures_file" 2>/dev/null || echo '[]' > "$signatures_file"
fi
rm -f "$temp_signatures" "$temp_signatures.items"
local unique_signature_count=$(jq 'length' "$signatures_file" 2>/dev/null || echo "0")
echo "✅ Text parsing found $(wc -l < "$failures_file") failures with $unique_signature_count unique signatures"
# Clean up
rm -f "$temp_failures" "$temp_detailed"
return 0
fi
# Fallback: look for any error indicators
echo "⚠️ Standard failure patterns not found, checking for error indicators"
local error_patterns=(
'panic:'
'fatal error:'
'build failed'
'compilation error'
'timeout'
'killed'
'error:'
)
for pattern in "${error_patterns[@]}"; do
if grep -i -A 5 "$pattern" "$text_file" >> "$temp_detailed" 2>/dev/null; then
found_any=true
grep -i "$pattern" "$text_file" >> "$temp_failures" 2>/dev/null || true
fi
done
if [[ "$found_any" == "true" ]]; then
sort -u "$temp_failures" > "$failures_file"
cp "$temp_detailed" "$detailed_failures_file" 2>/dev/null || true
echo "⚠️ Found $(wc -l < "$failures_file") error indicators (not standard test failures)"
rm -f "$temp_failures" "$temp_detailed"
return 0
fi
rm -f "$temp_failures" "$temp_detailed"
# If exit code indicated failure but no patterns found, create generic entry
if [[ "${TEST_EXIT_CODE:-0}" -ne 0 ]]; then
echo "Generic test failure (exit code ${TEST_EXIT_CODE:-0}) - pattern detection failed" > "$failures_file"
echo "⚠️ Exit code indicates failure but no recognizable patterns found"
return 1
else
touch "$failures_file" # Create empty failures file
echo "✅ No failures detected and exit code is 0"
return 0
fi
}
# Utility function for safe numeric validation
sanitize_numeric() {
local value="$1"
value=$(echo "$value" | tr -d '\n\r' | xargs)
if [[ "$value" =~ ^[0-9]+$ ]]; then
echo "$value"
else
echo "0"
fi
}
# Export functions for use in other steps
export -f detect_test_failures
export -f detect_failures_from_json
export -f detect_failures_from_text
export -f sanitize_numeric
DETECTION_FUNCTIONS_EOF
# Source the functions to make them available
source test-failure-functions.sh
echo "✅ Failure detection functions defined and loaded"
- name: 🔍 Detect test failures
id: detect-failures
shell: bash
run: |
# Source the functions
source test-failure-functions.sh
# Run detection with provided inputs
OUTPUT_FILE="${{ inputs.output-file }}"
EXIT_CODE="${{ inputs.exit-code }}"
MODE="${{ inputs.mode }}"
FAILURES_FILE="${{ inputs.failures-file }}"
# Detect failures
detect_test_failures "$OUTPUT_FILE" "$EXIT_CODE" "$MODE" "$FAILURES_FILE"
detection_result=$?
# Calculate outputs
if [[ -f "$FAILURES_FILE" ]]; then
FAILURE_COUNT=$(wc -l < "$FAILURES_FILE" 2>/dev/null | tr -d '\n\r' | xargs)
FAILURE_COUNT=$(sanitize_numeric "$FAILURE_COUNT")
else
FAILURE_COUNT=0
fi
HAS_FAILURES="false"
if [[ "$FAILURE_COUNT" -gt 0 ]] || [[ "$detection_result" -ne 0 ]]; then
HAS_FAILURES="true"
fi
DETAILED_FILE="${FAILURES_FILE%.txt}-detailed.txt"
# Set outputs
echo "failure-count=$FAILURE_COUNT" >> $GITHUB_OUTPUT
echo "has-failures=$HAS_FAILURES" >> $GITHUB_OUTPUT
echo "detailed-failures-file=$DETAILED_FILE" >> $GITHUB_OUTPUT
echo "📊 Failure detection results:"
echo " • Failure count: $FAILURE_COUNT"
echo " • Has failures: $HAS_FAILURES"
echo " • Detailed file: $DETAILED_FILE"
# Always exit with 0 to not fail the workflow step
# The outputs contain the failure information for downstream steps
exit 0