From ba9b858ab21ba0af4d280fffd221c74d77eaa11b Mon Sep 17 00:00:00 2001 From: Drew Oldag <47493171+drewoldag@users.noreply.github.com> Date: Mon, 22 Apr 2024 13:10:05 -0700 Subject: [PATCH] Code coverage reporting for C++ code (#94) * 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. --- .github/workflows/testing-and-coverage.yml | 18 ++++++++++++++++-- CMakeLists.txt | 8 ++++++++ setup.py | 2 +- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/.github/workflows/testing-and-coverage.yml b/.github/workflows/testing-and-coverage.yml index 826aed46..17df2812 100644 --- a/.github/workflows/testing-and-coverage.yml +++ b/.github/workflows/testing-and-coverage.yml @@ -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'] @@ -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 }} diff --git a/CMakeLists.txt b/CMakeLists.txt index 68dc0665..3ef880db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/setup.py b/setup.py index 4d7a0e99..d91b5baf 100644 --- a/setup.py +++ b/setup.py @@ -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()