Skip to content

Commit

Permalink
Code coverage reporting for C++ code (#94)
Browse files Browse the repository at this point in the history
* Making modifications to CMakeLists and the github action in order to record and upload code coverage numbers.

* Slightly different incantation, --no-build-isolation assumes all build back end is pre-installed.

* Need to install `lcov` on the ubuntu image.

* Attempting to combine coverage reports into one file to address "invalid token" error.

* Making a few attempts, just committing this as a checkpoint.

* Taking a more straightforward approach, and just looking at env var.

* Attempting to fix the upload error by changing the final output file name.
  • Loading branch information
drewoldag authored Apr 22, 2024
1 parent 7248c89 commit ba9b858
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
18 changes: 16 additions & 2 deletions .github/workflows/testing-and-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
build:

runs-on: ubuntu-latest
env:
INCLUDE_COVERAGE: TRUE
strategy:
matrix:
python-version: ['3.9', '3.10', '3.11', '3.12']
Expand All @@ -21,20 +23,32 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -yy lcov
python -m pip install --upgrade pip
pip install -e .[dev]
python -m pip install .[dev]
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Run unit tests with pytest
run: |
python -m pytest -s tests --cov=lephare --cov-report=xml
python -m pytest -s tests --cov=lephare --cov-report=lcov
- name: Collect C++ coverage
run: |
lcov --output-file coverage.cpp --capture --directory build
lcov --output-file coverage.cpp --extract coverage.cpp $PWD/src/"*"
cat coverage.lcov coverage.cpp > coverage.txt
- name: Upload coverage report to codecov
uses: codecov/codecov-action@v4
with:
files: coverage.txt
token: ${{ secrets.CODECOV_TOKEN }}
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ else()
add_compile_options("-mno-vzeroupper" "-mavx2" "-floop-unroll-and-jam" )
endif()

# only set these flags if in `INCLUDE_COVERAGE` env var is set (to any value)
if(DEFINED ENV{INCLUDE_COVERAGE})
message(STATUS "Building with code coverage enabled.")
SET(GCC_COVERAGE_COMPILE_FLAGS "-g -O0 -coverage -fprofile-arcs -ftest-coverage")
SET(GCC_COVERAGE_LINK_FLAGS "-coverage -lgcov")
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )
SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}" )
endif()

add_library(lepharelib STATIC ${SOURCES})
find_package(OpenMP)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def build_extension(self, ext):
cmake_args += ["-A", "x64"]
build_args += ["--", "/m"]
else:
cmake_args += ["-DCMAKE_BUILD_TYPE=" + cfg]
cmake_args += ["-DCMAKE_BUILD_TYPE:STRING=" + cfg]
build_args += ["--", "-j2"]

env = os.environ.copy()
Expand Down

0 comments on commit ba9b858

Please sign in to comment.