Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shakemno/a5402a3fb1b99d318a0ddb4ca25ab74e to your computer and use it in GitHub Desktop.
Save shakemno/a5402a3fb1b99d318a0ddb4ca25ab74e to your computer and use it in GitHub Desktop.
Extension to the sonarqube script for converting xcode code coverage to generic report found at: https://github.com/SonarSource/sonar-scanning-examples/tree/master/swift-coverage
#!/usr/bin/env bash
set -euo pipefail
function convert_file {
local xccovarchive_file="$1"
local file_name="$2"
local xccov_options="$3"
echo " <file path=\"$file_name\">"
xcrun xccov view $xccov_options --file "$file_name" "$xccovarchive_file" | \
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 "$@"; do
local xccov_options=""
if [[ $xccovarchive_file == *".xcresult/" ]]; then
xccov_options="--archive"
fi
xcrun xccov view $xccov_options --file-list "$xccovarchive_file" | while read -r file_name; do
if [[ $file_name == *" "* ]]; then
continue
fi
if [[ $file_name == *"/Pods/"* ]]; then
continue
fi
if [[ $file_name == *".m" ]] || [[ $file_name == *".h" ]]; then
continue
fi
convert_file "$xccovarchive_file" "$file_name" "$xccov_options"
done
done
echo '</coverage>'
}
xccov_to_generic "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment