3/21/2020 Code Coverage Reports in SonarQube for Swift on macOS
Code Coverage Reports in SonarQube for Swift on
macOS
Laxman Sahni Follow
Jul 17, 2018 · 4 min read
System requirements
To install and run SonarQube, your development environment must meet these
minimum requirements:
Operating Systems: macOS Sierra 10.13.4 (64-bit)
Disk Space: 1.17 GB (does not include disk space for IDE/tools).
Prerequisites
JDK 1.8
Download JDK 1.8 installer ➜ Move to Applications folder
Get the SonarQube
1. Download the following installation bundle to get the latest stable release of the
SonarQube:
SonarQube 6.7.4
2. Extract the file in the desired location, e.g. ~/Downloads through macOS built-in
extracting tool/unzip CLI in terminal/third-party extracting tools
sudo chmod 777 ~/Downloads/sonarqube-6.7.4/
https://medium.com/@agavatar/code-coverage-reports-in-sonarqube-for-swift-on-macos-49797b6a8fea 1/8
3/21/2020 Code Coverage Reports in SonarQube for Swift on macOS
3. Start the SonarQube server:
~/Downloads/sonarqube-6.7.4/bin/macosx-universal-64/sonar.sh console
Exceptions like
Encountered an error running main:
java.nio.file.AccessDeniedException: ~/Downloads/sonarqube-
6.7.4/temp/conf/es/elasticsearch.yml
Exception of wrapper
may occur, delete contents of temp folder to fix it.
sudo rm -rf temp/*
SonarQube Scanner
1. Download the following installation bundle to get the latest stable release of the
SonarQube Scanner:
2. Expand the downloaded file into the directory of your choice. We’ll refer to it as
<install_directory> in the next steps. I used ~/Downloads .
https://medium.com/@agavatar/code-coverage-reports-in-sonarqube-for-swift-on-macos-49797b6a8fea 2/8
3/21/2020 Code Coverage Reports in SonarQube for Swift on macOS
3. Update the global settings to point to your SonarQube server by editing
<install_directory>/conf/sonar-scanner.properties :
#----- Default SonarQube server
#sonar.host.url=http://localhost:9000
SonarQube Server URL in sonar-scanner.properties
4. Add the <install_directory>/bin directory to your path.
<install_directory>/bin in PATH
5. You can verify your installation by opening a new shell and executing the command
sonar-scanner -h . You should get output like this:
usage: sonar-scanner [options]
Options:
-D,--define <arg> Define property
-h,--help Display help information
-v,--version Display version information
-X,--debug Produce execution debug output
xcpretty
gem install xcpretty
https://medium.com/@agavatar/code-coverage-reports-in-sonarqube-for-swift-on-macos-49797b6a8fea 3/8
3/21/2020 Code Coverage Reports in SonarQube for Swift on macOS
SwiftLint (HomeBrew installed) and
brew install swiftlint
Version 0.26.0 or above.
Tailor
brew install tailor
Version 0.11.1 or above.
slather
gem install slather
Version 2.4.0 or above.
slather version
lizard
sudo pip install lizard
SonarQube Plugin for Swift
Download the plugin binary into the $SONARQUBE_HOME/extensions/plugins
directory
https://medium.com/@agavatar/code-coverage-reports-in-sonarqube-for-swift-on-macos-49797b6a8fea 4/8
3/21/2020 Code Coverage Reports in SonarQube for Swift on macOS
plugin binary in $SONARQUBE_HOME/extensions/plugins folder
Copy run-sonar-swift.sh somewhere in your PATH. I copied it under
~/Downloads/sonar-scanner-3.2.0.1227-macosx/bin which is in PATH.
Restart the SonarQube server.
Configuration (once per project)
Copy sonar-project.properties in your Xcode project root folder (along your
.xcodeproj file)
Edit the sonar-project.properties file to match your Xcode iOS project
sonar-project.properties
# must be unique in a given SonarQube instance
sonar.projectKey=CodeSample:CodingAssessmentApp
sonar.projectName=CodingAssessmentApp
sonar.projectVersion=1.0
# Project description
sonar.projectDescription=Coding Assessment App
# Path to source directories
https://medium.com/@agavatar/code-coverage-reports-in-sonarqube-for-swift-on-macos-49797b6a8fea 5/8
3/21/2020 Code Coverage Reports in SonarQube for Swift on macOS
sonar.sources=CodingAssessmentApp
# Path to test directories (comment if no test)
sonar.tests=CodingAssessmentAppTests
# Destination Simulator to run surefire
# As string expected in destination argument of xcodebuild command
# Example = sonar.swift.simulator=platform=iOS Simulator,name=iPhone
6,OS=9.2
sonar.swift.simulator=platform=iOS Simulator,name=iPhone 8,OS=11.4
# Xcode project configuration (.xcodeproj)
# and use the later to specify which project(s) to include in the
analysis (comma separated list)
# Specify either xcodeproj or xcodeproj + xcworkspace
sonar.swift.project=CodingAssessmentApp.xcodeproj
sonar.swift.workspace=CodingAssessmentApp.xcworkspace
# Specify your appname.
# This will be something like "myApp"
# Use when basename is different from targeted scheme.
# Or when slather fails with 'No product binary found'
# sonar.swift.appName=myApp
# Scheme to build your application
sonar.swift.appScheme=CodingAssessmentApp
# Configuration to use for your scheme. if you do not specify that
the default will be Debug
sonar.swift.appConfiguration=Debug
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
https://medium.com/@agavatar/code-coverage-reports-in-sonarqube-for-swift-on-macos-49797b6a8fea 6/8
3/21/2020 Code Coverage Reports in SonarQube for Swift on macOS
# Paths to exclude from coverage report (surefire, 3rd party
libraries etc.)
# sonar.swift.excludedPathsFromCoverage=pattern1,pattern2
sonar.swift.excludedPathsFromCoverage=.*Tests.*
Launching an analysis
Run the run-sonar-swift.sh script from your Xcode project root folder. If you error run-
sonar-swift.sh: Permission denied ,
Permission denied
Run
chmod +x run-sonar-swift.sh
from path of directory of run-sonar-swift.sh
Swift Coverage Results
Open browser and navigate to URL http://localhost:9000. You should see your project in
dashboard.
SonarQube Dashboard
Click on the project to see coverage report.
https://medium.com/@agavatar/code-coverage-reports-in-sonarqube-for-swift-on-macos-49797b6a8fea 7/8
3/21/2020 Code Coverage Reports in SonarQube for Swift on macOS
Where to Go From Here?
You can generate code coverage reports in Xcode with a Ruby gem Slather.
As always, any feedback is appreciated, so feel free to comment down here or reach out on
twitter — and, as always,
iOS Sonarqube Swift Xcode Macos
About Help Legal
https://medium.com/@agavatar/code-coverage-reports-in-sonarqube-for-swift-on-macos-49797b6a8fea 8/8