Skip to content

Commit fad9d63

Browse files
authored
test: improve code coverage reporting (#4849)
* Speed up the generation of coverage reports by using multiple cores. * Add codecov step to coverage workflow.
1 parent efe6722 commit fad9d63

12 files changed

+718
-192
lines changed

.codecov.yml

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
2-
codecov:
3-
ci:
4-
- !appveyor
5-
- travis
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
target: 60%
6+
threshold: 2%

.github/actions/build/action.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ inputs:
66
required: true
77
cmake-args:
88
default: null
9+
cmake-target:
10+
default: all
911
# An implicit input is the environment variable `build_dir`.
1012
runs:
1113
using: composite
@@ -26,4 +28,5 @@ runs:
2628
cmake \
2729
--build ${build_dir} \
2830
--config ${{ inputs.configuration }} \
29-
--parallel ${NUM_PROCESSORS:-$(nproc)}
31+
--parallel ${NUM_PROCESSORS:-$(nproc)} \
32+
--target ${{ inputs.cmake-target }}

.github/workflows/nix.yml

+71-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
cc: /usr/bin/clang-14
5050
cxx: /usr/bin/clang++-14
5151
runs-on: [self-hosted, heavy]
52-
container: thejohnfreeman/rippled-build-ubuntu:12e19cd9034b
52+
container: rippleci/rippled-build-ubuntu:aaf5e3e
5353
env:
5454
build_dir: .build
5555
steps:
@@ -125,7 +125,7 @@ jobs:
125125
- "-Dunity=ON"
126126
needs: dependencies
127127
runs-on: [self-hosted, heavy]
128-
container: thejohnfreeman/rippled-build-ubuntu:12e19cd9034b
128+
container: rippleci/rippled-build-ubuntu:aaf5e3e
129129
env:
130130
build_dir: .build
131131
steps:
@@ -159,3 +159,72 @@ jobs:
159159
- name: test
160160
run: |
161161
${build_dir}/rippled --unittest --unittest-jobs $(nproc)
162+
163+
164+
coverage:
165+
strategy:
166+
fail-fast: false
167+
matrix:
168+
platform:
169+
- linux
170+
compiler:
171+
- gcc
172+
configuration:
173+
- Debug
174+
needs: dependencies
175+
runs-on: [self-hosted, heavy]
176+
container: rippleci/rippled-build-ubuntu:aaf5e3e
177+
env:
178+
build_dir: .build
179+
steps:
180+
- name: download cache
181+
uses: actions/download-artifact@v3
182+
with:
183+
name: ${{ matrix.platform }}-${{ matrix.compiler }}-${{ matrix.configuration }}
184+
- name: extract cache
185+
run: |
186+
mkdir -p ~/.conan
187+
tar -xzf conan.tar -C ~/.conan
188+
- name: check environment
189+
run: |
190+
echo ${PATH} | tr ':' '\n'
191+
conan --version
192+
cmake --version
193+
gcovr --version
194+
env | sort
195+
ls ~/.conan
196+
- name: checkout
197+
uses: actions/checkout@v3
198+
- name: dependencies
199+
uses: ./.github/actions/dependencies
200+
with:
201+
configuration: ${{ matrix.configuration }}
202+
- name: build
203+
uses: ./.github/actions/build
204+
with:
205+
generator: Ninja
206+
configuration: ${{ matrix.configuration }}
207+
cmake-args: >-
208+
-Dcoverage=ON
209+
-Dcoverage_format=xml
210+
-DCODE_COVERAGE_VERBOSE=ON
211+
-DCMAKE_CXX_FLAGS="-O0"
212+
-DCMAKE_C_FLAGS="-O0"
213+
cmake-target: coverage
214+
- name: build
215+
shell: bash
216+
run: |
217+
mv "${build_dir}/coverage.xml" ./
218+
- name: archive coverage report
219+
uses: actions/upload-artifact@v3
220+
with:
221+
name: coverage.xml
222+
path: coverage.xml
223+
retention-days: 30
224+
- name: upload coverage report
225+
uses: codecov/codecov-action@v3
226+
with:
227+
files: coverage.xml
228+
fail_ci_if_error: true
229+
verbose: true
230+
token: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/windows.yml

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ jobs:
9090
configuration: ${{ matrix.configuration }}
9191
# Hard code for now. Move to the matrix if varied options are needed
9292
cmake-args: '-Dassert=ON -Dreporting=OFF -Dunity=ON'
93+
cmake-target: install
9394
- name: test (permitted to silently fail)
9495
shell: bash
9596
# Github runners are resource limited, which causes unit tests to fail

BUILD.md

+62
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,72 @@ It patches their CMake to correctly import its dependencies.
262262
generator. Pass `--help` to see the rest of the command line options.
263263
264264
265+
## Coverage report
266+
267+
The coverage report is intended for developers using compilers GCC
268+
or Clang (including Apple Clang). It is generated by the build target `coverage`,
269+
which is only enabled when the `coverage` option is set, e.g. with
270+
`--options coverage=True` in `conan` or `-Dcoverage=ON` variable in `cmake`
271+
272+
Prerequisites for the coverage report:
273+
274+
- [gcovr tool][gcovr] (can be installed e.g. with [pip][python-pip])
275+
- `gcov` for GCC (installed with the compiler by default) or
276+
- `llvm-cov` for Clang (installed with the compiler by default)
277+
- `Debug` build type
278+
279+
A coverage report is created when the following steps are completed, in order:
280+
281+
1. `rippled` binary built with instrumentation data, enabled by the `coverage`
282+
option mentioned above
283+
2. completed run of unit tests, which populates coverage capture data
284+
3. completed run of the `gcovr` tool (which internally invokes either `gcov` or `llvm-cov`)
285+
to assemble both instrumentation data and the coverage capture data into a coverage report
286+
287+
The above steps are automated into a single target `coverage`. The instrumented
288+
`rippled` binary can also be used for regular development or testing work, at
289+
the cost of extra disk space utilization and a small performance hit
290+
(to store coverage capture). In case of a spurious failure of unit tests, it is
291+
possible to re-run the `coverage` target without rebuilding the `rippled` binary
292+
(since it is simply a dependency of the coverage report target). It is also possible
293+
to select only specific tests for the purpose of the coverage report, by setting
294+
the `coverage_test` variable in `cmake`
295+
296+
The default coverage report format is `html-details`, but the user
297+
can override it to any of the formats listed in `Builds/CMake/CodeCoverage.cmake`
298+
by setting the `coverage_format` variable in `cmake`. It is also possible
299+
to generate more than one format at a time by setting the `coverage_extra_args`
300+
variable in `cmake`. The specific command line used to run the `gcovr` tool will be
301+
displayed if the `CODE_COVERAGE_VERBOSE` variable is set.
302+
303+
By default, the code coverage tool runs parallel unit tests with `--unittest-jobs`
304+
set to the number of available CPU cores. This may cause spurious test
305+
errors on Apple. Developers can override the number of unit test jobs with
306+
the `coverage_test_parallelism` variable in `cmake`.
307+
308+
Example use with some cmake variables set:
309+
310+
```
311+
cd .build
312+
conan install .. --output-folder . --build missing --settings build_type=Debug
313+
cmake -DCMAKE_BUILD_TYPE=Debug -Dcoverage=ON -Dcoverage_test_parallelism=2 -Dcoverage_format=html-details -Dcoverage_extra_args="--json coverage.json" -DCMAKE_TOOLCHAIN_FILE:FILEPATH=build/generators/conan_toolchain.cmake ..
314+
cmake --build . --target coverage
315+
```
316+
317+
After the `coverage` target is completed, the generated coverage report will be
318+
stored inside the build directory, as either of:
319+
320+
- file named `coverage.`_extension_ , with a suitable extension for the report format, or
321+
- directory named `coverage`, with the `index.html` and other files inside, for the `html-details` or `html-nested` report formats.
322+
323+
265324
## Options
266325
267326
| Option | Default Value | Description |
268327
| --- | ---| ---|
269328
| `assert` | OFF | Enable assertions.
270329
| `reporting` | OFF | Build the reporting mode feature. |
330+
| `coverage` | OFF | Prepare the coverage report. |
271331
| `tests` | ON | Build tests. |
272332
| `unity` | ON | Configure a unity build. |
273333
| `san` | N/A | Enable a sanitizer with Clang. Choices are `thread` and `address`. |
@@ -362,6 +422,8 @@ If you want to experiment with a new package, follow these steps:
362422
[5]: https://en.wikipedia.org/wiki/Unity_build
363423
[6]: https://github.com/boostorg/beast/issues/2648
364424
[7]: https://github.com/boostorg/beast/issues/2661
425+
[gcovr]: https://gcovr.com/en/stable/getting-started.html
426+
[python-pip]: https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/
365427
[build_type]: https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html
366428
[runtime]: https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html
367429
[toolchain]: https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html

0 commit comments

Comments
 (0)