Skip to content

Commit

Permalink
Fixed PCOV not reporting coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Dec 17, 2024
1 parent 3cf5ceb commit 51ed73d
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 14 deletions.
8 changes: 4 additions & 4 deletions .ahoy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions .scaffold/tests/bats/_assert_functional.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
#
# Functional assertions.
#

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
}
13 changes: 13 additions & 0 deletions .scaffold/tests/bats/_assert_init.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bats

load _helper
load _assert_functional

export BATS_FIXTURE_EXPORT_CODEBASE_ENABLED=1

Expand Down Expand Up @@ -121,6 +122,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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bats

load _helper
load _assert_functional

export BATS_FIXTURE_EXPORT_CODEBASE_ENABLED=1

Expand Down Expand Up @@ -127,6 +128,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
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions README.dist.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

---
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 51ed73d

Please sign in to comment.