-
-
Notifications
You must be signed in to change notification settings - Fork 0
193 lines (166 loc) · 7.14 KB
/
Copy pathswift.yml
File metadata and controls
193 lines (166 loc) · 7.14 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
name: Swift CI
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]
jobs:
test:
name: Test and Coverage
runs-on: macos-15
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Swift
uses: swift-actions/setup-swift@v2
with:
swift-version: "6.0"
- name: Cache Swift packages
uses: actions/cache@v4
with:
path: .build
key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }}
restore-keys: |
${{ runner.os }}-spm-
- name: Build
run: swift build
- name: Run tests with coverage
id: tests
run: |
swift test --enable-code-coverage 2>&1 | tee test_output.txt
# Extract test count from output
TEST_COUNT=$(grep -oE "Test run with [0-9]+ tests" test_output.txt | grep -oE "[0-9]+" | head -1 || echo "0")
if [ -z "$TEST_COUNT" ] || [ "$TEST_COUNT" = "0" ]; then
# Try alternative pattern
TEST_COUNT=$(grep -oE "[0-9]+ tests? passed" test_output.txt | grep -oE "[0-9]+" | head -1 || echo "0")
fi
if [ -z "$TEST_COUNT" ] || [ "$TEST_COUNT" = "0" ]; then
# Try one more pattern
TEST_COUNT=$(grep -oE "passed.*[0-9]+ tests?" test_output.txt | grep -oE "[0-9]+" | head -1 || echo "0")
fi
echo "test_count=$TEST_COUNT" >> $GITHUB_OUTPUT
echo "📊 Test count: $TEST_COUNT"
if [ "$TEST_COUNT" = "0" ]; then
echo "⚠️ Could not extract test count, showing last 20 lines of output:"
tail -20 test_output.txt || true
fi
- name: Find coverage data
id: coverage
run: |
PROFDATA=$(find .build -name "*.profdata" | head -1)
if [ -z "$PROFDATA" ]; then
echo "❌ No coverage data found"
find .build -type f -name "*.profdata" || echo "No profdata files"
exit 1
fi
echo "profdata=$PROFDATA" >> $GITHUB_OUTPUT
echo "📊 Coverage data: $PROFDATA"
- name: Find test binary
id: binary
run: |
# Try multiple paths for test binary
TEST_BINARY=""
# Method 1: xctest bundle
TEST_BINARY=$(find .build -path "*/EKNetworkPackageTests.xctest/Contents/MacOS/EKNetworkPackageTests" 2>/dev/null | head -1)
# Method 2: Direct binary
if [ -z "$TEST_BINARY" ]; then
TEST_BINARY=$(find .build -name "*PackageTests" -type f -perm +111 2>/dev/null | grep -v ".dSYM" | head -1)
fi
# Method 3: Any test binary
if [ -z "$TEST_BINARY" ]; then
TEST_BINARY=$(find .build -name "*Tests" -type f -perm +111 2>/dev/null | grep -v ".dSYM" | head -1)
fi
if [ -z "$TEST_BINARY" ]; then
echo "❌ Test binary not found"
echo "Searching in .build:"
find .build -type f -perm +111 2>/dev/null | head -10 || true
exit 1
fi
echo "binary=$TEST_BINARY" >> $GITHUB_OUTPUT
echo "🧪 Test binary: $TEST_BINARY"
- name: Generate coverage report
run: |
PROFDATA="${{ steps.coverage.outputs.profdata }}"
TEST_BINARY="${{ steps.binary.outputs.binary }}"
# Detect architecture
ARCH=$(uname -m)
if [ "$ARCH" = "arm64" ]; then
ARCH_FLAG="-arch arm64"
else
ARCH_FLAG="-arch x86_64"
fi
echo "📈 Generating coverage report..."
xcrun llvm-cov show \
-instr-profile "$PROFDATA" \
"$TEST_BINARY" \
$ARCH_FLAG \
-format=text \
-ignore-filename-regex=".*Tests.*|.*Version\.swift|.*EKNetworkVersion\.swift|.*ProgressDelegate\.swift" \
> coverage_report.txt 2>&1 || {
echo "⚠️ Coverage report generation had issues, but continuing..."
echo "PROFDATA: $PROFDATA"
echo "TEST_BINARY: $TEST_BINARY"
echo "ARCH: $ARCH"
ls -la "$PROFDATA" || true
ls -la "$TEST_BINARY" || true
}
- name: Calculate coverage
id: calc
run: |
if [ ! -f coverage_report.txt ]; then
echo "❌ Coverage report not found"
exit 1
fi
# llvm-cov format: " line_number|execution_count|code"
# Count lines with execution data (lines that have |)
TOTAL_LINES=$(grep -E "^[[:space:]]*[0-9]+\|[[:space:]]*[0-9]+\|" coverage_report.txt 2>/dev/null | wc -l | tr -d ' ' || echo "0")
# Count covered lines (execution count > 0)
COVERED_LINES=$(grep -E "^[[:space:]]*[0-9]+\|[[:space:]]*[1-9][0-9]*\|" coverage_report.txt 2>/dev/null | wc -l | tr -d ' ' || echo "0")
if [ "$TOTAL_LINES" -eq 0 ]; then
echo "⚠️ No lines found in coverage report"
echo "First 20 lines of report:"
head -20 coverage_report.txt || true
exit 1
fi
# Calculate percentage
COVERAGE=$(awk "BEGIN {printf \"%.2f\", ($COVERED_LINES * 100) / $TOTAL_LINES}")
COVERAGE_INT=$(echo "$COVERAGE" | cut -d. -f1)
echo "total=$TOTAL_LINES" >> $GITHUB_OUTPUT
echo "covered=$COVERED_LINES" >> $GITHUB_OUTPUT
echo "coverage=$COVERAGE" >> $GITHUB_OUTPUT
echo "coverage_int=$COVERAGE_INT" >> $GITHUB_OUTPUT
echo ""
echo "✅ Code Coverage Report"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Total lines: $TOTAL_LINES"
echo "Covered lines: $COVERED_LINES"
echo "Coverage: ${COVERAGE}%"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
- name: Check coverage threshold
run: |
COVERAGE_INT="${{ steps.calc.outputs.coverage_int }}"
COVERAGE="${{ steps.calc.outputs.coverage }}"
if [ "$COVERAGE_INT" -lt 98 ]; then
echo "❌ Code coverage is ${COVERAGE}%, but required minimum is 98%"
exit 1
else
echo "✅ Code coverage is ${COVERAGE}% (meets 98% requirement)"
fi
- name: Upload coverage report
uses: actions/upload-artifact@v4
if: always()
with:
name: coverage-report
path: coverage_report.txt
retention-days: 30
- name: Create test summary
run: |
echo "## Test Results" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "✅ **Tests:** ${{ steps.tests.outputs.test_count }} passed" >> $GITHUB_STEP_SUMMARY
echo "📊 **Coverage:** ${{ steps.calc.outputs.coverage }}% (${{ steps.calc.outputs.covered }}/${{ steps.calc.outputs.total }} lines)" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Coverage Details" >> $GITHUB_STEP_SUMMARY
echo "- Total lines: ${{ steps.calc.outputs.total }}" >> $GITHUB_STEP_SUMMARY
echo "- Covered lines: ${{ steps.calc.outputs.covered }}" >> $GITHUB_STEP_SUMMARY
echo "- Coverage: ${{ steps.calc.outputs.coverage }}%" >> $GITHUB_STEP_SUMMARY