From 76eada5aa8f35f60e62fbe3d1fb831b7ef3a9f79 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Wed, 18 Dec 2024 09:57:27 +1100 Subject: [PATCH] Fixed PCOV not reporting coverage. --- .ahoy.yml | 8 ++++---- .circleci/config.yml | 2 +- .github/workflows/test.yml | 3 +-- .scaffold/tests/bats/_assert_init.bash | 13 +++++++++++++ .scaffold/tests/bats/ahoy.bats | 2 ++ .scaffold/tests/bats/make.bats | 2 ++ Makefile | 8 ++++---- README.dist.md | 4 ++-- README.md | 2 +- 9 files changed, 30 insertions(+), 14 deletions(-) diff --git a/.ahoy.yml b/.ahoy.yml index 9a9774dc..5ba79757 100644 --- a/.ahoy.yml +++ b/.ahoy.yml @@ -61,28 +61,28 @@ commands: usage: Run tests. cmd: | pushd "build" >/dev/null || exit 1 - vendor/bin/phpunit + php -d pcov.directory=.. vendor/bin/phpunit popd >/dev/null || exit 1 test-unit: usage: Run unit tests. cmd: | pushd "build" >/dev/null || exit 1 - vendor/bin/phpunit --testsuite unit "$@" + php -d pcov.directory=.. vendor/bin/phpunit --testsuite unit "$@" popd >/dev/null || exit 1 test-kernel: usage: Run kernel tests. cmd: | pushd "build" >/dev/null || exit 1 - vendor/bin/phpunit --testsuite kernel "$@" + php -d pcov.directory=.. vendor/bin/phpunit --testsuite kernel "$@" popd >/dev/null || exit 1 test-functional: usage: Run functional tests. cmd: | pushd "build" >/dev/null || exit 1 - vendor/bin/phpunit --testsuite functional "$@" + php -d pcov.directory=.. vendor/bin/phpunit --testsuite functional "$@" popd >/dev/null || exit 1 reset: diff --git a/.circleci/config.yml b/.circleci/config.yml index d3490691..3f803186 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -84,7 +84,7 @@ job-test: &job-test - run: name: Run tests - command: vendor/bin/phpunit || [ "${CI_TEST_IGNORE_FAILURE:-0}" -eq 1 ] + command: php -d pcov.directory=.. vendor/bin/phpunit || [ "${CI_TEST_IGNORE_FAILURE:-0}" -eq 1 ] working_directory: build environment: BROWSERTEST_OUTPUT_DIRECTORY: /tmp diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 497254ba..138fa961 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -111,7 +111,6 @@ jobs: php-version: ${{ matrix.php-version }} extensions: gd, sqlite, pdo_sqlite coverage: pcov - ini-values: pcov.directory=. # Disable Symfony deprecations helper for PHP 8.4+ until minor # versions of Drupal 10 and 11 fully support PHP 8.4. @@ -156,7 +155,7 @@ jobs: - name: Run tests working-directory: build - run: vendor/bin/phpunit || [ "${CI_TEST_IGNORE_FAILURE:-0}" -eq 1 ] + run: php -d pcov.directory=.. vendor/bin/phpunit || [ "${CI_TEST_IGNORE_FAILURE:-0}" -eq 1 ] env: BROWSERTEST_OUTPUT_DIRECTORY: /tmp diff --git a/.scaffold/tests/bats/_assert_init.bash b/.scaffold/tests/bats/_assert_init.bash index c2cb11aa..efa3a6f8 100644 --- a/.scaffold/tests/bats/_assert_init.bash +++ b/.scaffold/tests/bats/_assert_init.bash @@ -185,3 +185,16 @@ assert_workflow_run() { popd >/dev/null || exit 1 } + +assert_test_coverage() { + local dir="${1:-$(pwd)}" + pushd "${dir}" >/dev/null || exit 1 + + assert_file_exists ".logs/coverage/phpunit/cobertura.xml" + assert_file_not_contains ".logs/coverage/phpunit/cobertura.xml" 'coverage line-rate="0"' + + assert_file_exists ".logs/coverage/phpunit/.coverage-html/index.html" + assert_file_contains ".logs/coverage/phpunit/.coverage-html/index.html" "33.33% covered" + + popd >/dev/null || exit 1 +} diff --git a/.scaffold/tests/bats/ahoy.bats b/.scaffold/tests/bats/ahoy.bats index 1c7015af..b47b1e19 100644 --- a/.scaffold/tests/bats/ahoy.bats +++ b/.scaffold/tests/bats/ahoy.bats @@ -121,6 +121,8 @@ export BATS_FIXTURE_EXPORT_CODEBASE_ENABLED=1 run ahoy test-unit assert_success + assert_test_coverage + sed -i -e "s/assertEquals/assertNotEquals/g" "${BUILD_DIR}/tests/src/Unit/YourExtensionServiceUnitTest.php" run ahoy test-unit assert_failure diff --git a/.scaffold/tests/bats/make.bats b/.scaffold/tests/bats/make.bats index d5c3cf5c..7beed357 100644 --- a/.scaffold/tests/bats/make.bats +++ b/.scaffold/tests/bats/make.bats @@ -127,6 +127,8 @@ export BATS_FIXTURE_EXPORT_CODEBASE_ENABLED=1 run make test-unit assert_success + assert_test_coverage + sed -i -e "s/assertEquals/assertNotEquals/g" "${BUILD_DIR}/tests/src/Unit/YourExtensionServiceUnitTest.php" run make test-unit assert_failure diff --git a/Makefile b/Makefile index c3e8463f..9b7486c8 100644 --- a/Makefile +++ b/Makefile @@ -66,22 +66,22 @@ lint-fix: test: pushd "build" >/dev/null || exit 1 && \ - BROWSERTEST_OUTPUT_DIRECTORY=/tmp vendor/bin/phpunit && \ + BROWSERTEST_OUTPUT_DIRECTORY=/tmp php -d pcov.directory=.. vendor/bin/phpunit && \ popd >/dev/null || exit 1 test-unit: pushd "build" >/dev/null || exit 1 && \ - vendor/bin/phpunit --testsuite unit && \ + php -d pcov.directory=.. vendor/bin/phpunit --testsuite unit && \ popd >/dev/null || exit 1 test-kernel: pushd "build" >/dev/null || exit 1 && \ - vendor/bin/phpunit --testsuite kernel && \ + php -d pcov.directory=.. vendor/bin/phpunit --testsuite kernel && \ popd >/dev/null || exit 1 test-functional: pushd "build" >/dev/null || exit 1 && \ - BROWSERTEST_OUTPUT_DIRECTORY=/tmp vendor/bin/phpunit --testsuite functional && \ + BROWSERTEST_OUTPUT_DIRECTORY=/tmp php -d pcov.directory=.. vendor/bin/phpunit --testsuite functional && \ popd >/dev/null || exit 1 reset: diff --git a/README.dist.md b/README.dist.md index 37614d0a..9362dbe9 100644 --- a/README.dist.md +++ b/README.dist.md @@ -170,8 +170,8 @@ You may also run tests using the `phpunit` command directly: ```bash cd build -./vendor/bin/phpunit tests/src/Unit/MyUnitTest.php -./vendor/bin/phpunit --group=wip +php -d pcov.directory=.. vendor/bin/phpunit tests/src/Unit/MyUnitTest.php +php -d pcov.directory=.. vendor/bin/phpunit --group=wip ``` --- diff --git a/README.md b/README.md index 75209e38..63027193 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ and push the code to [Drupal.org](https://drupal.org). - Drupal version matrix: `stable`, `canary` and `legacy`. - CI providers: [GitHub Actions](.github/workflows/test.yml) and [CircleCI](.circleci/config.yml) - - Code coverage with [codecov.io](https://codecov.io). + - Code coverage with https://github.com/krakjoe/pcov pushed to [codecov.io](https://codecov.io). - Develop locally using PHP running on your host using identical [`.devtools`](.devtools) scripts as in CI: - Uses [drupal-composer/drupal-project](https://github.com/drupal-composer/drupal-project)