Skip to content

Commit 0709c40

Browse files
Copilotvsanthanam
andcommitted
Filter coverage to only include project source files, exclude dependencies
Co-authored-by: vsanthanam <3914230+vsanthanam@users.noreply.github.com>
1 parent 3871089 commit 0709c40

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

.github/workflows/ci.yml

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,35 @@ jobs:
148148
COVERAGE_JSON=$(swift test --show-codecov-path)
149149
echo "Coverage report location: $COVERAGE_JSON"
150150
151-
# Extract coverage statistics
152-
LINES_PERCENT=$(jq -r '.data[0].totals.lines.percent' "$COVERAGE_JSON")
153-
LINES_COVERED=$(jq -r '.data[0].totals.lines.covered' "$COVERAGE_JSON")
154-
LINES_TOTAL=$(jq -r '.data[0].totals.lines.count' "$COVERAGE_JSON")
151+
# Filter coverage to only include project source files (exclude dependencies)
152+
# Count only files in the /Sources/ directory to exclude Swift package dependencies
153+
LINES_TOTAL=$(jq '[.data[0].files[] | select(.filename | contains("/Sources/")) | .summary.lines.count] | add // 0' "$COVERAGE_JSON")
154+
LINES_COVERED=$(jq '[.data[0].files[] | select(.filename | contains("/Sources/")) | .summary.lines.covered] | add // 0' "$COVERAGE_JSON")
155155
156-
FUNCTIONS_PERCENT=$(jq -r '.data[0].totals.functions.percent' "$COVERAGE_JSON")
157-
FUNCTIONS_COVERED=$(jq -r '.data[0].totals.functions.covered' "$COVERAGE_JSON")
158-
FUNCTIONS_TOTAL=$(jq -r '.data[0].totals.functions.count' "$COVERAGE_JSON")
156+
FUNCTIONS_TOTAL=$(jq '[.data[0].files[] | select(.filename | contains("/Sources/")) | .summary.functions.count] | add // 0' "$COVERAGE_JSON")
157+
FUNCTIONS_COVERED=$(jq '[.data[0].files[] | select(.filename | contains("/Sources/")) | .summary.functions.covered] | add // 0' "$COVERAGE_JSON")
159158
160-
REGIONS_PERCENT=$(jq -r '.data[0].totals.regions.percent' "$COVERAGE_JSON")
161-
REGIONS_COVERED=$(jq -r '.data[0].totals.regions.covered' "$COVERAGE_JSON")
162-
REGIONS_TOTAL=$(jq -r '.data[0].totals.regions.count' "$COVERAGE_JSON")
159+
REGIONS_TOTAL=$(jq '[.data[0].files[] | select(.filename | contains("/Sources/")) | .summary.regions.count] | add // 0' "$COVERAGE_JSON")
160+
REGIONS_COVERED=$(jq '[.data[0].files[] | select(.filename | contains("/Sources/")) | .summary.regions.covered] | add // 0' "$COVERAGE_JSON")
163161
164-
# Round percentages to 2 decimal places
165-
LINES_PERCENT=$(printf "%.2f" "$LINES_PERCENT")
166-
FUNCTIONS_PERCENT=$(printf "%.2f" "$FUNCTIONS_PERCENT")
167-
REGIONS_PERCENT=$(printf "%.2f" "$REGIONS_PERCENT")
162+
# Calculate percentages
163+
if [ "$LINES_TOTAL" -gt 0 ]; then
164+
LINES_PERCENT=$(echo "scale=2; ($LINES_COVERED * 100) / $LINES_TOTAL" | bc)
165+
else
166+
LINES_PERCENT="0.00"
167+
fi
168+
169+
if [ "$FUNCTIONS_TOTAL" -gt 0 ]; then
170+
FUNCTIONS_PERCENT=$(echo "scale=2; ($FUNCTIONS_COVERED * 100) / $FUNCTIONS_TOTAL" | bc)
171+
else
172+
FUNCTIONS_PERCENT="0.00"
173+
fi
174+
175+
if [ "$REGIONS_TOTAL" -gt 0 ]; then
176+
REGIONS_PERCENT=$(echo "scale=2; ($REGIONS_COVERED * 100) / $REGIONS_TOTAL" | bc)
177+
else
178+
REGIONS_PERCENT="0.00"
179+
fi
168180
169181
# Create markdown summary
170182
cat >> $GITHUB_STEP_SUMMARY << EOF

COVERAGE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ Code coverage is enabled in `.github/workflows/ci.yml` for macOS test jobs:
8484
run: swift test --enable-code-coverage
8585
```
8686
87+
The coverage report filters results to only include project source files (those in `/Sources/` directory), excluding Swift package dependencies. This ensures accurate coverage metrics for the project's own code.
88+
8789
### Codecov Configuration
8890

8991
The Codecov token is stored as a repository secret (`CODECOV_TOKEN`). For public repositories, this token is optional but recommended for better reliability and rate limits.

0 commit comments

Comments
 (0)