From 9c7315a66a8381cca3b28c4fc8ed908eb8a5c125 Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Mon, 26 May 2025 17:15:21 -0600 Subject: [PATCH] CI: Add a test coverage job This uses grcov internally to generate the HTML report. We use grcov instead of, say, lcov, because grcov actually generates accessible pages. We use the gnome-build-meta:core-nightly container image. It doesn't contain grcov by default, but we download the binary from its github page. We can change this later to build a custom container image for gnome-calendar's own CI. This will publish the report on https://gnome.pages.gitlab.gnome.org/gnome-calendar/coverage/ --- .gitlab-ci.yml | 40 +++++++++++++++++++++++++++++++ .gitlab-ci/build-with-coverage.sh | 9 +++++++ .gitlab-ci/gen-coverage.sh | 14 +++++++++++ .gitlab-ci/install-grcov.sh | 9 +++++++ 4 files changed, 72 insertions(+) create mode 100755 .gitlab-ci/build-with-coverage.sh create mode 100755 .gitlab-ci/gen-coverage.sh create mode 100755 .gitlab-ci/install-grcov.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 19c903be..5538c75b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -18,6 +18,7 @@ variables: stages: - check - build + - analysis - deploy style-check-diff: @@ -48,3 +49,42 @@ nightly-aarch64: extends: '.publish_nightly' needs: ['build-flatpak-aarch64'] when: manual + +coverage: + stage: analysis + image: quay.io/gnome_infrastructure/gnome-build-meta:core-nightly + needs: + - job: build-flatpak + artifacts: false + script: + - .gitlab-ci/install-grcov.sh + - .gitlab-ci/build-with-coverage.sh + - .gitlab-ci/gen-coverage.sh + coverage: '/Coverage: \d+\.\d+/' + artifacts: + name: "gnome-calendar-${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}" + expire_in: 2 days + when: always + reports: + coverage_report: + coverage_format: cobertura + path: public/coverage.xml + paths: + - "_build/meson-logs" + - public + +pages: + stage: deploy + needs: [ coverage ] + script: + # No-op, just to gitlab thinks there's something to do. + # The jobs that this job depends on have already populated public/ + - echo + artifacts: + paths: + - public + rules: + # Restrict to the main branch so not every branch tries to deploy the web site + - if: ($CI_DEFAULT_BRANCH == $CI_COMMIT_BRANCH) + # Alternatively, restrict it to the GNOME namespace to avoid every fork pushing a set of pages by default + # - if: ($CI_DEFAULT_BRANCH == $CI_COMMIT_BRANCH && $CI_PROJECT_NAMESPACE == "GNOME") diff --git a/.gitlab-ci/build-with-coverage.sh b/.gitlab-ci/build-with-coverage.sh new file mode 100755 index 00000000..f54fddad --- /dev/null +++ b/.gitlab-ci/build-with-coverage.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +set -eux -o pipefail + +export CFLAGS="-coverage -ftest-coverage -fprofile-arcs" + +meson setup --buildtype=debug --prefix /usr _build . +meson compile -C _build +meson test -C _build diff --git a/.gitlab-ci/gen-coverage.sh b/.gitlab-ci/gen-coverage.sh new file mode 100755 index 00000000..cfc78e24 --- /dev/null +++ b/.gitlab-ci/gen-coverage.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +set -eux -o pipefail + +mkdir -p public +grcov _build --source-dir ./ --prefix-dir ../ --output-type cobertura --branch --ignore-not-existing -o public/coverage.xml +grcov _build --source-dir ./ --prefix-dir ../ --output-type html --branch --ignore-not-existing -o public/coverage + +# Print "Coverage: 42.42" so .gitlab-ci.yml will pick it up with a regex +# +# We scrape this from the HTML report, not the JSON summary, because coverage.json +# uses no decimal places, just something like "42%". + +grep -Eo 'abbr title.* %' public/coverage/index.html | head -n 1 | grep -Eo '[0-9.]+ %' | grep -Eo '[0-9.]+' | awk '{ print "Coverage:", $1 }' diff --git a/.gitlab-ci/install-grcov.sh b/.gitlab-ci/install-grcov.sh new file mode 100755 index 00000000..ae654c72 --- /dev/null +++ b/.gitlab-ci/install-grcov.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +set -eux -o pipefail + +wget https://github.com/mozilla/grcov/releases/download/v0.10.0/grcov-x86_64-unknown-linux-gnu.tar.bz2 +tar xvf grcov-x86_64-unknown-linux-gnu.tar.bz2 +mkdir -p /usr/local/bin +mv grcov /usr/local/bin/grcov +