Skip to content

Commit 2bf6333

Browse files
committed
feat(ci): add a reusable C++ coverage workflow
1 parent edc3732 commit 2bf6333

2 files changed

Lines changed: 151 additions & 1 deletion

File tree

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
name: CI test C++ coverage
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
build-command:
7+
description: Shell commands that build the C++ code with coverage instrumentation, for example "make -C src coverage".
8+
required: true
9+
type: string
10+
filters:
11+
description: gcovr filters, one regular expression per line, selecting the source files to report.
12+
required: true
13+
type: string
14+
flag-name:
15+
description: Name for this coverage report in Coveralls. The caller closes the parallel build with ci-coveralls-finish.
16+
type: string
17+
default: cpp
18+
python-version:
19+
description: Python version used to install the package and run the tests.
20+
type: string
21+
default: "3.14"
22+
conda-packages:
23+
description: >-
24+
Space-separated conda package specs to install, for example programs
25+
the tests call that pip cannot install. Leave empty to skip conda.
26+
type: string
27+
default: ""
28+
channels:
29+
description: Comma-separated conda channels, in priority order.
30+
type: string
31+
default: conda-forge
32+
extras:
33+
description: Optional-dependency group installed with the package. Leave empty to install the package alone.
34+
type: string
35+
default: testing
36+
pre-test-command:
37+
description: Shell commands run in the same step as pytest, for example exporting the paths of the instrumented binaries.
38+
type: string
39+
default: ""
40+
pytest-args:
41+
description: Extra arguments passed to pytest.
42+
type: string
43+
default: ""
44+
test-path:
45+
description: Path passed to pytest.
46+
type: string
47+
default: tests
48+
gcovr-root:
49+
description: gcovr root directory.
50+
type: string
51+
default: "."
52+
gcovr-args:
53+
description: Extra arguments passed to gcovr.
54+
type: string
55+
default: ""
56+
timeout-minutes:
57+
description: Timeout for the job.
58+
type: number
59+
default: 30
60+
61+
permissions:
62+
contents: read
63+
64+
jobs:
65+
cpp-coverage:
66+
name: Test
67+
runs-on: ubuntu-26.04
68+
timeout-minutes: ${{ inputs.timeout-minutes }}
69+
permissions:
70+
contents: read
71+
checks: write
72+
statuses: write
73+
defaults:
74+
run:
75+
shell: bash -el {0}
76+
77+
steps:
78+
- name: Checkout
79+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
80+
with:
81+
persist-credentials: false
82+
83+
- name: Set up Python ${{ inputs.python-version }}
84+
uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
85+
with:
86+
python-version: ${{ inputs.python-version }}
87+
88+
- name: Set up conda
89+
if: inputs.conda-packages != ''
90+
uses: conda-incubator/setup-miniconda@be893c923ea9cf1cf7cd510fbdde27c7e18cbdcb # v4.1.0
91+
with:
92+
miniforge-variant: Miniforge3
93+
python-version: ${{ inputs.python-version }}
94+
activate-environment: testing
95+
channels: ${{ inputs.channels }}
96+
auto-activate: false
97+
98+
- name: Install conda packages
99+
if: inputs.conda-packages != ''
100+
env:
101+
PYTHON_VERSION: ${{ inputs.python-version }}
102+
PACKAGES: ${{ inputs.conda-packages }}
103+
CHANNELS: ${{ inputs.channels }}
104+
run: |
105+
channel_args=()
106+
IFS=',' read -ra channel_list <<< "${CHANNELS}"
107+
for channel in "${channel_list[@]}"; do
108+
channel_args+=(-c "${channel}")
109+
done
110+
111+
read -r -a package_list <<< "${PACKAGES}"
112+
conda install -y "${channel_args[@]}" "python=${PYTHON_VERSION}" "${package_list[@]}"
113+
114+
- name: Install the package
115+
env:
116+
EXTRAS: ${{ inputs.extras }}
117+
run: |
118+
python -m pip install --upgrade pip
119+
if [ -n "${EXTRAS}" ]; then
120+
python -m pip install -e ".[${EXTRAS}]"
121+
else
122+
python -m pip install -e .
123+
fi
124+
125+
- name: Build with coverage instrumentation
126+
env:
127+
BUILD_COMMAND: ${{ inputs.build-command }}
128+
run: eval "${BUILD_COMMAND}"
129+
130+
- name: Run tests
131+
env:
132+
TEST_PATH: ${{ inputs.test-path }}
133+
PYTEST_ARGS: ${{ inputs.pytest-args }}
134+
PRE_TEST_COMMAND: ${{ inputs.pre-test-command }}
135+
run: |
136+
if [ -n "${PRE_TEST_COMMAND}" ]; then
137+
eval "${PRE_TEST_COMMAND}"
138+
fi
139+
140+
read -r -a extra_args <<< "${PYTEST_ARGS}"
141+
python -m pytest "${extra_args[@]}" "${TEST_PATH}"
142+
143+
- name: Upload coverage to Coveralls
144+
uses: CCPBioSim/.github/actions/cpp-coverage-upload@edc3732fa8d31a58312ca784e65cad9bdfba9347 # 0.13.0
145+
with:
146+
filters: ${{ inputs.filters }}
147+
flag-name: ${{ inputs.flag-name }}
148+
root: ${{ inputs.gcovr-root }}
149+
gcovr-args: ${{ inputs.gcovr-args }}

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Callers pin a full commit SHA with the version in a trailing comment, so Renovat
6464
|---|---|---|
6565
| `ci-test-python` | Run pytest on a Python and OS matrix, report coverage to Coveralls in a separate `Coverage` job, and optionally re-run the tests at the lowest versions that `pyproject.toml` allows (`lowest-dependencies-check`) | `package`, and `checks` and `statuses` write |
6666
| `ci-test-conda` | The same for conda-based setups such as AiiDA plugins | `package`, `conda-packages`, and `checks` and `statuses` write |
67+
| `ci-test-cpp-coverage` | Build C++ code with coverage instrumentation, run the tests against it, and upload the C++ report to Coveralls as a named parallel build | `build-command`, `filters`, and `checks` and `statuses` write. Close the build with `ci-coveralls-finish` |
6768
| `ci-docs` | Build Sphinx docs with warnings as errors | Nothing required |
6869
| `ci-pre-commit` | Run all pre-commit hooks | Nothing required |
6970
| `ci-conda-build` | Build a conda recipe without uploading it | Nothing required |
@@ -76,7 +77,7 @@ Callers pin a full commit SHA with the version in a trailing comment, so Renovat
7677

