SONAR Qube Integration — iOS Projects (with Code Coverage)

Rakesh Chander
2 min readOct 23, 2020

XCode 11 & above

Static Code Analysis tool — SONAR — is being used by Organisations for tracking Code Reliability, Security, Maintainability & Technical Debt. We have Code Coverage synced on SONAr as well.

For iOS Projects, we can have this sync working by following below steps -

  • Download SONAR SCANNER at machine
  • Create sonar-project.properties file and place that at project root
  • Add below key-values in properties file -
sonar.host.url= 
sonar.links.ci=
sonar.links.scm=
sonar.projectVersion=1.0.0
sonar.sources=Pretups
sonar.cfamily.build-wrapper-output.bypass=true
sonar.projectName=
sonar.projectKey=
sonar.exclusions=Pods/**,**/Assets/**
  • Open Terminal & change directory to your project
  • Execute below command
bash $HOME/Documents/Sonar/sonar*/bin/sonar-scanner

You are DONE!! you can now check your SONAR Qube Portal for analysis details.

Code Coverage Sync

Xcode has its own file format for code coverage — xcresult.

SONAR doesn’t understand this file structure and this file needs to be mapped as per SONAR syntax. We need to convert xcresult file into required format.

  • Create a file xcode-coverage-mapper.sh and paste below code
#!/usr/bin/env bashset -euo pipefailfunction convert_file {local xccovarchive_file="$1"local file_name="$2"echo "  <file path=\"$file_name\">"local line_coverage_cmd="xcrun xccov view"if [[ $@ == *".xcresult"* ]]; thenline_coverage_cmd="$line_coverage_cmd --archive"filine_coverage_cmd="$line_coverage_cmd --file \"$file_name\" \"$xccovarchive_file\""eval $line_coverage_cmd | \sed -n 's/^ *\([0-9][0-9]*\): 0.*$/    <lineToCover lineNumber="\1" covered="false"\/>/p;s/^ *\([0-9][0-9]*\): [1-9].*$/    <lineToCover lineNumber="\1" covered="true"\/>/p'echo '  </file>'}function xccov_to_generic {echo '<coverage version="1">'for xccovarchive_file in "$@"; dolocal file_list_cmd="xcrun xccov view"if [[ $@ == *".xcresult"* ]]; thenfile_list_cmd="$file_list_cmd --archive"fifile_list_cmd="$file_list_cmd --file-list \"$xccovarchive_file\""eval $file_list_cmd | while read -r file_name; doconvert_file "$xccovarchive_file" "$file_name"donedoneecho '</coverage>'}xccov_to_generic "$@"
  • Execute Tests using below command (Note that derivedDataPath has been defined to make sure xcresult file is generated at defined location)
xcodebuild -workspace <WORKSPACE_NAME>.xcworkspace -scheme <SCHEME_NAME> clean test -destination 'platform=iOS Simulator,name=iPhone 11,OS=14.0'  -enableCodeCoverage YES  -derivedDataPath Build/
  • After executing Tests with Code Coverage enabled, execute below command
bash xcode-coverage-mapper.sh Build/Logs/Test/*.xcresult/ > sonarqube-generic-coverage.xml
  • Above command will generate a file named “sonarqube-generic-coverage.xml” at project root.
  • Make below entry in sonar-properties file
sonar.coverageReportPaths=sonarqube-generic-coverage.xml
  • Execute SONAR using sonar-scanner again as stated above

You’ll see that Code Coverage has also been synced and now you can see uncovered lines in SONAR portal.

--

--

Rakesh Chander

I believe in modular development practices for better reusability, coding practices, robustness & scaling — inline with automation.