Skip to content

Commit 6d1f20d

Browse files
committedMar 28, 2025·
[INJIMOB-2885]: fix swift sonar coverage report generation
Signed-off-by: adityankannan-tw <[email protected]>
1 parent 6dbe574 commit 6d1f20d

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed
 

‎.github/workflows/swift-sonar-analysis.yml

+28-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ on:
66
description: "Swift package location"
77
required: true
88
type: string
9+
TEST_SCHEME:
10+
description: "Test scheme of the swift package"
11+
required: true
12+
type: string
913
SONAR_URL:
1014
required: false
1115
type: string
@@ -67,24 +71,34 @@ jobs:
6771
run: |
6872
cd ${{ inputs.SERVICE_LOCATION }}
6973
70-
# Check if Tests directory exists and is not empty
71-
if [ -d "Tests" ] && [ "$(ls -A Tests)" ]; then
72-
swift test --enable-code-coverage
73-
74-
# Verify coverage data exists before merging
75-
if [ ! -f .build/debug/codecov/default.profdata ]; then
76-
echo "Error: Coverage data file (.profdata) not found!"
77-
exit 1
74+
# Ensure Tests directory exists (to prevent SonarQube errors)
75+
if [ ! -d "Tests" ]; then
76+
echo "Tests directory is missing. Creating an empty Tests directory."
77+
mkdir Tests
7878
fi
7979
80-
# Merging coverage data
81-
xcrun llvm-profdata merge -sparse .build/debug/codecov/default.profdata -o .build/debug/codecov/profdata.profdata
80+
# Check if Tests directory exists and is not empty
81+
if [ -d "Tests" ] && [ "$(ls -A Tests)" ]; then
82+
83+
# Run tests
84+
xcodebuild test \
85+
-scheme "${{ inputs.TEST_SCHEME }}" \
86+
-destination 'platform=iOS Simulator,OS=latest,name=iPhone 15' \
87+
-enableCodeCoverage YES
88+
89+
# Find test bundle and executable
90+
TEST_BUNDLE=$(find ~/Library/Developer/Xcode/DerivedData -type d -name "*.xctest" | head -n 1)
91+
TEST_EXECUTABLE=$(find "$TEST_BUNDLE" -type f -perm -111 | head -n 1)
92+
COVERAGE_PATH=$(find ~/Library/Developer/Xcode/DerivedData -name "Coverage.profdata" | head -n 1)
8293
83-
# Generating coverage report
84-
xcrun --run llvm-cov show -instr-profile=.build/debug/codecov/profdata.profdata .build/debug/*PackageTests.xctest/Contents/MacOS/*PackageTests > coverage.report
94+
# Generate coverage report
95+
xcrun llvm-cov show \
96+
-instr-profile="$COVERAGE_PATH" \
97+
"$TEST_EXECUTABLE" \
98+
-format=text > coverage.report
8599
else
86-
echo "No tests found. Creating empty coverage report for sonar."
87-
touch coverage.report
100+
echo "No tests found. Creating empty coverage report for SonarQube."
101+
touch coverage.report
88102
fi
89103
90104
- name: Run Sonar Analysis

0 commit comments

Comments
 (0)
Please sign in to comment.