# Code coverage not calculated with Swift and Github Actions

**URL:** <https://community.sonarsource.com/t/code-coverage-not-calculated-with-swift-and-github-actions/59442>\
**Category:** SonarQube Cloud\
**Tags:** swift, coverage, sonarqube-cloud\
**Created:** [March 7, 2022, 9:41am UTC](https://community.sonarsource.com/t/code-coverage-not-calculated-with-swift-and-github-actions/59442 "2022-03-07T09:41:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AmmarArnt](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ammararnt/32/30106_2.png) [@AmmarArnt](https://community.sonarsource.com/u/AmmarArnt)\
**Post date:** [March 7, 2022, 9:41am UTC](https://community.sonarsource.com/t/code-coverage-not-calculated-with-swift-and-github-actions/59442/1 "2022-03-07T09:41:12Z")

</div>

- ALM used (GitHub)
- CI system used (Github Actions)
- Scanner command used when applicable:

```nohighlight
 uses: SonarSource/sonarcloud-github-action@master
 env:
     GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
     SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

```

sonar-project.properties file:  
sonar.projectKey=xxx-ios

sonar.organization=xxx-ios-app  
sonar.c.file.suffixes=-  
sonar.cpp.file.suffixes=-  
sonar.objc.file.suffixes=-  
sonar.coverageReportPaths=sonar-generic-coverage.xml  
sonar.sources=.  
sonar.exclusions=Tests/\*\*  
sonar.tests=Tests  
sonar.test.inclusions=Tests/\*.swift  
sonar.verbose=true

- Languages of the repository: Swift

- Steps to reproduce:  
I have a github actions workflow job on a PR which executes the following to generate the code coverage report (uses xcode 13.2.1):  
`xcodebuild test-without-building -scheme "$scheme" -"$filetype_parameter" "$file_to_build" -destination "platform=$platform,name=$device" -enableCodeCoverage YES -resultBundlePath "CodeCoverage.xcresult"`

Then the report is transformed using:  
`bash xcov-to-sonar-generic.sh CodeCoverage.xcresult > sonar-generic-coverage.xml`

Since SonarScanner only runs on linux, and my xcodebuild runs on mac, I export the artifact for another github actions job:

```nohighlight
uses: actions/upload-artifact@v2
with:
    name: code-coverage
    path: sonar-generic-coverage.xml

```

And in the following github actions job I execute the following:

```nohighlight
  sonar:
    name: Run SonarCloud scan
    needs: build
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
      - name: Download code coverage 
        uses: actions/download-artifact@v2
        with:
          name: code-coverage
      - name: Scan
        uses: SonarSource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

```

However, I do not get the coverage in the scan analysis results, it stays at 0%. This is the log I get:

INFO: Sensor Generic Coverage Report  
INFO: Parsing /github/workspace/sonar-generic-coverage.xml  
INFO: Imported coverage data for 0 files  
INFO: Coverage data ignored for 159 unknown files, including:  
/Users/runner/work/ios/ios/qcast/Cells/Auth/AuthTextFieldTableCell.swift

My folder structure is as follows:

rootDir:  
 → qcast  
 → qcast.xcodeproj  
 → qcast.xcworkspace  
 → Tests

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [March 7, 2022, 12:32pm UTC](https://community.sonarsource.com/t/code-coverage-not-calculated-with-swift-and-github-actions/59442/2 "2022-03-07T12:32:35Z")

</div>

Hey there.

You’ll usually run into some trouble trying to upload a coverage report generated on another machine. You might be interested in what this user did here (using `macos-latest` and instead of using the GitHub action, executing the scanner manually).

> [@SonarCloud ran by github-actions doesn't generate code coverage for swift project](https://community.sonarsource.com/t/sonarcloud-ran-by-github-actions-doesnt-generate-code-coverage-for-swift-project/29620/3):
>
> You can always brew sonar-scanner and run it directly. To generate code coverage you need to build, test and generate coverage report. We use slather for this. [Here is how](https://github.com/Adyen/adyen-ios/blob/develop/.github/workflows/pr_scan.yml)

At the same time, I don’t know if this is really how we want this use-case to be handled, so I’ll ping internally.

---

<div class="post-metadata">

**Author:** ![AmmarArnt](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ammararnt/32/30106_2.png) [@AmmarArnt](https://community.sonarsource.com/u/AmmarArnt)\
**Post date:** [March 7, 2022, 2:36pm UTC](https://community.sonarsource.com/t/code-coverage-not-calculated-with-swift-and-github-actions/59442/4 "2022-03-07T14:36:06Z")

</div>

I’ve seen this slather discussion, but didn’t like the manual approach of using a bunch of tools explicitly.  
I guess I’ll give it a try then, given that there is no workaround.

However, I am hoping for a cleaner fix, by using the sonar-scanner github actions instead.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [March 15, 2022, 9:50am UTC](https://community.sonarsource.com/t/code-coverage-not-calculated-with-swift-and-github-actions/59442/7 "2022-03-15T09:50:24Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
