SonarSource/sonar-scanning-examples

Path is NOT relative to repo in xccov-to-sonarqube-generic.sh

pabloarista opened this issue · 3 comments

When running the script xccov-to-sonarqube-generic.sh the XML attribute path in the node file shows the absolute path. This causes a problem for the code coverage and sonar reports no coverage at all. I manually updated the script to add a global variable:

root_dir="$(pwd)/"

and then in the convert_file function I just did a replace:

function convert_file {
    local xccovarchive_file="$1"
    local file_name="$2"
    local relative_file_name="${file_name/$root_dir/}"
    local xccov_options="$3"
    echo "  <file path=\"$relative_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>'
}

I hope at the very least this helps someone else who is having the same issue. I spent 2 days on this and finally figured it out by looking at the file and thinking it was logical that the coverage wasn't captured

Hey, I usually run this xccov-to-sonarqube-generic.sh script in a pipeline. And this pipeline downloads this script from the github source everytime. So changing the script everytime was kinda troublesome.
So I decided to just add another shell command to fix the coverage report xml file after it was generated.

For those interested, all you have to do is run something like

sed -i 's|'"$(pwd)"'||g' sonarqube-generic-coverage.xml

(TIP: if you use Mac OSX like I do, you may need to use sed -Ei '' 's|modern_regex||g' ...

Please see our latest updates including updated references to the improved xccov-to-sonarqube-generic.sh script (in this repository) and the Test coverage parameters documentation:

Swift with Xcode 13.3+

You can use the xccov-to-sonarqube-generic.sh script from the sonar-scanning-examples/swift-coverage project to convert output from Xcode 13.3's xccov tool to the Generic test data format.

See the community guide for help with importing your coverage or test data.

Swift with Xcode 7-13.2

sonar.swift.coverage.reportPaths

Path to the report generated by llvm-cov show. The path may be absolute or relative to the project root.

If you continue to have issues, please respond here and we can take a look together. Or just report it at Sonar Community for more eyes to look at it.