From de38e8eeecf80a7ca5ee870ec68a59897216b33b Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Thu, 24 Oct 2024 15:07:06 +0000 Subject: [PATCH] Build/Test Tools: Use the reusable PHPUnit test workflow for coverage. This updates the Test Coverage workflow to make use of the reusable PHPUnit test workflow to avoid having duplicate code. `concurrency` has also been configured for the workflow, and 4 test jobs have been consolidated into 2. Props jrf. Fixes #62296. git-svn-id: https://develop.svn.wordpress.org/trunk@59287 602fd350-edb4-49c9-b593-d223f7449a82 --- .../workflows/reusable-phpunit-tests-v3.yml | 46 ++++- .github/workflows/test-coverage.yml | 163 ++---------------- 2 files changed, 56 insertions(+), 153 deletions(-) diff --git a/.github/workflows/reusable-phpunit-tests-v3.yml b/.github/workflows/reusable-phpunit-tests-v3.yml index 5acdbae89c..6e0e0e80a2 100644 --- a/.github/workflows/reusable-phpunit-tests-v3.yml +++ b/.github/workflows/reusable-phpunit-tests-v3.yml @@ -52,6 +52,11 @@ on: required: false type: 'string' default: 'example.org' + coverage-report: + description: 'Whether to generate a code coverage report.' + required: false + type: boolean + default: false report: description: 'Whether to report results to WordPress.org Hosting Tests' required: false @@ -62,8 +67,14 @@ on: required: false type: boolean default: false + secrets: + CODECOV_TOKEN: + description: 'The Codecov token required for uploading reports.' + required: false env: LOCAL_PHP: ${{ inputs.php }}-fpm + LOCAL_PHP_XDEBUG: ${{ inputs.coverage-report || false }} + LOCAL_PHP_XDEBUG_MODE: ${{ inputs.coverage-report && 'coverage' || 'develop,debug' }} LOCAL_DB_TYPE: ${{ inputs.db-type }} LOCAL_DB_VERSION: ${{ inputs.db-version }} LOCAL_PHP_MEMCACHED: ${{ inputs.memcached }} @@ -88,13 +99,15 @@ jobs: # - Logs debug information about what's installed within the WordPress Docker containers. # - Install WordPress within the Docker container. # - Run the PHPUnit tests. + # - Upload the code coverage report to Codecov.io. + # - Upload the HTML code coverage report as an artifact. # - Ensures version-controlled files are not modified or deleted. # - Checks out the WordPress Test reporter repository. # - Submit the test results to the WordPress.org host test results. phpunit-tests: - name: PHP ${{ inputs.php }} / ${{ 'mariadb' == inputs.db-type && 'MariaDB' || 'MySQL' }} ${{ inputs.db-version }}${{ inputs.multisite && ' multisite' || '' }}${{ inputs.phpunit-test-groups && format( ' ({0})', inputs.phpunit-test-groups ) || '' }}${{ inputs.memcached && ' with memcached' || '' }}${{ inputs.report && ' (test reporting enabled)' || '' }} ${{ 'example.org' != inputs.tests-domain && inputs.tests-domain || '' }} + name: PHP ${{ inputs.php }} ${{ ! inputs.coverage-report && '/ ' || 'with ' }}${{ 'mariadb' == inputs.db-type && 'MariaDB' || 'MySQL' }} ${{ inputs.db-version }}${{ inputs.multisite && ' multisite' || '' }}${{ inputs.phpunit-test-groups && format( ' ({0})', inputs.phpunit-test-groups ) || '' }}${{ inputs.memcached && ' with memcached' || '' }}${{ inputs.report && ' (test reporting enabled)' || '' }} ${{ 'example.org' != inputs.tests-domain && inputs.tests-domain || '' }} runs-on: ${{ inputs.os }} - timeout-minutes: 20 + timeout-minutes: ${{ inputs.coverage-report && 120 || 20 }} steps: - name: Configure environment variables @@ -167,31 +180,48 @@ jobs: - name: Install WordPress run: npm run env:install - - name: Run PHPUnit tests${{ inputs.phpunit-test-groups && format( ' ({0} groups)', inputs.phpunit-test-groups ) || '' }} + - name: Run PHPUnit tests${{ inputs.phpunit-test-groups && format( ' ({0} groups)', inputs.phpunit-test-groups ) || '' }}${{ inputs.coverage-report && ' with coverage report' || '' }} continue-on-error: ${{ inputs.allow-errors }} - run: node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit --verbose -c ${{ env.PHPUNIT_CONFIG }}${{ inputs.phpunit-test-groups && format( ' --group {0}', inputs.phpunit-test-groups ) || '' }} + run: node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit --verbose -c ${{ env.PHPUNIT_CONFIG }}${{ inputs.phpunit-test-groups && format( ' --group {0}', inputs.phpunit-test-groups ) || '' }}${{ inputs.coverage-report && format( ' --coverage-clover wp-code-coverage-{0}-{1}.xml --coverage-html wp-code-coverage-{0}-{1}', ( inputs.multisite && 'multisite' || 'single' ), github.sha ) || '' }} - name: Run AJAX tests - if: ${{ ! inputs.phpunit-test-groups }} + if: ${{ ! inputs.phpunit-test-groups && ! inputs.coverage-report }} continue-on-error: ${{ inputs.allow-errors }} run: node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit --verbose -c ${{ env.PHPUNIT_CONFIG }} --group ajax - name: Run ms-files tests as a multisite install - if: ${{ inputs.multisite && ! inputs.phpunit-test-groups }} + if: ${{ inputs.multisite && ! inputs.phpunit-test-groups && ! inputs.coverage-report }} continue-on-error: ${{ inputs.allow-errors }} run: node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit --verbose -c ${{ env.PHPUNIT_CONFIG }} --group ms-files - name: Run external HTTP tests - if: ${{ ! inputs.multisite && ! inputs.phpunit-test-groups }} + if: ${{ ! inputs.multisite && ! inputs.phpunit-test-groups && ! inputs.coverage-report }} continue-on-error: ${{ inputs.allow-errors }} run: node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit --verbose -c ${{ env.PHPUNIT_CONFIG }} --group external-http # __fakegroup__ is excluded to force PHPUnit to ignore the settings in phpunit.xml.dist. - name: Run (Xdebug) tests - if: ${{ inputs.php != '8.4' && ! inputs.phpunit-test-groups }} + if: ${{ inputs.php != '8.4' && ! inputs.phpunit-test-groups && ! inputs.coverage-report }} continue-on-error: ${{ inputs.allow-errors }} run: LOCAL_PHP_XDEBUG=true node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit -v --group xdebug --exclude-group __fakegroup__ + - name: Upload test coverage report to Codecov + if: ${{ inputs.coverage-report }} + uses: codecov/codecov-action@b9fd7d16f6d7d1b5d2bec1a2887e65ceed900238 # v4.6.0 + with: + token: ${{ secrets.CODECOV_TOKEN }} + file: wp-code-coverage${{ inputs.multisite && '-multisite' || '-single' }}-${{ github.sha }}.xml + flags: ${{ inputs.multisite && 'multisite' || 'single' }},php + fail_ci_if_error: true + + - name: Upload HTML coverage report as artifact + if: ${{ inputs.coverage-report }} + uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3 + with: + name: wp-code-coverage${{ inputs.multisite && '-multisite' || '-single' }}-${{ github.sha }} + path: wp-code-coverage${{ inputs.multisite && '-multisite' || '-single' }}-${{ github.sha }} + overwrite: true + - name: Ensure version-controlled files are not modified or deleted run: git diff --exit-code diff --git a/.github/workflows/test-coverage.yml b/.github/workflows/test-coverage.yml index f6c68ff0af..0b935d4f5c 100644 --- a/.github/workflows/test-coverage.yml +++ b/.github/workflows/test-coverage.yml @@ -24,6 +24,13 @@ on: # Allow manually triggering the workflow. workflow_dispatch: +# Cancels all previous workflow runs for pull requests that have not completed. +concurrency: + # The concurrency group contains the workflow name and the branch name for pull requests + # or the commit hash for any other events. + group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }} + cancel-in-progress: true + # Disable permissions for all available scopes by default. # Any needed permissions should be configured at the job level. permissions: {} @@ -35,160 +42,26 @@ env: PUPPETEER_SKIP_DOWNLOAD: ${{ true }} jobs: - # Runs the PHPUnit tests for WordPress. # - # Performs the following steps: - # - Sets environment variables. - # - Checks out the repository. - # - Sets up Node.js. - # - Sets up PHP. - # - Installs Composer dependencies. - # - Installs npm dependencies - # - Logs general debug information about the runner. - # - Logs Docker debug information (about the Docker installation within the runner). - # - Starts the WordPress Docker container. - # - Logs the running Docker containers. - # - Logs debug information about what's installed within the WordPress Docker containers. - # - Install WordPress within the Docker container. - # - Run the PHPUnit tests as a single site. - # - Ensures version-controlled files are not modified or deleted. - # - Upload the single site code coverage report to Codecov.io. - # - Run the PHPUnit tests as a multisite installation. - # - Ensures version-controlled files are not modified or deleted. - # - Upload the multisite code coverage report to Codecov.io. + # Creates a PHPUnit test jobs for generating code coverage reports. + # test-coverage-report: - name: ${{ matrix.multisite && 'Multisite' || 'Single site' }} report (${{ matrix.format }}) - runs-on: ubuntu-latest + name: ${{ matrix.multisite && 'Multisite' || 'Single site' }} report + uses: WordPress/wordpress-develop/.github/workflows/reusable-phpunit-tests-v3.yml@trunk permissions: contents: read - timeout-minutes: 120 if: ${{ github.repository == 'WordPress/wordpress-develop' }} strategy: fail-fast: false matrix: multisite: [ false, true ] - format: [ clover, html ] - - steps: - - name: Configure environment variables - run: | - echo "PHP_FPM_UID=$(id -u)" >> $GITHUB_ENV - echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV - - - name: Checkout repository - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 - with: - show-progress: ${{ runner.debug == '1' && 'true' || 'false' }} - - - name: Set up Node.js - uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4 - with: - node-version-file: '.nvmrc' - cache: npm - - ## - # This allows Composer dependencies to be installed using a single step. - # - # Since the tests are currently run within the Docker containers where the PHP version varies, - # the same PHP version needs to be configured for the action runner machine so that the correct - # dependency versions are installed and cached. - ## - - name: Set up PHP - uses: shivammathur/setup-php@c541c155eee45413f5b09a52248675b1a2575231 # v2.31.1 - with: - php-version: '7.4' - coverage: none - - # Since Composer dependencies are installed using `composer update` and no lock file is in version control, - # passing a custom cache suffix ensures that the cache is flushed at least once per week. - - name: Install Composer dependencies - uses: ramsey/composer-install@57532f8be5bda426838819c5ee9afb8af389d51a # v3.0.0 - with: - custom-cache-suffix: $(/bin/date -u --date='last Mon' "+%F") - - - name: Install npm Dependencies - run: npm ci - - - name: Log debug information - run: | - echo "$GITHUB_REF" - echo "$GITHUB_EVENT_NAME" - npm --version - node --version - curl --version - git --version - composer --version - locale -a - - - name: Docker debug information - run: | - docker -v - - - name: Start Docker environment - run: | - npm run env:start - - - name: Log running Docker containers - run: docker ps -a - - - name: WordPress Docker container debug information - run: | - docker compose run --rm mysql mysql --version - docker compose run --rm php php --version - docker compose run --rm php php -m - docker compose run --rm php php -i - docker compose run --rm php locale -a - - - name: Install WordPress - run: npm run env:install - - - name: Run tests as a single site - if: ${{ ! matrix.multisite }} - run: npm run test:php -- --verbose -c phpunit.xml.dist --coverage-${{ 'html' == matrix.format && 'html' || 'clover' }} wp-code-coverage-single-${{ github.sha }}${{ 'clover' == matrix.format && '.xml' || '' }} - - - name: Ensure version-controlled files are not modified during the tests - run: git diff --exit-code - - - name: Upload single site report to Codecov - if: ${{ ! matrix.multisite && matrix.format == 'clover' && github.event_name != 'pull_request' }} - uses: codecov/codecov-action@b9fd7d16f6d7d1b5d2bec1a2887e65ceed900238 # v4.6.0 - with: - token: ${{ secrets.CODECOV_TOKEN }} - file: wp-code-coverage-single-${{ github.sha }}${{ 'clover' == matrix.format && '.xml' || '' }} - flags: single,php - fail_ci_if_error: true - - - name: Upload single site HTML report as artifact - if: ${{ ! matrix.multisite && matrix.format == 'html' }} - uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3 - with: - name: wp-code-coverage-single-${{ github.sha }} - path: wp-code-coverage-single-${{ github.sha }} - overwrite: true - - - name: Run tests as a multisite install - if: ${{ matrix.multisite }} - run: npm run test:php -- --verbose -c tests/phpunit/multisite.xml --coverage-${{ 'html' == matrix.format && 'html' || 'clover' }} wp-code-coverage-multisite-${{ github.sha }}${{ 'clover' == matrix.format && '.xml' || '' }} - - - name: Ensure version-controlled files are not modified during the tests - run: git diff --exit-code - - - name: Upload multisite report to Codecov - if: ${{ matrix.multisite && matrix.format == 'clover' && github.event_name != 'pull_request' }} - uses: codecov/codecov-action@b9fd7d16f6d7d1b5d2bec1a2887e65ceed900238 # v4.6.0 - with: - token: ${{ secrets.CODECOV_TOKEN }} - file: wp-code-coverage-multisite-${{ github.sha }}${{ 'clover' == matrix.format && '.xml' || '' }} - flags: multisite,php - fail_ci_if_error: true - - - name: Upload multisite HTML report as artifact - if: ${{ matrix.multisite && matrix.format == 'html' }} - uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3 - with: - name: wp-code-coverage-multisite-${{ github.sha }} - path: wp-code-coverage-multisite-${{ github.sha }} - overwrite: true + coverage-report: [ true ] + with: + php: '8.3' + multisite: ${{ matrix.multisite }} + coverage-report: ${{ matrix.coverage-report }} + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} slack-notifications: name: Slack Notifications