Skip to content

Commit

Permalink
Apply changes suggested in PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
francoismora committed Jan 30, 2023
1 parent c180926 commit 8abd30c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 42 deletions.
33 changes: 16 additions & 17 deletions swift-coverage/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# SonarQube-Scanner for Swift Code Coverage

This example demonstrates how to import Xcode Coverage data (aka ProfData) to SonarQube for a Swift project. It supports Xcode 13 and above.
This example demonstrates how to import Xcode Coverage data to SonarQube for a Swift project. It supports Xcode 13 and above.

## Prerequisites

* [SonarQube](http://www.sonarqube.org/downloads/) 9.8 or Latest
* [SonarQube Scanner](http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner) 4.8+
* [SonarQube](http://www.sonarqube.org/downloads/) 8.9 LTS or Latest
* [SonarQube Scanner](http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner) 4.7+

## Usage

Expand All @@ -22,42 +22,41 @@ xcodebuild -project swift-coverage-example.xcodeproj/ -scheme swift-coverage-exa
The `xccov` command line tool is the recommended option to view Xcode coverage
data and is more straightforward to use than the older `llvm-cov` tool. With
the script `xccov-to-sonarqube-generic.sh`, you can convert Xcode test results
stored in `*.xcresult` folders to a SonarQube generic XML file and then pass it
to the Sonar Scanner to get your Xcode coverage data into SonarQube.
stored in `*.xcresult` folders to the [SonarQube generic test coverage format](https://docs.sonarqube.org/latest/analyzing-source-code/test-coverage/generic-test-data/).

First, convert your Xcode coverage to the SonarQube generic format with the
First, convert your Xcode coverage to the SonarQube format with the
command:

```shell
bash xccov-to-sonarqube-generic.sh Build/Logs/Test/*.xcresult/ >Coverage.xml
```

Then, configure the report file with the `sonar.coverageReportPaths` option
when executing sonar-scanner:
Then, use the parameter `sonar.coverageReportPaths` to reference the generated report:

```shell
sonar-scanner -Dsonar.projectKey=TestCoverage -Dsonar.coverageReportPaths=Coverage.xml
sonar-scanner -Dsonar.coverageReportPaths=Coverage.xml
```

This parameter accepts a comma-separated list of files, which means you can also provide multiple coverage reports from multiple test results.

1.b Using llvm-cov

You can also provide code coverage data using the `llvm-cov` format. The
process of generating an llvm-cov report requires several steps to get the
coverage for the application executable and the dynamic library binaries. For
the project example, you can use the following command where `<id>` should
match the folder name under `ProfileData`:
coverage for the application executable and the dynamic library binaries.

In the case of the project example, first, locate the `Coverage.profdata` file under the `ProfileData` folder. Then, generate an `llvm-cov` report with the command:

```shell
xcrun --run llvm-cov show -instr-profile=Build/Build/ProfileData/<id>/Coverage.profdata \
xcrun --run llvm-cov show -instr-profile=Build/Build/ProfileData/xxxx/Coverage.profdata \
Build/Build/Products/Debug/swift-coverage-example.app/Contents/MacOS/swift-coverage-example \
>Coverage.report
```

Then, configure the report file with the `sonar.swift.coverage.reportPaths`
option when executing sonar-scanner:
Finally, use the parameter `sonar.swift.coverage.reportPaths` to reference the generated report. This parameter also accepts a comma-separated list of files.

```shell
sonar-scanner -Dsonar.projectKey=TestCoverage -Dsonar.sources=. -Dsonar.swift.coverage.reportPaths=Coverage.report
sonar-scanner -Dsonar.swift.coverage.reportPaths=Coverage.report
```

2. Verify that for the project "swift-coverage-example" the coverage value is > 65%.
2. Verify that for the project "swift-coverage-example" the coverage value is around 75%.
47 changes: 22 additions & 25 deletions swift-coverage/swift-coverage-example/xccov-to-sonarqube-generic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,18 @@
set -euo pipefail

function convert_xccov_to_xml {
sed -n \
-e '/:$/s/&/\&amp;/g;s/^\(.*\):$/ <file path="\1">/p' \
-e 's/^ *\([0-9][0-9]*\): 0.*$/ <lineToCover lineNumber="\1" covered="false"\/>/p' \
-e 's/^ *\([0-9][0-9]*\): [1-9].*$/ <lineToCover lineNumber="\1" covered="true"\/>/p' \
-e 's/^$/ <\/file>/p'
}

function convert_archive {
local xccovarchive_file="$1"
xcrun xccov view --archive "$xccovarchive_file" | convert_xccov_to_xml
sed -n \
-e '/:$/s/&/\&amp;/g;s/^\(.*\):$/ <file path="\1">/p' \
-e 's/^ *\([0-9][0-9]*\): 0.*$/ <lineToCover lineNumber="\1" covered="false"\/>/p' \
-e 's/^ *\([0-9][0-9]*\): [1-9].*$/ <lineToCover lineNumber="\1" covered="true"\/>/p' \
-e 's/^$/ <\/file>/p'
}

function xccov_to_generic {
local xcresult="$1"

echo '<coverage version="1">'
for xccovarchive_file in "$@"; do
if [[ ! -d $xccovarchive_file ]]; then
echo "Coverage file not found at path: $xccovarchive_file" 1>&2;
exit 1
elif (( xcode_version < 13 )); then
echo "Xcode version not supported ($xcode_version) version 13 or above is required" 1>&2;
exit 1
elif [[ $xccovarchive_file != *".xcresult"* ]]; then
echo "Incorrect test results path. Required *.xcresult: $xccovarchive_file" 1>&2;
exit 1
fi

convert_archive "$xccovarchive_file"
done
xcrun xccov view --archive "$xcresult" | convert_xccov_to_xml
echo '</coverage>'
}

Expand All @@ -48,4 +32,17 @@ if [ $? -ne 0 ]; then
exit -1
fi

xccov_to_generic "$@"
xcresult="$1"

if (( xcode_version < 13 )); then
echo "Xcode version not supported ($xcode_version) version 13 or above is required" 1>&2;
exit 1
elif [[ ! -d $xcresult ]]; then
echo "Coverage file not found at path: $xcresult" 1>&2;
exit 1
elif [[ $xcresult != *".xcresult"* ]]; then
echo "Expecting input to match '*.xcresult', got: $xcresult" 1>&2;
exit 1
fi

xccov_to_generic "$xcresult"

0 comments on commit 8abd30c

Please sign in to comment.