-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrun_coverage_tests.sh
More file actions
executable file
·552 lines (473 loc) · 22.5 KB
/
run_coverage_tests.sh
File metadata and controls
executable file
·552 lines (473 loc) · 22.5 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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#!/bin/bash
set -euo pipefail
# Script to run Go tests with per-directory coverage reporting
# Usage: ./run_coverage_tests.sh [COV_THRESHOLD] [PRINT_TS] [FAIL_ON_NO_TESTS] [DEBUG]
#
# Arguments:
# COV_THRESHOLD - Minimum coverage percentage required (default: 64.2)
# PRINT_TS - Build ID/timestamp for reports (default: empty)
# FAIL_ON_NO_TESTS - Fail if directories have no tests (default: false)
# DEBUG - Keep temp files for debugging (default: false)
#
# Behavior:
# - Runs all tests in all directories, even if some fail (for complete visibility)
# - Tracks all test failures and reports them at the end
# - Exits with code 1 if ANY tests fail OR overall coverage is below threshold
# - Generates coverage reports for all directories where tests ran successfully
#
# Output files (in current directory):
# - coverage.out: Combined coverage profile for use with go tool cover
# - coverage_report.txt: Human-readable coverage summary
COV_THRESHOLD=${1:-64.2}
PRINT_TS=${2:-""}
FAIL_ON_NO_TESTS=${3:-false} # Set to true if directories without tests should fail the build
DEBUG=${4:-false} # Set to true for verbose debugging
OVERALL_EXIT_CODE=0
FAILED_DIRS=""
# Create temporary directory for intermediate files
WORK_DIR=$(mktemp -d -t coverage-XXXXXX)
# Cleanup function to remove temporary files on exit
cleanup() {
if [[ "$DEBUG" != "true" ]]; then
rm -rf "$WORK_DIR"
else
echo "DEBUG: Keeping work directory: $WORK_DIR" >&2
fi
}
trap cleanup EXIT
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to find Go binary in common locations
find_go() {
# Check if go is already in PATH
if command -v go >/dev/null 2>&1; then
echo "go"
return 0
fi
# Check common Go installation locations
for go_path in \
"/usr/local/go/bin/go" \
"/usr/bin/go" \
"/snap/bin/go" \
"$HOME/go/bin/go" \
"/opt/go/bin/go"; do
if [[ -x "$go_path" ]]; then
echo "$go_path"
return 0
fi
done
return 1
}
# Find and set up Go
GO_BIN=$(find_go)
if [[ -z "$GO_BIN" ]]; then
echo -e "${RED}ERROR: Go command not found in PATH or common locations.${NC}" >&2
echo "Searched locations:" >&2
echo " - PATH: $(echo $PATH | tr ':' '\n' | grep -E 'go|bin' || echo 'No Go paths in PATH')" >&2
echo " - /usr/local/go/bin/go" >&2
echo " - /usr/bin/go" >&2
echo " - /snap/bin/go" >&2
echo " - $HOME/go/bin/go" >&2
echo " - /opt/go/bin/go" >&2
echo "" >&2
echo "Solutions:" >&2
echo " 1. Install Go: sudo apt install golang-go" >&2
echo " 2. Run without sudo: ./scripts/run_coverage_tests.sh" >&2
echo " 3. Use sudo with PATH: sudo env PATH=\$PATH ./scripts/run_coverage_tests.sh" >&2
exit 1
fi
# If Go is not 'go' (i.e., full path), add its directory to PATH
if [[ "$GO_BIN" != "go" ]]; then
GO_DIR=$(dirname "$GO_BIN")
export PATH="$GO_DIR:$PATH"
echo "Added Go directory to PATH: $GO_DIR" >&2
fi
# Verify Go is working
echo "Using Go: $GO_BIN" >&2
echo "Go version: $($GO_BIN version)" >&2
echo "Working directory: $(pwd)" >&2
echo "Go module status:" >&2
$GO_BIN mod tidy >/dev/null 2>&1 || echo " Warning: go mod tidy failed" >&2
if [[ -f "go.mod" ]]; then
echo " Go module found: $(head -n1 go.mod)" >&2
else
echo " No go.mod file found" >&2
fi
# Test basic Go functionality
echo "Testing basic Go functionality..." >&2
if $GO_BIN list ./... >/dev/null 2>&1; then
echo " Go can list packages successfully" >&2
else
echo " Warning: 'go list ./...' failed" >&2
fi
echo "" >&2
echo -e "${BLUE}=== Running Unit Tests with Coverage ===${NC}"
echo "Coverage threshold: ${COV_THRESHOLD}%"
if [[ -n "${PRINT_TS}" ]]; then
echo "Build ID: ${PRINT_TS}"
fi
echo ""
# Find all directories with Go files (including those without tests)
ALL_GO_DIRS=$(find . -name "*.go" -type f | sed 's|/[^/]*$||' | sort -u | grep -v vendor | grep -v ".git" || true)
# Find directories with test files
TEST_DIRS=$(find . -name "*_test.go" -type f | sed 's|/[^/]*$||' | sort -u | grep -v vendor | grep -v ".git" || true)
if [[ -z "${ALL_GO_DIRS}" ]]; then
echo -e "${RED}ERROR: No Go directories found${NC}"
exit 1
fi
echo "Found $(echo "$ALL_GO_DIRS" | wc -l) directories with Go files" >&2
echo "Found $(echo "$TEST_DIRS" | wc -l) directories with test files" >&2
echo "" >&2
# Create table header
echo "| Directory | Coverage | Result |"
echo "|-------------------------------------|----------|----------|"
# Arrays to store results and failed tests
declare -a DIR_RESULTS
declare -a DIR_COVERAGE
declare -a DIR_STATUS
declare -a FAILED_TEST_DETAILS=() # Initialize empty array
# Run tests for each directory with Go files
for GO_DIR in ${ALL_GO_DIRS}; do
DIR_NAME=$(echo ${GO_DIR} | sed 's|^\./||')
COVERAGE_FILE="${WORK_DIR}/${DIR_NAME//\//_}_coverage.out"
TEST_LOG="${WORK_DIR}/${DIR_NAME//\//_}_test.log"
if [[ "$DEBUG" == "true" ]]; then
echo "DEBUG: Processing directory: ${GO_DIR}" >&2
echo "DEBUG: DIR_NAME: ${DIR_NAME}" >&2
echo "DEBUG: COVERAGE_FILE: ${COVERAGE_FILE}" >&2
echo "DEBUG: TEST_LOG: ${TEST_LOG}" >&2
fi
# Check if this directory has test files
if echo "${TEST_DIRS}" | grep -q "^${GO_DIR}$"; then
# Directory has tests - run them
TEST_CMD="$GO_BIN test -v -coverprofile=\"${COVERAGE_FILE}\" \"${GO_DIR}\""
if [[ "$DEBUG" == "true" ]]; then
echo "DEBUG: Running: $TEST_CMD" >&2
fi
if timeout 300 $GO_BIN test -v -coverprofile="${COVERAGE_FILE}" "${GO_DIR}" > "${TEST_LOG}" 2>&1; then
# Tests passed, check coverage
if [[ -f "${COVERAGE_FILE}" ]] && [[ -s "${COVERAGE_FILE}" ]]; then
# Calculate coverage percentage
COVERAGE_PCT=$($GO_BIN tool cover -func="${COVERAGE_FILE}" | grep "total:" | awk '{print $3}' | sed 's/%//')
if [[ -z "${COVERAGE_PCT}" ]]; then
if [[ "$DEBUG" == "true" ]]; then
echo "DEBUG: No total coverage found for ${DIR_NAME}" >&2
echo "DEBUG: Coverage file content:" >&2
head -n 3 "${COVERAGE_FILE}" >&2
echo "DEBUG: Cover tool output:" >&2
$GO_BIN tool cover -func="${COVERAGE_FILE}" >&2
fi
COVERAGE_PCT="0.0"
fi
# Tests passed and coverage was generated - this is a PASS
STATUS="PASS"
STATUS_COLOR="${GREEN}"
DIR_RESULTS+=("${DIR_NAME}")
DIR_COVERAGE+=("${COVERAGE_PCT}")
DIR_STATUS+=("${STATUS}")
printf "| %-35s | %8s%% | ${STATUS_COLOR}%-8s${NC} |\n" \
"${DIR_NAME}" "${COVERAGE_PCT}" "${STATUS}"
else
# No coverage file or empty - tests failed
STATUS="FAIL"
OVERALL_EXIT_CODE=1
# Extract failed test information
FAILED_TESTS=""
if [[ -f "${TEST_LOG}" ]]; then
# Extract failed test names from test output (|| true to prevent exit on no matches)
FAILED_TESTS=$(grep -E "^--- FAIL:|FAIL\s+.*\s+\(" "${TEST_LOG}" 2>/dev/null | sed 's/^--- FAIL: //' | sed 's/FAIL[[:space:]]*//' | sed 's/[[:space:]]*(.*//' | sort -u | tr '\n' ', ' | sed 's/,$//' || true)
if [[ -z "${FAILED_TESTS}" ]]; then
# If no specific test names found, check for compilation errors
if grep -q "build failed" "${TEST_LOG}" 2>/dev/null || grep -q "compilation error" "${TEST_LOG}" 2>/dev/null; then
FAILED_TESTS="compilation error"
else
FAILED_TESTS="unknown test failure"
fi
fi
else
FAILED_TESTS="no test output"
fi
FAILED_DIRS="${FAILED_DIRS} ${DIR_NAME}(no-coverage)"
FAILED_TEST_DETAILS+=("${DIR_NAME}: ${FAILED_TESTS}")
if [[ "$DEBUG" == "true" ]]; then
echo "DEBUG: No coverage file for ${DIR_NAME}" >&2
if [[ -f "${COVERAGE_FILE}" ]]; then
echo "DEBUG: Coverage file exists but is empty" >&2
ls -la "${COVERAGE_FILE}" >&2
else
echo "DEBUG: Coverage file does not exist: ${COVERAGE_FILE}" >&2
fi
echo "DEBUG: Test output:" >&2
cat "${TEST_LOG}" >&2
fi
printf "| %-35s | %8s | ${RED}%-8s${NC} |\n" \
"${DIR_NAME}" "N/A" "FAIL"
fi
else
# Tests failed - mark for failure but continue with other tests
STATUS="FAIL"
OVERALL_EXIT_CODE=1 # Mark build as failed, but continue testing
# Extract failed test information
FAILED_TESTS=""
if [[ -f "${TEST_LOG}" ]]; then
# Extract failed test names from test output (|| true to prevent exit on no matches)
FAILED_TESTS=$(grep -E "^--- FAIL:|FAIL\s+.*\s+\(" "${TEST_LOG}" 2>/dev/null | sed 's/^--- FAIL: //' | sed 's/FAIL[[:space:]]*//' | sed 's/[[:space:]]*(.*//' | sort -u | tr '\n' ', ' | sed 's/,$//' || true)
if [[ -z "${FAILED_TESTS}" ]]; then
# If no specific test names found, check for compilation errors
if grep -q "build failed" "${TEST_LOG}" 2>/dev/null || grep -q "compilation error" "${TEST_LOG}" 2>/dev/null; then
FAILED_TESTS="compilation error"
else
FAILED_TESTS="unknown test failure"
fi
fi
else
FAILED_TESTS="no test output"
fi
FAILED_DIRS="${FAILED_DIRS} ${DIR_NAME}(test-fail)"
FAILED_TEST_DETAILS+=("${DIR_NAME}: ${FAILED_TESTS}")
# Try to get coverage even if tests failed
COVERAGE_PCT="N/A"
if [[ -f "${COVERAGE_FILE}" ]] && [[ -s "${COVERAGE_FILE}" ]]; then
COVERAGE_FROM_FAILED=$($GO_BIN tool cover -func="${COVERAGE_FILE}" | grep "total:" | awk '{print $3}' | sed 's/%//')
if [[ -n "${COVERAGE_FROM_FAILED}" ]]; then
COVERAGE_PCT="${COVERAGE_FROM_FAILED}%"
fi
fi
if [[ "$DEBUG" == "true" ]]; then
echo "DEBUG: Tests failed for ${DIR_NAME}" >&2
echo "DEBUG: Command: $TEST_CMD" >&2
echo "DEBUG: Test output:" >&2
cat "${TEST_LOG}" >&2
echo "DEBUG: ---" >&2
fi
printf "| %-35s | %8s | ${RED}%-8s${NC} |\n" \
"${DIR_NAME}" "${COVERAGE_PCT}" "FAIL"
# Continue with next directory instead of exiting
fi
else
# Directory has no tests
if [[ "${FAIL_ON_NO_TESTS}" == "true" ]]; then
printf "| %-35s | %8s | ${RED}%-8s${NC} |\n" \
"${DIR_NAME}" "N/A" "FAIL"
OVERALL_EXIT_CODE=1
FAILED_DIRS="${FAILED_DIRS} ${DIR_NAME}(no-tests)"
else
printf "| %-35s | %8s | ${YELLOW}%-8s${NC} |\n" \
"${DIR_NAME}" "N/A" "NO-TESTS"
fi
fi
done
echo ""
# Generate overall coverage report
echo -e "${BLUE}=== Generating Overall Coverage Report ===${NC}"
# Calculate overall coverage by running tests on all packages at once
OVERALL_COVERAGE_FILE="${WORK_DIR}/overall_coverage.out"
echo "Calculating overall repository coverage..."
# Method 1: Try to get overall coverage even if some tests fail
echo "Attempting overall coverage calculation (Method 1)..."
if $GO_BIN test -coverprofile="${OVERALL_COVERAGE_FILE}" ./... > "${WORK_DIR}/overall_test.log" 2>&1; then
# All tests passed
if [[ -f "${OVERALL_COVERAGE_FILE}" ]] && [[ -s "${OVERALL_COVERAGE_FILE}" ]]; then
OVERALL_COVERAGE=$($GO_BIN tool cover -func="${OVERALL_COVERAGE_FILE}" | grep "total:" | awk '{print $3}' | sed 's/%//')
if [[ -z "${OVERALL_COVERAGE}" ]]; then
OVERALL_COVERAGE="0.0"
fi
echo "✓ Overall repository coverage: ${OVERALL_COVERAGE}%"
COVERAGE_METHOD="all tests passed"
# Copy the overall coverage file for artifacts
cp "${OVERALL_COVERAGE_FILE}" coverage.out 2>/dev/null || echo "mode: set" > coverage.out
else
echo "✗ No overall coverage data generated despite test success"
OVERALL_COVERAGE="0.0"
echo "mode: set" > coverage.out
COVERAGE_METHOD="failed - no data"
fi
else
# Some tests failed, but try to get coverage anyway
echo "Some tests failed, attempting coverage with failures ignored..."
# Method 1b: Try to get coverage despite test failures by continuing on failure
if $GO_BIN test -coverprofile="${OVERALL_COVERAGE_FILE}" -failfast=false ./... > "${WORK_DIR}/overall_test_with_failures.log" 2>&1 || true; then
if [[ -f "${OVERALL_COVERAGE_FILE}" ]] && [[ -s "${OVERALL_COVERAGE_FILE}" ]]; then
OVERALL_COVERAGE=$($GO_BIN tool cover -func="${OVERALL_COVERAGE_FILE}" | grep "total:" | awk '{print $3}' | sed 's/%//')
if [[ -z "${OVERALL_COVERAGE}" ]]; then
OVERALL_COVERAGE="0.0"
fi
echo "✓ Overall repository coverage (with test failures): ${OVERALL_COVERAGE}%"
COVERAGE_METHOD="with test failures"
# Copy the overall coverage file for artifacts
cp "${OVERALL_COVERAGE_FILE}" coverage.out 2>/dev/null || echo "mode: set" > coverage.out
else
echo "⚠ Method 1 failed, falling back to Method 2..."
COVERAGE_METHOD="fallback to combined files"
# Method 2: Combine individual coverage files from successful tests
COMBINED_COVERAGE="${WORK_DIR}/combined_coverage.out"
echo "mode: set" > "${COMBINED_COVERAGE}"
SUCCESSFUL_DIRS=0
TOTAL_LINES=0
COVERED_LINES=0
for TEST_DIR in ${TEST_DIRS}; do
DIR_NAME=$(echo ${TEST_DIR} | sed 's|^\./||')
COVERAGE_FILE="${WORK_DIR}/${DIR_NAME//\//_}_coverage.out"
if [[ -f "${COVERAGE_FILE}" ]] && [[ -s "${COVERAGE_FILE}" ]]; then
# Skip the mode line and append
tail -n +2 "${COVERAGE_FILE}" >> "${COMBINED_COVERAGE}" 2>/dev/null || true
SUCCESSFUL_DIRS=$((SUCCESSFUL_DIRS + 1))
# Extract coverage stats for better calculation
FUNC_OUTPUT=$($GO_BIN tool cover -func="${COVERAGE_FILE}" 2>/dev/null || true)
if echo "$FUNC_OUTPUT" | grep -q "total:"; then
TOTAL_LINE=$(echo "$FUNC_OUTPUT" | grep "total:")
# Get statements count and coverage percentage
STMT_TOTAL=$(echo "$TOTAL_LINE" | awk '{print $2}')
STMT_COVERAGE=$(echo "$TOTAL_LINE" | awk '{print $3}' | sed 's/%//')
if [[ -n "$STMT_TOTAL" ]] && [[ -n "$STMT_COVERAGE" ]] && [[ "$STMT_TOTAL" != "0" ]]; then
STMT_COVERED=$(echo "scale=0; $STMT_TOTAL * $STMT_COVERAGE / 100" | bc -l)
TOTAL_LINES=$(echo "$TOTAL_LINES + $STMT_TOTAL" | bc -l)
COVERED_LINES=$(echo "$COVERED_LINES + $STMT_COVERED" | bc -l)
fi
fi
fi
done
# Calculate overall coverage from combined files
if [[ -s "${COMBINED_COVERAGE}" ]] && [[ $SUCCESSFUL_DIRS -gt 0 ]]; then
# Try the direct method first
OVERALL_COVERAGE=$($GO_BIN tool cover -func="${COMBINED_COVERAGE}" | grep "total:" | awk '{print $3}' | sed 's/%//' 2>/dev/null || echo "")
# If that fails, use our manual calculation
if [[ -z "$OVERALL_COVERAGE" ]] && [[ $(echo "$TOTAL_LINES > 0" | bc -l) -eq 1 ]]; then
OVERALL_COVERAGE=$(echo "scale=1; $COVERED_LINES * 100 / $TOTAL_LINES" | bc -l)
fi
if [[ -z "${OVERALL_COVERAGE}" ]]; then
OVERALL_COVERAGE="0.0"
fi
echo "✓ Overall repository coverage (from $SUCCESSFUL_DIRS successful test directories): ${OVERALL_COVERAGE}%"
# Copy combined coverage for artifact
cp "${COMBINED_COVERAGE}" coverage.out 2>/dev/null || echo "mode: set" > coverage.out
else
echo "✗ No coverage data available from any method"
OVERALL_COVERAGE="0.0"
echo "mode: set" > coverage.out
COVERAGE_METHOD="no data available"
fi
fi
else
echo "✗ All coverage calculation methods failed"
OVERALL_COVERAGE="0.0"
echo "mode: set" > coverage.out
COVERAGE_METHOD="all methods failed"
fi
fi
# Display results
echo "Coverage threshold: ${COV_THRESHOLD}%"
echo "Calculation method: ${COVERAGE_METHOD}"
# Normalize both values to 1 decimal place to avoid precision issues
# (e.g., 64.29 displays as 64.3 but fails >= 64.3)
COVERAGE_NORMALIZED=$(printf '%.1f' "${OVERALL_COVERAGE}")
THRESHOLD_NORMALIZED=$(printf '%.1f' "${COV_THRESHOLD}")
# Track coverage status separately from test status
COVERAGE_PASSED="true"
if (( $(echo "${COVERAGE_NORMALIZED} >= ${THRESHOLD_NORMALIZED}" | bc -l) )); then
echo -e "${GREEN}✓ Overall coverage PASSED threshold${NC}"
else
echo -e "${RED}✗ Overall coverage FAILED threshold${NC}"
COVERAGE_PASSED="false"
OVERALL_EXIT_CODE=1
fi
# Generate coverage reports for saving
# Note: Status reflects COVERAGE check only, not test failures
echo "## Test Coverage Report" > coverage_report.txt
echo "" >> coverage_report.txt
echo "**Overall Coverage:** ${COVERAGE_NORMALIZED}%" >> coverage_report.txt
echo "**Threshold:** ${THRESHOLD_NORMALIZED}% (applies to overall coverage only)" >> coverage_report.txt
echo "**Status:** $(if [[ "${COVERAGE_PASSED}" == "true" ]]; then echo "PASSED"; else echo "FAILED"; fi)" >> coverage_report.txt
echo "" >> coverage_report.txt
# FIXED: Use safer array check that works with set -u
if [[ "${#FAILED_TEST_DETAILS[@]}" -gt 0 ]] 2>/dev/null; then
echo "**Failed Tests:**" >> coverage_report.txt
for detail in "${FAILED_TEST_DETAILS[@]}"; do
echo " • ${detail}" >> coverage_report.txt
done
echo "" >> coverage_report.txt
fi
echo "**Note:** Directory PASS/FAIL indicates test results only, not coverage." >> coverage_report.txt
echo "**Note:** Coverage threshold applies to overall repository coverage only." >> coverage_report.txt
echo "" >> coverage_report.txt
echo "| Directory | Coverage | Result |" >> coverage_report.txt
echo "|-------------------------------------|----------|----------|" >> coverage_report.txt
# Recreate the table for the report
for GO_DIR in ${ALL_GO_DIRS}; do
DIR_NAME=$(echo ${GO_DIR} | sed 's|^\./||')
COVERAGE_FILE="${WORK_DIR}/${DIR_NAME//\//_}_coverage.out"
TEST_LOG="${WORK_DIR}/${DIR_NAME//\//_}_test.log"
# Check if this directory has test files
if echo "${TEST_DIRS}" | grep -q "^${GO_DIR}$"; then
# Directory has tests
if [[ -f "${COVERAGE_FILE}" ]] && [[ -s "${COVERAGE_FILE}" ]]; then
COVERAGE_PCT=$($GO_BIN tool cover -func="${COVERAGE_FILE}" | grep "total:" | awk '{print $3}' | sed 's/%//')
if [[ -z "${COVERAGE_PCT}" ]]; then
COVERAGE_PCT="0.0"
fi
# Check if tests passed by looking at the test log
if grep -q "PASS" "${TEST_LOG}" && ! grep -q "FAIL" "${TEST_LOG}"; then
STATUS="PASS"
else
STATUS="FAIL"
fi
printf "| %-35s | %8s%% | %-8s |\n" \
"${DIR_NAME}" "${COVERAGE_PCT}" "${STATUS}" >> coverage_report.txt
else
# Tests failed or no coverage generated - check if any coverage was generated
COVERAGE_DISPLAY="N/A"
if [[ -f "${COVERAGE_FILE}" ]] && [[ -s "${COVERAGE_FILE}" ]]; then
COVERAGE_FROM_FAILED=$($GO_BIN tool cover -func="${COVERAGE_FILE}" | grep "total:" | awk '{print $3}')
if [[ -n "${COVERAGE_FROM_FAILED}" ]]; then
COVERAGE_DISPLAY="${COVERAGE_FROM_FAILED}"
fi
fi
printf "| %-35s | %8s | %-8s |\n" \
"${DIR_NAME}" "${COVERAGE_DISPLAY}" "FAIL" >> coverage_report.txt
fi
else
# Directory has no tests
if [[ "${FAIL_ON_NO_TESTS}" == "true" ]]; then
printf "| %-35s | %8s | %-8s |\n" \
"${DIR_NAME}" "N/A" "FAIL" >> coverage_report.txt
else
printf "| %-35s | %8s | %-8s |\n" \
"${DIR_NAME}" "N/A" "NO-TESTS" >> coverage_report.txt
fi
fi
done
echo ""
if [[ ${OVERALL_EXIT_CODE} -eq 0 ]]; then
echo -e "${GREEN}🎉 All tests passed!${NC}"
echo -e "${GREEN}🎉 Overall coverage requirements met!${NC}"
else
if [[ -n "${FAILED_DIRS}" ]]; then
echo -e "${RED}❌ Test failures detected in the following directories:${NC}"
# FIXED: Use safer array check for displaying failed test details
if [[ "${#FAILED_TEST_DETAILS[@]}" -gt 0 ]] 2>/dev/null; then
for detail in "${FAILED_TEST_DETAILS[@]}"; do
echo -e "${RED} • ${detail}${NC}"
done
fi
echo ""
echo -e "${RED}❌ Build will FAIL due to test failures${NC}"
fi
if (( $(echo "${OVERALL_COVERAGE} < ${COV_THRESHOLD}" | bc -l) )); then
echo -e "${RED}❌ Overall coverage (${OVERALL_COVERAGE}%) below threshold (${COV_THRESHOLD}%)${NC}"
echo -e "${RED}❌ Build will FAIL due to insufficient coverage${NC}"
fi
fi
echo ""
echo "Note: All tests are executed even if some fail (for complete visibility)"
echo "Note: Directory PASS/FAIL is based on test results only, not coverage."
echo "Note: Overall coverage threshold (${COV_THRESHOLD}%) applies to repository total."
echo "Note: Directories without tests are marked as NO-TESTS and $(if [[ "${FAIL_ON_NO_TESTS}" == "true" ]]; then echo "DO"; else echo "DO NOT"; fi) cause build failure"
echo ""
echo "Generated files:"
echo " - coverage.out: Combined coverage profile (use with 'go tool cover')"
echo " - coverage_report.txt: Human-readable coverage summary"
exit ${OVERALL_EXIT_CODE}