Skip to content

Parse coverage to Cobertura format and upload them #794

Parse coverage to Cobertura format and upload them

Parse coverage to Cobertura format and upload them #794

Workflow file for this run

name: QA & sanity checks
on:
push:
branches:
- main
tags:
- "*"
pull_request:
jobs:
go-sanity:
name: "Go: Code sanity"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Go code sanity check
uses: canonical/desktop-engineering/gh-actions/go/code-sanity@main
with:
golangci-lint-configfile: ".golangci.yaml"
tools-directory: "tools"
go-tests:
name: "Go: Tests"
runs-on: ubuntu-24.04 # ubuntu-latest-runner
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Install dependencies
run: |
set -eu
sudo apt update
sudo apt install -y git-delta
- name: Install coverage collection dependencies
run: |
set -eu
go install github.com/AlekSi/gocov-xml@latest
go install github.com/axw/gocov/gocov@latest
dotnet tool install -g dotnet-reportgenerator-globaltool
- name: Prepare tests artifacts path
run: |
set -eu
artifacts_dir=$(mktemp -d --tmpdir authd-test-artifacts-XXXXXX)
echo AUTHD_TEST_ARTIFACTS_DIR="${artifacts_dir}" >> $GITHUB_ENV
- name: Install gotestfmt and our wrapper script
uses: canonical/desktop-engineering/gh-actions/go/gotestfmt@main
- name: Run tests (with coverage collection)
run: |
set -eu
# The coverage is not written if the output directory does not exist, so we need to create it.
cov_dir="$(pwd)/coverage"
raw_cov_dir="${cov_dir}/raw_files"
codecov_dir="${cov_dir}/codecov"
mkdir -p "${raw_cov_dir}" "${codecov_dir}"
# Print executed commands to ease debugging
set -x
# Overriding the default coverage directory is not an exported flag of go test (yet), so
# we need to override it using the test.gocoverdir flag instead.
#TODO: Update when https://go-review.googlesource.com/c/go/+/456595 is merged.
go test -json -cover -covermode=set ./... -shuffle=on -args -test.gocoverdir="${raw_cov_dir}" 2>&1 | \
gotestfmt --logfile "${AUTHD_TEST_ARTIFACTS_DIR}/gotestfmt.cover.log"
# Convert the raw coverage data into textfmt so we can merge the Rust one into it
go tool covdata textfmt -i="${raw_cov_dir}" -o="${cov_dir}/coverage.out"
# Filter out the testutils package
grep -v -e "testutils" "${cov_dir}/coverage.out" >"${cov_dir}/coverage.out.filtered"
# Generate the Cobertura report for Go
gocov convert "${cov_dir}/coverage.out.filtered" | gocov-xml > "${cov_dir}/coverage.xml"
reportgenerator -reports:"${cov_dir}/coverage.xml" -targetdir:"${codecov_dir}" -reporttypes:Cobertura
# Store the coverage directory for the next steps
echo COVERAGE_DIR="${codecov_dir}" >> $GITHUB_ENV
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ${{ env.COVERAGE_DIR }}
- name: Upload coverage report as artifact
if: github.ref == 'refs/heads/main'
uses: actions/upload-artifact@v4
with:
name: coverage.zip
path: ${{ env.COVERAGE_DIR }}
- name: Upload test artifacts
if: failure()
uses: actions/upload-artifact@v4
with:
name: authd-${{ github.job }}-artifacts-${{ github.run_attempt }}
path: ${{ env.AUTHD_TEST_ARTIFACTS_DIR }}