7778
Coverage is measured on one leg only, the first runner in `os` on `coverage-python-version`, or on the first entry of `python-versions` when that version is not in the list. That leg saves `coverage.xml` as an artifact, and the `Coverage` job uploads it to Coveralls, so the other legs run without the coverage overhead. Coverage is on by default in `ci-test-python` and off in `ci-test-conda`, so a conda-based caller sets `coverage: true` where it wants a report. A caller that runs a test workflow from a matrix gives each leg its own `coverage-flag` and closes the build with `ci-coveralls-finish`, so the reports have distinct names and combine in Coveralls.
7879

79-
The `actions/cpp-coverage-upload` composite action turns gcov data into an lcov report and uploads it to Coveralls as a named parallel build, so C++ coverage combines with Python coverage. `ci-test-python` and `ci-test-conda` take `pre-test-command`, and `ci-docs` takes `pre-build-command`, for steps that must run first, such as downloading data or generating files. The release workflows also take `dry-run`, which runs every step except the ones that publish or change the repository.
80+
`ci-test-cpp-coverage` measures C++ coverage for packages with compiled code. It installs the package, and any `conda-packages` the tests need, runs `build-command` to build the instrumented code, runs pytest, and uploads the C++ report through the `actions/cpp-coverage-upload` composite action. To report combined coverage, a caller sets `coverage-flag` on `ci-test-python` or `ci-test-conda`, runs `ci-test-cpp-coverage` alongside it, and closes the build with `ci-coveralls-finish` once both have finished. Do this on pull requests as well as on `main`, so Coveralls compares the same total. `ci-test-python` and `ci-test-conda` take `pre-test-command`, and `ci-docs` takes `pre-build-command`, for steps that must run first, such as downloading data or generating files. The release workflows also take `dry-run`, which runs every step except the ones that publish or change the repository.
8081

8182
Workflows in this repository are audited with `zizmor` through pre-commit, which runs in CI through `ci-pre-commit`.
8283

0 commit comments

Comments
 (0)