Coverage Report on Swift Package Manager Project

SonarQube - * Enterprise Edition * Version 9.3 (build 51899)
Scanner - SonarQube Scanner 4.6.2.2747
Xcode Version 13.2 (13C90)
Apple LLVM version 13.0.0 (clang-1300.0.29.30)

We are generating a coverage report like so

  1. swift test --enable-code-coverage
  2. xcrun llvm-cov show
    -use-color=false
    -instr-profile=.build/debug/codecov/default.profdata
    -output-dir=Sonar
    .build/debug/NetworkingPackageTests.xctest/Contents/MacOS/NetworkingPackageTests

which results in a
Sonar/Index.txt
Sonar/coverage/…/… contains the directory structure with single source file coverage data

in sonar-project.properties we have configured

sonar.swift.coverage.reportPaths=Sonar/Index.txt

The following are also configured (details partially left out)
sonar.projectKey=
sonar.projectName=
sonar.projectVersion=
sonar.projectDescription=
sonar.language=swift
sonar.sources=./Sources
sonar.c.file.suffixes=-
sonar.cpp.file.suffixes=-
sonar.objc.file.suffixes=-
sonar.test.inclusions=.swift
sonar.exclusions=/Test/,**/
.xml,Pods//*,Reports//*
sonar.swift.appName=
sonar.sourceEncoding=UTF-8
sonar.swift.excludedPathsFromCoverage=.Tests.

After running sonar-scanner with the specified parameter
the jobs run successfully and we see all the files from the coverage directory but the overall coverage is reported 0% in the single source files all covered lines are marked red in SonarQube.

Do we miss something here?

Kind regards

Hey there.

sonar.swift.coverage.reportPath is the correct analysis parameter, rather than sonar.swift.coverage.reportPaths (note the final s isn’t present in the correct parameter). You’ll find the correct analysis parameter also documented here

Please note that these analysis parameters don’t have any effect (they’re either removed, or come from the community supported GitHub - Idean/sonar-swift: Open source Swift plugin for SonarQube (also supports Objective-C))

Morning.
Corrected the path parameter and run the job again. Here’s the result for the swift path.

INFO: Sensor Swift Code Quality and Security [swift]
INFO: 47 source files to be analyzed
INFO: 47/47 source files have been analyzed
WARN: Property ‘sonar.swift.coverage.reportPath’ is deprecated and will be soon ignored, use ‘sonar.swift.coverage.reportPaths’ instead.
INFO: Parse coverage report (Sonar/index.txt)
INFO: Sensor Swift Code Quality and Security [swift] (done) | time=2060ms

Any idea?

Something I noticed is that Coverage for a single file is shown with a red bar on the lines, which are covered by unit tests.

@Colin Could you comment?

It is somehow disappointing that getting coverage data in Sonarqube for iOS projects is such a pain.
Is it really not working?

SonarCloud (currently on 9.5)
Scanner: 4.7.0.2747
Xcode: 13.4.1 with Swift 5.6
Apple LLVM version 13.1.6 (clang-1316.0.21.2.5)

I have run into this problem myself. Our project builds a swift package and we’re not using xcodebuild to build and run tests. The following works for us, note I am using sonar.swift.coverage.reportPaths option, Previously suggested option sonar.swift.coverage.reportPath is deprecated and I get a warning during the scan not to use it.

Build and run tests:

swift test --enable-code-coverage

Generate coverage report and save to a file:

xcrun llvm-cov show -instr-profile=.build/debug/codecov/default.profdata .build/debug/MyPackageTests.xctest/Contents/MacOS/MyPackageTests > coverage.report

Run the scan:

sonar-scanner -Dsonar.swift.coverage.reportPaths=coverage.report

THX.
I used
xcrun llvm-cov show
–use-color=false
–instr-profile=.build/debug/codecov/default.profdata
–output-dir=Sonar
.build/debug/NetworkingPackageTests.xctest/Contents/MacOS/NetworkingPackageTests
before.
Removing --output_dir option helped to get the coverage report.

Hi I am trying to analyze code coverage in my local spm with sonarQube. Unfortunately it does not work properly? It ignores the files in the spm to check the coverage code. Can you give me some tips? Thank you! Greetings, Doc Xray

sonar.projectKey=…

sonar.projectName=…

sonar.projectVersion=1

Define source and test directories

sonar.sources=localSPMs/SpmA/Sources/SpmA

sonar.tests=localSPMs/SpmA/Sources/SpmA/Tests/SpmATests

Simulator configuration for Swift (iOS)

sonar.swift.simulator=platform=iOS Simulator,name=iPhone 14 Pro Max,OS=17.0

sonar.swift.project=<…>.xcodeproj

sonar.swift.workspace=<…>.xcodeproj/<…>.xcworkspace

sonar.swift.appScheme=

Encoding and SCM settings

sonar.sourceEncoding=UTF-8

sonar.scm.disabled=true

Exclude patterns for code analysis and coverage

Including the exclusion of files ending in *View.swift

sonar.exclusions=/+CoreData.*,/GeneratedRequests.swift,/Assets.swift,/Fonts.swift,/Strings.swift,/Mock.swift,/*Tests.swift,/*View.swift

sonar.coverage.exclusions=/+CoreData.*,/GeneratedRequests.swift,/Assets.swift,/Fonts.swift,/Strings.swift,/*Tests.swift,/Mock.swift,/Spec.swift,**/*View.swift

Path to coverage report generated by your testing framework

sonar.coverageReportPaths=sonarqube-generic-coverage.xml

Additional paths and files to exclude from coverage to ensure focus is only on relevant production code

sonar.swift.excludedPathsFromCoverage=/+CoreData.*,/GeneratedRequests.swift,/Mock.swift,/Assets.swift,/Fonts.swift,/Strings.swift,**/*View.swift, **/*Tests.swift

Hey @doctor-xray

Please don’t bump a two year old thread. I will close this one. Feel free to raise a new one with the info requested in the template post.