Skip to content

Commit 73f4a2b

Browse files
committed
feat: Add code coverage measurement and documentation
This commit introduces code coverage measurement using `lcov` for the project. - **feat(ci):** Integrated `lcov` into the `test-linux` GitHub Actions workflow to automatically generate and upload coverage reports. - **feat(cmake):** Added an `ENABLE_COVERAGE` option to `CMakeLists.txt` to compile the project with coverage instrumentation flags. - **docs:** Added a new "Code Coverage" section to `README.md` with instructions on how to manually generate and view coverage reports using `lcov` and `genhtml`. Signed-off-by: Dariusz Frankiewicz <[email protected]>
1 parent ca288a8 commit 73f4a2b

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

.github/workflows/build.yml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,37 @@ jobs:
8888
8989
test-linux:
9090
runs-on: ubuntu-latest
91-
needs: build-linux-amd64
9291
steps:
9392
- name: Checkout code
9493
uses: actions/checkout@v6
9594

9695
- name: Set up CMake
9796
uses: jwlawson/actions-setup-cmake@v2
9897

99-
- name: Download build artifacts
100-
uses: actions/download-artifact@v6
101-
with:
102-
name: build-PC-artifacts
98+
- name: Build PC (Linux) for coverage
99+
run: |
100+
cmake -S ${{github.workspace}} -B ${{github.workspace}}/build -DENABLE_COVERAGE=ON
101+
cmake --build ${{github.workspace}}/build
103102
104103
- name: Run tests for PC
105104
run: |
106-
tar -xvf build-pc.tar
107105
cd build
108106
ctest
109107
108+
- name: Generate coverage report
109+
run: |
110+
sudo apt-get update
111+
sudo apt-get install -y lcov
112+
lcov --capture --directory ${{github.workspace}}/build --output-file ${{github.workspace}}/build/coverage.info
113+
lcov --remove ${{github.workspace}}/build/coverage.info '/usr/*' --output-file ${{github.workspace}}/build/coverage.info
114+
lcov --list ${{github.workspace}}/build/coverage.info
115+
116+
- name: Upload coverage report
117+
uses: actions/upload-artifact@v5
118+
with:
119+
name: coverage-report
120+
path: ${{github.workspace}}/build/coverage.info
121+
110122
- name: Upload test results
111123
uses: actions/upload-artifact@v5
112124
with:

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ option(OAPV_BUILD_APPS "Build the included command line apps" ON)
6868
option(OAPV_BUILD_STATIC_LIB "Build oapv static library" ON)
6969
option(OAPV_BUILD_SHARED_LIB "Build oapv shared library" ON)
7070

71+
option(ENABLE_COVERAGE "Enable code coverage measurement" OFF)
72+
7173
if(OAPV_BUILD_APPS AND OAPV_APP_STATIC_BUILD AND NOT OAPV_BUILD_STATIC_LIB)
7274
message(FATAL_ERROR "Cannot build static apps without static lib.")
7375
endif()
@@ -104,6 +106,9 @@ endif()
104106
if(MSVC)
105107
message("Not supported yet!")
106108
elseif(UNIX OR MINGW)
109+
if(ENABLE_COVERAGE)
110+
set(CMAKE_BUILD_TYPE "Debug")
111+
endif()
107112
if(NOT CMAKE_BUILD_TYPE)
108113
set(CMAKE_BUILD_TYPE "Release")
109114
endif()
@@ -120,6 +125,11 @@ elseif(UNIX OR MINGW)
120125
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPT_DBG} -${OPT_LV} -fomit-frame-pointer -pthread -std=c99")
121126
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-unused-function -Wno-pointer-sign -Wno-pointer-to-int-cast")
122127
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lm")
128+
129+
if(ENABLE_COVERAGE)
130+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage")
131+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
132+
endif()
123133
else()
124134
message("Unknown compiler")
125135
endif()

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,40 @@ Pattern file of APV bitstream for [ImHex](https://github.com/WerWolv/ImHex) is p
132132

133133
In build directory run ``ctest``
134134

135+
## Code Coverage
136+
137+
To generate a code coverage report manually:
138+
139+
1. **`lcov` is required.**
140+
141+
2. **Build and Test with Coverage:**
142+
```bash
143+
# Create build directory and configure with coverage enabled
144+
cmake -S . -B build -DENABLE_COVERAGE=ON
145+
146+
# Build the project
147+
cmake --build build
148+
149+
# Run tests to generate coverage data
150+
cd build && ctest && cd ..
151+
```
152+
153+
3. **Generate HTML Report:**
154+
```bash
155+
# Capture coverage data
156+
lcov --capture --directory build --output-file build/coverage.info
157+
158+
# Remove coverage for external files (optional)
159+
lcov --remove build/coverage.info '/usr/*' --output-file build/coverage.info
160+
161+
# Generate the HTML report
162+
mkdir -p coverage_report
163+
genhtml build/coverage.info --output-directory coverage_report
164+
```
165+
166+
4. **View Report:**
167+
Open `coverage_report/index.html` in your web browser.
168+
135169
## Packaging
136170

137171
For generating package ready for distribution (default deb) execute in build directory ``cpack``, or other formats (tgz, zip etc.) ``cpack -G TGZ``.

0 commit comments

Comments
 (0)