From b9c0431c49e79504c2e17d749f7c170ce8bbbf1a Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 22 Mar 2021 01:17:30 +0100 Subject: [PATCH] CI: switch to GitHub Actions - step 3: test and coverage stage This commit: * Adds a GH Actions workflow for running the unit tests against PHP 5.5 - current. Note: - This workflow will run for all pull requests and for merges to master, but not for other push events. - Code coverage will be checked for the highest and lowest PHP version so the results can be combined to cover any polyfills and work-arounds. Includes adjusting the codecov configuration to expect 2 reports for each build. - The PHP 8.1 (nightly) job is "allowed to fail" (`experimental` = true). If it does fail, the build status will unfortunately show as "failed", even though all non-experimental jobs have succeeded. This is a known issue in GHA: https://github.com/actions/toolkit/issues/399 - For PHP 5.2-5.4, the tests will still be run via Travis as the test server setup is incompatible with PHP < 5.5 and using multiple PHP versions in a workflow run does work on Travis, but doesn't work the same on GH Actions. * Strips down the `.travis.yml` configuration to the bare minimum to run the tests against PHP 5.2 - 5.4. * Adds a "Build Status" badge in the Readme to use the results from this particular GH Actions runs. Note: for the time being, the tests will not be run against PHP 8.1/nightly as PHPUnit 7 is incompatible, meaning that that build would _always_ fail. Once the version constraints for PHHUnit have been widened (see PR 446), the build against PHP nightly should be enabled. Co-authored-by: Alain Schlesser --- .codecov.yml | 4 ++ .github/workflows/test.yml | 144 +++++++++++++++++++++++++++++++++++++ .travis.yml | 50 +++---------- README.md | 3 +- 4 files changed, 159 insertions(+), 42 deletions(-) create mode 100644 .github/workflows/test.yml diff --git a/.codecov.yml b/.codecov.yml index 6db29e875..63f22d276 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -1,3 +1,7 @@ +codecov: + notify: + after_n_builds: 2 + coverage: round: nearest # Status will be green when coverage is between 85 and 100%. diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 000000000..26daef5e0 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,144 @@ +name: Test + +on: + push: + branches: + - master + pull_request: + # Allow manually triggering the workflow. + workflow_dispatch: + +jobs: + #### TEST STAGE #### + test: + runs-on: ubuntu-latest + + strategy: + # Keys: + # - coverage: Whether to run the tests with code coverage. + # - experimental: Whether the build is "allowed to fail". + matrix: + php: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4'] + coverage: [false] + experimental: [false] + + include: + # Run code coverage on high/low PHP. + - php: '5.5' + coverage: true + experimental: false + - php: '8.0' + coverage: true + experimental: false + + # Test against PHP Nightly. + # This should be enabled once the PHPUnit version constraints have been widened. + #- php: '8.1' + # coverage: false + # experimental: true + + name: "Test: PHP ${{ matrix.php }}" + + continue-on-error: ${{ matrix.experimental }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set coverage variable + id: set_cov + run: | + if [ ${{ matrix.coverage }} == "true" ]; then + echo '::set-output name=COV::xdebug' + else + echo '::set-output name=COV::none' + fi + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: ${{ steps.set_cov.outputs.COV }} + tools: cs2pr + + # Install dependencies and handle caching in one go. + # @link https://github.com/marketplace/actions/install-composer-dependencies + - name: Install Composer dependencies - normal + if: ${{ startsWith( matrix.php, '8' ) == false && matrix.php != 'latest' }} + uses: "ramsey/composer-install@v1" + + # For PHP 8.0 and "nightly", we need to install with ignore platform reqs. + - name: Install Composer dependencies - with ignore platform + if: ${{ startsWith( matrix.php, '8' ) || matrix.php == 'latest' }} + uses: "ramsey/composer-install@v1" + with: + composer-options: --ignore-platform-reqs + + - name: Setup problem matcher to provide annotations for PHPUnit + run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: '3.8' + + - name: Setup proxy server + run: pip3 install mitmproxy + + - name: Start test server + run: | + PORT=8080 vendor/bin/start.sh + echo "REQUESTS_TEST_HOST_HTTP=localhost:8080" >> $GITHUB_ENV + + - name: Start proxy server + run: | + PORT=9002 tests/utils/proxy/start.sh + PORT=9003 AUTH="test:pass" tests/utils/proxy/start.sh + echo "REQUESTS_HTTP_PROXY=localhost:9002" >> $GITHUB_ENV + echo "REQUESTS_HTTP_PROXY_AUTH=localhost:9003" >> $GITHUB_ENV + echo "REQUESTS_HTTP_PROXY_AUTH_USER=test" >> $GITHUB_ENV + echo "REQUESTS_HTTP_PROXY_AUTH_PASS=pass" >> $GITHUB_ENV + + - name: Ensure the HTTPS test instance on Heroku is spun up + run: curl -s -I http://requests-php-tests.herokuapp.com/ > /dev/null + + - name: Check mitmproxy version + run: mitmdump --version + + - name: Ping localhost domain + run: ping -c1 localhost + + - name: Access localhost on port 8080 + run: curl -i http://localhost:8080 + + - name: Access localhost on port 9002 + run: curl -i http://localhost:9002 + + - name: Show PHPUnit version + run: vendor/bin/phpunit --version + + - name: Run the unit tests, no code coverage + if: ${{ matrix.coverage == false }} + run: composer test + + - name: Run the unit tests with code coverage + if: ${{ matrix.coverage == true }} + run: vendor/bin/phpunit --coverage-clover clover.xml + + - name: Stop proxy server + continue-on-error: true + run: | + PORT=9002 tests/utils/proxy/stop.sh + PORT=9003 tests/utils/proxy/stop.sh + + - name: Stop test server + continue-on-error: true + run: vendor/bin/stop.sh + + - name: Send coverage report to Codecov + if: ${{ success() && matrix.coverage == true }} + uses: codecov/codecov-action@v1 + with: + files: ./clover.xml + fail_ci_if_error: true + verbose: true diff --git a/.travis.yml b/.travis.yml index 9ede64347..d35723933 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,36 +1,15 @@ dist: xenial language: php -env: - - COMPOSER_PHPUNIT=true - jobs: fast_finish: true include: - php: 5.2 dist: precise - env: COMPOSER_PHPUNIT=false - php: 5.3 dist: precise - env: COMPOSER_PHPUNIT=false - php: 5.4 dist: trusty - env: COMPOSER_PHPUNIT=false - - php: 5.5 - dist: trusty - env: COMPOSER_PHPUNIT=false - - php: 5.6 - env: TEST_COVERAGE=1 - - php: 7.0 - - php: 7.1 - - php: 7.2 - - php: 7.3 - - php: 7.4 - - php: 8.0 - - php: nightly - - allow_failures: - - php: nightly cache: directories: @@ -41,28 +20,20 @@ cache: - $HOME/.cache/composer/files install: - # Speed up build time by disabling Xdebug unless actually needed. + # Speed up build time by disabling Xdebug. # https://johnblackbourn.com/reducing-travis-ci-build-times-for-wordpress-projects/ # https://twitter.com/kelunik/status/954242454676475904 - - if [ "$TEST_COVERAGE" != '1' ]; then phpenv config-rm xdebug.ini || echo 'No xdebug config.'; fi + - phpenv config-rm xdebug.ini || echo 'No xdebug config.' # Setup the test server - - if [ "$COMPOSER_PHPUNIT" == 'false' ]; then phpenv local $( phpenv versions | grep 5.6 | tail -1 ); fi - - if [ "$COMPOSER_PHPUNIT" == 'false' ]; then travis_retry composer remove --dev --no-update phpunit/phpunit; fi - - if [ "$COMPOSER_PHPUNIT" == 'false' ]; then export PHPUNIT_BIN="phpunit"; - else export PHPUNIT_BIN="$(pwd)/vendor/bin/phpunit"; - fi + - phpenv local $( phpenv versions | grep 5.6 | tail -1 ) # The PHPCS and lint dependencies require PHP 5.3, so would block install on PHP < 5.3. # As they are only needed for code sniffing, remove them. - - travis_retry composer remove --dev --no-update squizlabs/php_codesniffer phpcompatibility/php-compatibility wp-coding-standards/wpcs dealerdirect/phpcodesniffer-composer-installer php-parallel-lint/php-parallel-lint php-parallel-lint/php-console-highlighter - - | - if [[ ${TRAVIS_PHP_VERSION:0:1} == "8" || $TRAVIS_PHP_VERSION == "nightly" ]]; then - travis_retry composer install --no-interaction --ignore-platform-reqs - else - travis_retry composer install --no-interaction - fi + # Also remove PHPUnit as we'll be using the Phar provided by Travis. + - travis_retry composer remove --dev --no-update phpunit/phpunit squizlabs/php_codesniffer phpcompatibility/php-compatibility wp-coding-standards/wpcs dealerdirect/phpcodesniffer-composer-installer php-parallel-lint/php-parallel-lint php-parallel-lint/php-console-highlighter + - travis_retry composer install --no-interaction - TESTPHPBIN=$(phpenv which php) - - if [ "$COMPOSER_PHPUNIT" == 'false' ]; then phpenv local --unset; fi + - phpenv local --unset # Setup the proxy - pip install --user mitmproxy==0.18.2 @@ -82,7 +53,7 @@ before_script: - curl -s -I http://requests-php-tests.herokuapp.com/ > /dev/null # Environment checks - - $PHPUNIT_BIN --version + - phpunit --version script: # Lint PHP files against parse errors for PHP 5.2 - 5.4. @@ -97,10 +68,8 @@ script: # Run the unit tests. - | - if [ "$TEST_COVERAGE" == '1' ]; then - $PHPUNIT_BIN --coverage-clover clover.xml; # PHPUnit 4.x does not yet support the `no-coverage` flag. - elif [ ${TRAVIS_PHP_VERSION:0:3} == "5.2" ]; then + if [ ${TRAVIS_PHP_VERSION:0:3} == "5.2" ]; then $PHPUNIT_BIN; else $PHPUNIT_BIN --no-coverage; @@ -109,4 +78,3 @@ script: after_script: - tests/utils/proxy/stop.sh - PATH=$PATH vendor/bin/stop.sh - - test $TEST_COVERAGE && bash <(curl -s https://codecov.io/bash) diff --git a/README.md b/README.md index 30370b551..1c80258d2 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ Requests for PHP [![CS](https://github.com/WordPress/Requests/actions/workflows/cs.yml/badge.svg)](https://github.com/WordPress/Requests/actions/workflows/cs.yml) [![Lint](https://github.com/WordPress/Requests/actions/workflows/lint.yml/badge.svg)](https://github.com/WordPress/Requests/actions/workflows/lint.yml) -[![Build Status](https://travis-ci.org/WordPress/Requests.svg?branch=master)](https://travis-ci.org/WordPress/Requests) +[![Test](https://github.com/WordPress/Requests/actions/workflows/test.yml/badge.svg)](https://github.com/WordPress/Requests/actions/workflows/test.yml) +[![CI PHP 5.2-5.4](https://travis-ci.org/WordPress/Requests.svg?branch=master)](https://travis-ci.org/WordPress/Requests) [![codecov.io](http://codecov.io/github/WordPress/Requests/coverage.svg?branch=master)](http://codecov.io/github/WordPress/Requests?branch=master) Requests is a HTTP library written in PHP, for human beings. It is roughly