We are using SonarCloud with my new employer and I’m facing a pretty strange issue with code coverage calculation on new code.
My Objective-C project is up and running, I’ve manually analyzed the master and develop branches with sonar-scanner. The master branch has 0% coverage from Xcode and the develop branches reaches 98.8%. SonarCloud, for both branches, reported that the coverage was 0% since I didn’t run the slather command to give them a generic coverage file.
So I’ve recently opened a PR from develop to fix some bugs & code smells reported by SonarCloud and I’ve also added a new UT to reach the 100% coverage on Xcode. And since the CI is now handling the whole thing, it runs the slather action by itself with fastlane.
Unfortunately, I can’t merge my PR because SonarCloud reports a 0% coverage on new code:
If all was fine with your manual branch analyses but you’re facing an issue when it’s executed in the CI… I’m guessing your manual runs were better configured!
In your analysis (scanner) logs you should find some log lines like this:
INFO: Sensor Generic Coverage Report
INFO: Parsing /Users/colin/Source/sonar-scanning-examples/swift-coverage/swift-coverage-example/sonarqube-generic-coverage.xml
INFO: Imported coverage data for 0 files
INFO: Coverage data ignored for 3 unknown files, including:
/Users/colin/Source/sonar-scanning-examples/swift-coverage/swift-coverage-example/swift-coverage-exampleTests/swift_coverage_exampleTests.swift
/Users/colin/Source/sonar-scanning-examples/swift-coverage/swift-coverage-example/swift-coverage-example/AppDelegate2.swift
/Users/colin/Source/sonar-scanning-examples/swift-coverage/swift-coverage-example/swift-coverage-example/AppDelegate.swift
INFO: Sensor Generic Coverage Report (done) | time=65ms
Hm. If it was actually finding any coverage at all, I would expect to see some coverage listed for Estimated after merge in your screenshot.
That’s an interesting point. So the 1.3k New Lines to Cover seeen in your screenshot is just… wrong? What code changes do you expect to see in the PR, and how does it compare to the Code tab of your pull request?
Okay! In that case, I would make sure that you have set sonar.tests correctly in your sonar-project.properties file so those Tests aren’t counted as New Lines of Code (and as code to be covered).
It doesn’t quite explain 0% estimated coverage after merge. But what I would suggest is testing that out on a new branch where you’ve actually added some source code.
Is your CI responsible for running your main branch analysis yet, or only PRs? You mentioned before you had coverage successfully imported on some manual runs.
What is really weird is that by adding dummy code, I managed to make it work and it went from 0% to 100% coverage on new code last time I tried… But it seems that I cannot make it work again.
Welp… nothing new, coverage is still at a whopping 0% even though Xcode reports a 100% and the generic coverage file is stating that each single line is covered