From 3e6cd53b4a5e1683bb160facd625e420b1a402cd Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 4 Aug 2021 19:48:56 +0000 Subject: [PATCH] Build/Test Tools: Switch to always running the tests via Composer. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously the tests were run via a PHPUnit Phar file for PHP 5.6–7.4, with PHP 8.0 using a Composer-installed version of PHPUnit. Running the tests via a Phar without the need for a `composer install` is (marginally) faster in overall build time, however, this commit is part of a larger chain of changes which will make the test suite PHPUnit cross-version compatible. With an eye on those upcoming changes, which will allow us to run the tests on the most appropriate PHPUnit version for each supported PHP version, it is opportune to switch to using a Composer-installed version of PHPUnit for all PHP versions supported by WordPress. Previously this was not possible without additional conditional `update` commands being run, due to the `composer.lock` file being in place and being locked at PHPUnit 7.5.20. Switching over to using the Composer-installed PHPUnit version, with that PHPUnit version adjusting based on the PHP version, allows for some minor simplifications in the GitHub Actions script. This means we need additional measures to make sure that the Composer cache file does not go too far out of date as that would significantly slow down the builds. By adding a "Last Monday" date to the cache key, in combination with the pre-existing OS, PHP version and the hash of the `composer.json` file, we can guarantee that: 1. There will be a cache created for each OS/PHP combination. 2. These caches will be replaced whenever a change is made to the `composer.json` file. 3. These caches will be replaced every Monday of each week ensuring that the cache file does not go too far out of date. Note: The NPM script `test:php` is now no longer needed during the builds. However, to prevent breaking the workflow of contributors who may be used to having the command available, the command remains available. In a future iteration we may be able to replace the caching of the Composer dependencies with the Composer cache action as offered on the GitHub marketplace, which would further simplify the script. Follow-up to [42960], [46290], [47881], [48957], [49037], [51543], [51544]. Props jrf, desrosj. Fixes #47381. git-svn-id: https://develop.svn.wordpress.org/trunk@51545 602fd350-edb4-49c9-b593-d223f7449a82 --- .github/workflows/phpunit-tests.yml | 42 ++++++++++++++--------------- .github/workflows/test-coverage.yml | 33 +++++++++++++++++++---- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/.github/workflows/phpunit-tests.yml b/.github/workflows/phpunit-tests.yml index aba74879f0..b368524ca5 100644 --- a/.github/workflows/phpunit-tests.yml +++ b/.github/workflows/phpunit-tests.yml @@ -30,9 +30,6 @@ concurrency: env: PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: ${{ true }} - COMPOSER_INSTALL: ${{ false }} - # Controls which NPM script to use for running PHPUnit tests. Options ar `php` and `php-composer`. - PHPUNIT_SCRIPT: php LOCAL_PHP_MEMCACHED: ${{ false }} SLOW_TESTS: 'external-http,media,restapi' @@ -121,33 +118,34 @@ jobs: - name: Install Dependencies run: npm ci - - name: Get composer cache directory + # This date is used to ensure that the Composer cache is refreshed at least once every week. + # http://man7.org/linux/man-pages/man1/date.1.html + - name: "Get last Monday's date" + id: get-date + run: echo "::set-output name=date::$(/bin/date -u --date='last Mon' "+%F")" + shell: bash + + - name: Get Composer cache directory id: composer-cache - if: ${{ env.COMPOSER_INSTALL == true || env.LOCAL_PHP == '8.0-fpm' }} run: echo "::set-output name=dir::$(composer config cache-files-dir)" - name: Cache Composer dependencies - if: ${{ env.COMPOSER_INSTALL == true || env.LOCAL_PHP == '8.0-fpm' }} uses: actions/cache@26968a09c0ea4f3e233fdddbafd1166051a095f6 # v2.1.4 env: cache-name: cache-composer-dependencies with: path: ${{ steps.composer-cache.outputs.dir }} - key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }} + key: ${{ runner.os }}-php-${{ matrix.php }}-date-${{ steps.get-date.outputs.date }}-composer-${{ hashFiles('**/composer.json') }} - name: Install Composer dependencies - if: ${{ env.COMPOSER_INSTALL == true || env.LOCAL_PHP == '8.0-fpm' }} run: | docker-compose run --rm php composer --version - # The PHPUnit 7.x phar is not compatible with PHP 8 and won't be updated, - # as PHPUnit 7 is no longer supported. The Composer-installed PHPUnit should be - # used for PHP 8 testing instead. + # Install using `composer update` as there is no `composer.lock` file. if [ ${{ env.LOCAL_PHP }} == '8.0-fpm' ]; then - docker-compose run --rm php composer install --ignore-platform-reqs - echo "PHPUNIT_SCRIPT=php-composer" >> $GITHUB_ENV + docker-compose run --rm php composer update --ignore-platform-reqs else - docker-compose run --rm php composer install + docker-compose run --rm php composer update fi - name: Docker debug information @@ -190,36 +188,36 @@ jobs: - name: Run slow PHPUnit tests if: ${{ matrix.split_slow }} - run: npm run test:${{ env.PHPUNIT_SCRIPT }} -- --verbose -c ${{ env.PHPUNIT_CONFIG }} --group ${{ env.SLOW_TESTS }} + run: npm run test:php-composer -- --verbose -c ${{ env.PHPUNIT_CONFIG }} --group ${{ env.SLOW_TESTS }} - name: Run PHPUnit tests for single site excluding slow tests if: ${{ matrix.php < '7.0' && ! matrix.split_slow && ! matrix.multisite }} - run: npm run test:${{ env.PHPUNIT_SCRIPT }} -- --verbose -c ${{ env.PHPUNIT_CONFIG }} --exclude-group ${{ env.SLOW_TESTS }},ajax,ms-files,ms-required + run: npm run test:php-composer -- --verbose -c ${{ env.PHPUNIT_CONFIG }} --exclude-group ${{ env.SLOW_TESTS }},ajax,ms-files,ms-required - name: Run PHPUnit tests for Multisite excluding slow tests if: ${{ matrix.php < '7.0' && ! matrix.split_slow && matrix.multisite }} - run: npm run test:${{ env.PHPUNIT_SCRIPT }} -- --verbose -c ${{ env.PHPUNIT_CONFIG }} --exclude-group ${{ env.SLOW_TESTS }},ajax,ms-files,ms-excluded,oembed-headers + run: npm run test:php-composer -- --verbose -c ${{ env.PHPUNIT_CONFIG }} --exclude-group ${{ env.SLOW_TESTS }},ajax,ms-files,ms-excluded,oembed-headers - name: Run PHPUnit tests if: ${{ matrix.php >= '7.0' }} - run: npm run test:${{ env.PHPUNIT_SCRIPT }} -- --verbose -c ${{ env.PHPUNIT_CONFIG }} + run: npm run test:php-composer -- --verbose -c ${{ env.PHPUNIT_CONFIG }} - name: Run AJAX tests if: ${{ ! matrix.split_slow }} - run: npm run test:${{ env.PHPUNIT_SCRIPT }} -- --verbose -c ${{ env.PHPUNIT_CONFIG }} --group ajax + run: npm run test:php-composer -- --verbose -c ${{ env.PHPUNIT_CONFIG }} --group ajax - name: Run ms-files tests as a multisite install if: ${{ matrix.multisite && ! matrix.split_slow }} - run: npm run test:${{ env.PHPUNIT_SCRIPT }} -- --verbose -c tests/phpunit/multisite.xml --group ms-files + run: npm run test:php-composer -- --verbose -c tests/phpunit/multisite.xml --group ms-files - name: Run external HTTP tests if: ${{ ! matrix.multisite && ! matrix.split_slow }} - run: npm run test:${{ env.PHPUNIT_SCRIPT }} -- --verbose -c phpunit.xml.dist --group external-http + run: npm run test:php-composer -- --verbose -c phpunit.xml.dist --group external-http # __fakegroup__ is excluded to force PHPUnit to ignore the settings in phpunit.xml.dist. - name: Run (xDebug) tests if: ${{ ! matrix.split_slow }} - run: LOCAL_PHP_XDEBUG=true npm run test:${{ env.PHPUNIT_SCRIPT }} -- -v --group xdebug --exclude-group __fakegroup__ + run: LOCAL_PHP_XDEBUG=true npm run test:php-composer -- -v --group xdebug --exclude-group __fakegroup__ - 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 e82fa1e1e4..3c804c5ca6 100644 --- a/.github/workflows/test-coverage.yml +++ b/.github/workflows/test-coverage.yml @@ -18,9 +18,6 @@ on: env: PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: ${{ true }} - COMPOSER_INSTALL: ${{ false }} - # Controls which NPM script to use for running PHPUnit tests. Options ar `php` and `php-composer`. - PHPUNIT_SCRIPT: php LOCAL_PHP: '7.4-fpm' LOCAL_PHP_XDEBUG: true LOCAL_PHP_MEMCACHED: ${{ false }} @@ -88,6 +85,32 @@ jobs: - name: Install Dependencies run: npm ci + # This date is used to ensure that the Composer cache is refreshed at least once every week. + # http://man7.org/linux/man-pages/man1/date.1.html + - name: "Get last Monday's date" + id: get-date + run: echo "::set-output name=date::$(/bin/date -u --date='last Mon' "+%F")" + shell: bash + + - name: Get Composer cache directory + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache Composer dependencies + uses: actions/cache@26968a09c0ea4f3e233fdddbafd1166051a095f6 # v2.1.4 + env: + cache-name: cache-composer-dependencies + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-php-${{ matrix.php }}-date-${{ steps.get-date.outputs.date }}-composer-${{ hashFiles('**/composer.json') }} + + - name: Install Composer dependencies + run: | + docker-compose run --rm php composer --version + + # Install using `composer update` as there is no `composer.lock` file. + docker-compose run --rm php composer update + - name: Docker debug information run: | docker -v @@ -121,7 +144,7 @@ jobs: - name: Run tests as a single site if: ${{ ! matrix.multisite }} - run: npm run test:${{ env.PHPUNIT_SCRIPT }} -- --verbose -c phpunit.xml.dist --coverage-clover wp-code-coverage-single-clover-${{ github.sha }}.xml + run: npm run test:php-composer -- --verbose -c phpunit.xml.dist --coverage-clover wp-code-coverage-single-clover-${{ github.sha }}.xml - name: Ensure version-controlled files are not modified during the tests run: git diff --exit-code @@ -135,7 +158,7 @@ jobs: - name: Run tests as a multisite install if: ${{ matrix.multisite }} - run: npm run test:${{ env.PHPUNIT_SCRIPT }} -- --verbose -c tests/phpunit/multisite.xml --coverage-clover wp-code-coverage-multisite-clover-${{ github.sha }}.xml + run: npm run test:php-composer -- --verbose -c tests/phpunit/multisite.xml --coverage-clover wp-code-coverage-multisite-clover-${{ github.sha }}.xml - name: Ensure version-controlled files are not modified during the tests run: git diff --exit-code