Skip to content

Commit

Permalink
Merge pull request #301 from Yoast/JRF/start-recording-code-coverage
Browse files Browse the repository at this point in the history
CI: start recording code coverage
  • Loading branch information
jrfnl committed Apr 30, 2023
2 parents 60e19f6 + 262e7a2 commit 972e83e
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
116 changes: 115 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
push:
branches:
- main
- develop
paths-ignore:
- '**.md'
pull_request:
Expand All @@ -27,6 +28,7 @@ env:
jobs:
#### TEST STAGE ####
test:
if: ${{ github.ref != 'refs/heads/develop' }}
runs-on: ubuntu-latest

strategy:
Expand All @@ -37,7 +39,9 @@ jobs:
# You can define jobs here and then augment them with extra variables in `include`,
# as well as add extra jobs in `include`.
# @link https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix
php_version: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
#
# The matrix is set up so as not to duplicate the builds which are run for code coverage.
php_version: ['5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '8.0', '8.1', '8.2']
cs_dependencies: ['lowest', 'highest']
experimental: [false]

Expand Down Expand Up @@ -154,3 +158,113 @@ jobs:
run: composer test -- --no-configuration --dont-report-useless-tests
env:
PHPCS_IGNORE_TESTS: 'PHPCompatibility,WordPress'

#### CODE COVERAGE STAGE ####
# N.B.: Coverage is only checked on the lowest and highest stable PHP versions
# and a low/high of each major for PHPCS.
# These builds are left out off the "test" stage so as not to duplicate test runs.
coverage:
# No use running the coverage builds if there are failing test builds.
needs: test
# The default condition is success(), but this is false when one of the previous jobs is skipped
if: always() && (needs.test.result == 'success' || needs.test.result == 'skipped')

runs-on: ubuntu-latest

strategy:
matrix:
# 7.4 should be updated to 8.2 when higher PHPUnit versions can be supported.
php_version: ['5.4', '7.4']
cs_dependencies: ['lowest', 'highest']

name: "Coverage${{ matrix.cs_dependencies == 'highest' && ' + Lint' || '' }}: PHP ${{ matrix.php_version }} - CS Deps ${{ matrix.cs_dependencies }}"

steps:
- name: Checkout code
uses: actions/checkout@v3

# On stable PHPCS versions, allow for PHP deprecation notices.
# Unit tests don't need to fail on those for stable releases where those issues won't get fixed anymore.
- name: Setup ini config
id: set_ini
run: |
if [[ "${{ matrix.cs_dependencies }}" != "highest" && "${{ matrix.phpcs_version }}" != "4.0.x-dev" ]]; then
echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On' >> $GITHUB_OUTPUT
else
echo 'PHP_INI=error_reporting=-1, display_errors=On' >> $GITHUB_OUTPUT
fi
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
ini-values: ${{ steps.set_ini.outputs.PHP_INI }}
coverage: xdebug
tools: cs2pr

- name: 'Composer: adjust CS dependencies (high)'
if: ${{ matrix.cs_dependencies == 'highest' }}
run: |
composer require --no-update --no-scripts squizlabs/php_codesniffer:"${{ env.PHPCS_HIGHEST }}" --no-interaction
composer require --no-update --no-scripts wp-coding-standards/wpcs:"${{ env.WPCS_HIGHEST }}" --no-interaction
# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
- name: Install Composer dependencies
uses: "ramsey/composer-install@v2"
with:
# Bust the cache at least once a month - output format: YYYY-MM.
custom-cache-suffix: $(date -u "+%Y-%m")

- name: "Composer: set PHPCS version for tests (low)"
if: ${{ matrix.cs_dependencies == 'lowest' }}
run: composer update squizlabs/php_codesniffer wp-coding-standards/wpcs --with-dependencies --prefer-lowest --ignore-platform-req=php+ --no-scripts --no-interaction

- name: Verify installed versions
run: composer info --no-dev

- name: Verify installed standards
run: vendor/bin/phpcs -i

# The results of the linting will be shown inline in the PR via the CS2PR tool.
# @link https://github.com/staabm/annotate-pull-request-from-checkstyle/
- name: Lint against parse errors
if: ${{ matrix.cs_dependencies == 'highest' }}
run: composer lint -- --checkstyle | cs2pr

- name: Run the unit tests with code coverage
run: composer coverage

# Uploading the results with PHP Coveralls v1 won't work from GH Actions, so switch the PHP version.
- name: Switch to PHP 7.4
if: ${{ success() && matrix.php_version != '7.4' }}
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
coverage: none

# Global install is used to prevent a conflict with the local composer.lock in PHP 8.0+.
- name: Install Coveralls
if: ${{ success() }}
run: composer global require php-coveralls/php-coveralls:"^2.5.3" --no-interaction

- name: Upload coverage results to Coveralls
if: ${{ success() }}
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
COVERALLS_PARALLEL: true
COVERALLS_FLAG_NAME: php-${{ matrix.php_version }}-cs-${{ matrix.cs_dependencies }}
run: php-coveralls -v -x build/logs/clover.xml

coveralls-finish:
needs: coverage
if: always() && needs.coverage.result == 'success'

runs-on: ubuntu-latest

steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.COVERALLS_TOKEN }}
parallel-finished: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ composer.lock
.cache/phpcs.cache
phpcs.xml
phpunit.xml
build/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Yoast Coding Standards

[![Coverage Status](https://coveralls.io/repos/github/Yoast/yoastcs/badge.svg?branch=develop)](https://coveralls.io/github/Yoast/yoastcs?branch=develop)

Yoast Coding Standards (YoastCS) is a project with rulesets for code style and quality tools to be used in Yoast projects.

## Installation
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf"
],
"test": [
"@php ./vendor/phpunit/phpunit/phpunit --filter Yoast --bootstrap=\"./vendor/squizlabs/php_codesniffer/tests/bootstrap.php\" ./vendor/squizlabs/php_codesniffer/tests/AllTests.php --no-coverage"
],
"coverage": [
"@php ./vendor/phpunit/phpunit/phpunit --filter Yoast --bootstrap=\"./vendor/squizlabs/php_codesniffer/tests/bootstrap.php\" ./vendor/squizlabs/php_codesniffer/tests/AllTests.php"
],
"check-complete": [
Expand Down
6 changes: 6 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@
<filter>
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">Yoast/Sniffs/</directory>
<directory suffix=".php">Yoast/Utils/</directory>
</whitelist>
</filter>

<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>

<php>
<env name="PHPCS_IGNORE_TESTS" value="PHPCompatibility,WordPress"/>
</php>
Expand Down

0 comments on commit 972e83e

Please sign in to comment.