Github Action - Python coverage

Hi,

Github actions use /home/runner/work/my-repo/my-repo as the run directory. coverage generated xml uses this directory as a source.

However, the SonarCloud action does not have access to it, which makes it not read the coverage report.

Issue:

INFO: Sensor Cobertura Sensor for Python coverage [python]
INFO: Python test coverage
INFO: Parsing report '/github/workspace/test-coverage.xml'
WARN: Invalid directory path in 'source' element: /home/runner/work/my-repo/my-repo
INFO: Sensor Cobertura Sensor for Python coverage [python] (done) | time=527ms

Coverage Report

<?xml version="1.0" ?>
<coverage branch-rate="0.3415" branches-covered="3803" branches-valid="11135" complexity="0" line-rate="0.6738" lines-covered="34579" lines-valid="51322" timestamp="1629295350749" version="5.5">
	<!-- Generated by coverage.py: https://coverage.readthedocs.io -->
	<!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd -->
	<sources>
		<source>/home/runner/work/my-repo/my-repo</source>
	</sources>
 ...

Workflow

name: Test

on:
  workflow_dispatch: # allow running the workflow manually
  pull_request:
    branches:
      - master
      - staging
      - dev

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0 
      - name: Cache dependency 
        uses: actions/cache@v2
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
      - name: Setup python environment # setting python environment to 3.x
        uses: actions/setup-python@v2
        with:
          python-version: '3.7'
      - name: Install requirements
        run: pip install -r requirements.txt
      - name: Run Test With Coverage
        run: coverage run --branch --source=$GITHUB_WORKSPACE ./manage.py test --noinput --parallel
      - name: Generate Coverage Report
        run: coverage xml -o test-coverage.xml
      - name: SonarCloud Scan
        uses: SonarSource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

As a workaround, I tried adding this to the workflow:

sed -i 's/home\/runner\/work\/my-repo\/my-repo/github\/workspace/' test-coverage.xml

and got this result in the SonarCloud log

INFO: Sensor Cobertura Sensor for Python coverage [python]
INFO: Python test coverage
INFO: Parsing report '/github/workspace/test-coverage.xml'
INFO: Sensor Cobertura Sensor for Python coverage [python] (done) | time=422ms

Which tells us that it was able to parse the file. However, there was still no coverage report:

Coverage and Duplications
No Coverage information No Coverage information (48.4% Estimated after merge)
0.0% Duplication (3.6% Estimated after merge)

What should I do?

1 Like

I don’t know if I can help. But I’m curious.

Does the content of /github/workspace/test-coverage.xml look correct?

I see the first command above specifies --source=..., and the second one doesn’t. Shouldn’t you do similarly in the second one?