diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index e6d9d80..02c8bab 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -36,7 +36,7 @@ jobs: - name: Build run: | phpize - ./configure + ./configure --enable-code-coverage make -j $(nproc) sudo make install sudo sh -c "echo 'extension=phpy.so' > /etc/php/${PHP_VERSION}/cli/conf.d/90-phpy.ini" @@ -49,3 +49,18 @@ jobs: sudo service redis-server start composer install composer test + + - name: run coverage + shell: bash + run: sudo apt-get install lcov && + sudo lcov --directory . --capture --branch-coverage --rc geninfo_unexecuted_blocks=1 --ignore-errors mismatch --output-file coverage.info && + sudo lcov --remove coverage.info '/usr/*' --output-file coverage.info && + sudo lcov --list coverage.info + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v5 + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: ./coverage.info + fail_ci_if_error: true + diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 7c4dabe..ffa3774 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -37,10 +37,24 @@ jobs: pwd - name: Build run: | - cmake . + cmake . -D CODE_COVERAGE=ON make -j $(nproc) - name: Run pytest tests run: | sudo service redis-server start pip install pytest pytest -v tests/ + + - name: run coverage + shell: bash + run: sudo apt-get install lcov && + sudo lcov --directory . --capture --branch-coverage --rc geninfo_unexecuted_blocks=1 --ignore-errors mismatch --output-file coverage.info && + sudo lcov --remove coverage.info '/usr/*' --output-file coverage.info && + sudo lcov --list coverage.info + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v5 + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: ./coverage.info + fail_ci_if_error: true \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d851fb..fb74622 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,26 @@ if (NOT DEFINED PYTHON_CONFIG) set(PYTHON_CONFIG "python3-config") endif() +# Code Coverage Configuration +add_library(coverage_config INTERFACE) + +option(CODE_COVERAGE "Enable coverage reporting" OFF) +if(CODE_COVERAGE) + message(STATUS "Open coverage") + # --coverage => -fprofile-arcs -ftest-coverage + target_compile_options(coverage_config INTERFACE + -O0 + -g + -fprofile-update=atomic + --coverage + ) + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.13) + target_link_options(coverage_config INTERFACE --coverage) + else() + target_link_libraries(coverage_config INTERFACE --coverage) + endif() +endif(CODE_COVERAGE) + file(GLOB_RECURSE SOURCE_FILES FOLLOW_SYMLINKS src/*.cc) execute_process(COMMAND ${PHP_CONFIG} --includes @@ -55,5 +75,9 @@ include_directories(BEFORE ${phpy_includes}) link_directories(${phpy_link_directories}) add_library(phpy SHARED ${SOURCE_FILES}) +if (CODE_COVERAGE) + target_link_libraries(phpy coverage_config gcov) +endif(CODE_COVERAGE) + set_target_properties(phpy PROPERTIES OUTPUT_NAME "lib/phpy") target_link_libraries(phpy ${phpy_libraries}) diff --git a/config.m4 b/config.m4 index a121bac..844912a 100644 --- a/config.m4 +++ b/config.m4 @@ -22,6 +22,11 @@ PHP_ARG_ENABLE([phpy], [Enable phpy support])], [no]) +PHP_ARG_ENABLE([code-coverage], + [whether to enable code coverage support], + [AS_HELP_STRING([--enable-code-coverage], + [Enable code coverage support])], [no], [no]) + AC_DEFUN([GET_PYTHON_LDFLAGS], [ TMP_RESULT="$($PHP_PYTHON_CONFIG --embed --ldflags)" if test $? -eq 0; then @@ -84,6 +89,16 @@ if test "$PHP_PHPY" != "no"; then EXTRAS_CXXFLAGS="-Wall -Wno-unused-function -Wno-deprecated -Wno-deprecated-declarations -z now" EXTRAS_CXXFLAGS="$EXTRAS_CXXFLAGS -std=c++14" + AC_MSG_CHECKING([code coverage]) + if test "$PHP_CODE_COVERAGE" != "no"; then + AC_MSG_RESULT([enabled]) + EXTRAS_CFLAGS="-fprofile-arcs -ftest-cover -fprofile-update=atomic" + EXTRAS_CXXFLAGS="-fprofile-arcs -ftest-coverage -fprofile-update=atomic" + EXTRA_LDFLAGS="--coverage" + else + AC_MSG_RESULT([disabled]) + fi + if test -f "$abs_srcdir/phpy.cc"; then phpy_source_dir=$abs_srcdir elif test -f "phpy.cc"; then