From 242bdb475d763dca98ff283aa0c6a42ecaf30fc6 Mon Sep 17 00:00:00 2001 From: Sam Seay Date: Mon, 1 Feb 2021 15:24:11 +1300 Subject: [PATCH] Migrate the Travis unit test suite to Github actions (https://github.com/woocommerce/woocommerce-admin/pull/6164) Add a matrix of tests against WP and WC versions and migrate the lint and js test to separate jobs. --- .../.github/workflows/e2e.yml | 4 + .../.github/workflows/lint-php.yml | 25 +++ .../.github/workflows/lint-test-js.yml | 20 +++ .../.github/workflows/test-php.yml | 69 ++++++++ plugins/woocommerce-admin/.travis.yml | 79 --------- .../bin/ci/gh-install-wp-tests.sh | 153 ++++++++++++++++++ plugins/woocommerce-admin/bin/js_lint_test.sh | 11 +- plugins/woocommerce-admin/bin/phpcs.sh | 21 ++- 8 files changed, 290 insertions(+), 92 deletions(-) create mode 100644 plugins/woocommerce-admin/.github/workflows/lint-php.yml create mode 100644 plugins/woocommerce-admin/.github/workflows/lint-test-js.yml create mode 100644 plugins/woocommerce-admin/.github/workflows/test-php.yml delete mode 100755 plugins/woocommerce-admin/.travis.yml create mode 100755 plugins/woocommerce-admin/bin/ci/gh-install-wp-tests.sh diff --git a/plugins/woocommerce-admin/.github/workflows/e2e.yml b/plugins/woocommerce-admin/.github/workflows/e2e.yml index 72014ec45f5..9d34e352df6 100644 --- a/plugins/woocommerce-admin/.github/workflows/e2e.yml +++ b/plugins/woocommerce-admin/.github/workflows/e2e.yml @@ -5,6 +5,10 @@ jobs: e2e-tests: runs-on: ubuntu-latest steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.7.0 + with: + access_token: ${{ github.token }} - name: Check out repository code uses: actions/checkout@v2 - name: Setup PHP diff --git a/plugins/woocommerce-admin/.github/workflows/lint-php.yml b/plugins/woocommerce-admin/.github/workflows/lint-php.yml new file mode 100644 index 00000000000..456b3ffba26 --- /dev/null +++ b/plugins/woocommerce-admin/.github/workflows/lint-php.yml @@ -0,0 +1,25 @@ +name: Lint the PHP +on: [pull_request] + +jobs: + lint-php: + runs-on: ubuntu-latest + steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.7.0 + with: + access_token: ${{ github.token }} + - name: Check out repository code + uses: actions/checkout@v2 + - name: Determine changed files + id: changed-files + uses: wyrihaximus/github-action-files-in-commit@v1.0 + - name: Setup PHP + uses: shivammathur/setup-php@2.9.0 + with: + php-version: 7.3 + - name: Lint the PHP + env: + CHANGED_FILES: ${{ steps.changed-files.outputs.files }} + run: bin/phpcs.sh + shell: bash diff --git a/plugins/woocommerce-admin/.github/workflows/lint-test-js.yml b/plugins/woocommerce-admin/.github/workflows/lint-test-js.yml new file mode 100644 index 00000000000..7b5411b30cb --- /dev/null +++ b/plugins/woocommerce-admin/.github/workflows/lint-test-js.yml @@ -0,0 +1,20 @@ +name: Lint and test JS +on: [pull_request] + +jobs: + test-lint-js: + runs-on: ubuntu-latest + steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.7.0 + with: + access_token: ${{ github.token }} + - name: Check out repository code + uses: actions/checkout@v2 + - name: Setup Node.js + uses: actions/setup-node@v2-beta + with: + node-version: '14' + - name: Lint and test the JS + run: bin/js_lint_test.sh + shell: bash diff --git a/plugins/woocommerce-admin/.github/workflows/test-php.yml b/plugins/woocommerce-admin/.github/workflows/test-php.yml new file mode 100644 index 00000000000..6313a1e50b9 --- /dev/null +++ b/plugins/woocommerce-admin/.github/workflows/test-php.yml @@ -0,0 +1,69 @@ +name: Run PHP unit tests +on: [pull_request] + +jobs: + test-php: + env: + WP_CORE_DIR: '/tmp/wordpress' + COMPOSER_DEV: 1 + + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php: ['7.1', '7.2', '7.3'] + wordpress: ['5.3', '5.6'] + woocommerce: ['4.8.0', '4.9.1'] + phpunit: ['7.5.20'] + composer: ['2.0.6'] + include: + - php: '7.0' + wordpress: '5.5' + woocommerce: 'latest' + phpunit: '6.5.9' + composer: '1.10.19' + - php: '7.0' + wordpress: '5.6' + woocommerce: '4.9.1' + phpunit: '6.5.9' + composer: '2.0.6' + - php: '7.3' + wordpress: '5.6' + woocommerce: '4.7.0' + phpunit: '6.5.9' + composer: '1.10.19' + steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.7.0 + with: + access_token: ${{ github.token }} + - name: Check out repository code + uses: actions/checkout@v2 + - name: Setup PHP + uses: shivammathur/setup-php@2.9.0 + with: + php-version: ${{matrix.php}} + tools: phpunit:${{matrix.phpunit}} + extensions: mysqli + - name: Setup Node.js + uses: actions/setup-node@v2-beta + with: + node-version: '14' + - name: Set up the tests + env: + WP_VERSION: ${{matrix.wordpress}} + WC_VERSION: ${{matrix.woocommerce}} + PHP_UNIT: ${{matrix.phpunit}} + COMPOSER_VERSION: ${{matrix.composer}} + run: | + sudo /etc/init.d/mysql start + bash bin/ci/gh-install-wp-tests.sh wc_admin_test root 'root' localhost + cd "$WP_CORE_DIR/wp-content/plugins/woocommerce-admin/" + npm run build:feature-config + composer install --no-dev + node --version + npm --version + timedatectl + - name: Run the PHP unit tests + run: bin/phpunit.sh + shell: bash diff --git a/plugins/woocommerce-admin/.travis.yml b/plugins/woocommerce-admin/.travis.yml deleted file mode 100755 index 9a56a7452bc..00000000000 --- a/plugins/woocommerce-admin/.travis.yml +++ /dev/null @@ -1,79 +0,0 @@ -sudo: required - -language: php - -dist: xenial - -services: - - xvfb - - mysql - - docker - -env: - - global: - - WC_VERSION=4.5.0 - -matrix: - include: - - name: 'PHP 7.3 unit tests, PHP Coding standards check and JS tests' - php: 7.3 - env: WP_VERSION=latest WP_MULTISITE=0 WP_CORE_DIR=/tmp/wordpress RUN_PHPCS=1 RUN_JS=1 COMPOSER_DEV=1 - - name: 'Latest WooCommerce' - php: 7.3 - env: WP_VERSION=latest WP_MULTISITE=0 WP_CORE_DIR=/tmp/wordpress COMPOSER_DEV=1 WC_VERSION= - - name: 'PHP 7.3 unit tests, run in random order' - php: 7.3 - env: WP_VERSION=latest WP_MULTISITE=0 WP_CORE_DIR=/tmp/wordpress RUN_RANDOM=1 COMPOSER_DEV=1 - - name: 'PHP 7.2 unit tests' - php: 7.2 - env: WP_VERSION=latest WP_MULTISITE=0 WP_CORE_DIR=/tmp/wordpress COMPOSER_DEV=1 - - name: 'PHP 7.1 unit tests' - php: 7.1 - env: WP_VERSION=latest WP_MULTISITE=0 WP_CORE_DIR=/tmp/wordpress COMPOSER_DEV=1 - - name: 'PHP 7.0 unit tests' - php: 7.0 - env: WP_VERSION=latest WP_MULTISITE=0 WP_CORE_DIR=/tmp/wordpress PHPUNIT=6 - allow_failures: - - php: 7.3 - env: WP_VERSION=latest WP_MULTISITE=0 WP_CORE_DIR=/tmp/wordpress RUN_RANDOM=1 COMPOSER_DEV=1 - - env: WP_VERSION=latest WP_MULTISITE=0 WP_CORE_DIR=/tmp/wordpress COMPOSER_DEV=1 WC_VERSION= - -before_install: - - nvm install 'lts/*' - -before_script: - - phpenv config-rm xdebug.ini - - export PATH="$WP_CORE_DIR/wp-content/plugins/woocommerce-admin/vendor/bin:$PATH" - - bash bin/install-wp-tests.sh wc_admin_test root '' localhost $WP_VERSION - - bash bin/travis.sh before - - node --version - - npm --version - - timedatectl - -script: - - bash bin/js_lint_test.sh - - bash bin/phpunit.sh - - bash bin/phpcs.sh - -branches: - only: - - main - -before_deploy: - # Remove our unneeded symlink. - - rm docs/components/packages - # Copy each component README to the docs folder. - - find packages/components/src -type f -name 'README.md' | sed 's/\(packages\/components\/src\/\)\(.*\)README\.md/docs\/components\/packages\/\2/g' | xargs mkdir -p - - find packages/components/src -type f -name 'README.md' | sed 's/\(packages\/components\/src\/\)\(.*\)\(README\.md\)/\1\2\3 docs\/components\/packages\/\2/g' | xargs -n2 cp - -deploy: - local_dir: docs - provider: pages - skip_cleanup: true - github_token: $GH_PAGES_TOKEN - keep_history: true - on: - condition: $RUN_JS == 1 # only deploy on the PHP 7.2/JS build - branch: main - target_branch: gh-pages - verbose: true diff --git a/plugins/woocommerce-admin/bin/ci/gh-install-wp-tests.sh b/plugins/woocommerce-admin/bin/ci/gh-install-wp-tests.sh new file mode 100755 index 00000000000..adb259bd604 --- /dev/null +++ b/plugins/woocommerce-admin/bin/ci/gh-install-wp-tests.sh @@ -0,0 +1,153 @@ +#!/usr/bin/env bash + +if [ $# -lt 3 ]; then + echo "usage: $0 " + exit 1 +fi + +DB_NAME=$1 +DB_USER=$2 +DB_PASS=$3 +DB_HOST='localhost' + +TMPDIR=${TMPDIR-/tmp} +TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") +WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib} +WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/} +WP_TESTS_TAG="branches/$WP_VERSION" + +download() { + if [ `which curl` ]; then + curl -s "$1" > "$2"; + elif [ `which wget` ]; then + wget -nv -O "$2" "$1" + fi +} + +set -ex + +install_wp() { + echo "::group::{install_wp}" + mkdir -p $WP_CORE_DIR + download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json + if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then + # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x + LATEST_VERSION=${WP_VERSION%??} + else + # otherwise, scan the releases and get the most up to date minor version of the major release + local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'` + LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1) + fi + + if [[ -z "$LATEST_VERSION" ]]; then + local ARCHIVE_NAME="wordpress-$WP_VERSION" + else + local ARCHIVE_NAME="wordpress-$LATEST_VERSION" + fi + + download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz + tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR + download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php + echo "::endgroup::" +} + +install_test_suite() { + echo "::group::{install_test_suite}" + # portable in-place argument for both GNU sed and Mac OSX sed + if [[ $(uname -s) == 'Darwin' ]]; then + local ioption='-i .bak' + else + local ioption='-i' + fi + + # removes testing suite + rm -rf $WP_TESTS_DIR + + # set up testing suite if it doesn't yet exist + if [ ! -d $WP_TESTS_DIR ]; then + # set up testing suite + mkdir -p $WP_TESTS_DIR + svn co https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes + fi + + if [ ! -f wp-tests-config.php ]; then + download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php + # remove all forward slashes in the end + WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::") + sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php + fi + echo "::endgroup::" +} + +install_db() { + echo "::group::{install_db}" + # drop existing database + echo "DROP DATABASE IF EXISTS $DB_NAME" | mysql --user="root" --password="$DB_PASS" + + # create database + echo "CREATE DATABASE IF NOT EXISTS $DB_NAME" | mysql --user="root" --password="$DB_PASS" + echo "::endgroup::" +} + +install_deps() { + echo "::group::{install_deps}" + # Script Variables + WP_SITE_URL="http://local.wordpress.test" + WORKING_DIR="$PWD" + + # Set up WordPress using wp-cli + mkdir -p "$WP_CORE_DIR" + cd "$WP_CORE_DIR" + + curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar + + # delete existing wp-config and woocommerce repository + rm -f wp-config.php + rm -rf wp-content/plugins/woocommerce + + php wp-cli.phar core config --dbname=$DB_NAME --dbuser=root --dbpass=$DB_PASS --dbhost=localhost --dbprefix=wptests_ --allow-root + php wp-cli.phar core install --url="$WP_SITE_URL" --title="Example" --admin_user=admin --admin_password=password --admin_email=info@example.com --path=$WP_CORE_DIR --skip-email --allow-root + + # Install WooCommerce (latest non-hyphenated (beta, RC) tag) + if [[ "$WC_VERSION" == "latest" ]]; then + LATEST_WC_TAG="$(git ls-remote --tags https://github.com/woocommerce/woocommerce.git | awk '{print $2}' | sed 's/^refs\/tags\///' | grep -E '^[0-9]\.[0-9]\.[0-9]$' | sort -V | tail -n 1)" + else + LATEST_WC_TAG="$WC_VERSION" + fi + + cd "wp-content/plugins/" + # As zip file does not include tests, we have to get it from git repo. + git clone --depth 1 --branch $LATEST_WC_TAG https://github.com/woocommerce/woocommerce.git + + # Bring in WooCommerce Core dependencies + composer self-update $COMPOSER_VERSION + cd "woocommerce" + composer install --no-dev + composer self-update 2.0.6 + + # Activate WooCommerce + cd "$WP_CORE_DIR" + php wp-cli.phar plugin activate woocommerce + + # Install woocommerce-admin + cd "$WP_CORE_DIR/wp-content/plugins" + cp -R $GITHUB_WORKSPACE ./ + + # Activate woocommerce-admin + cd "$WP_CORE_DIR" + php wp-cli.phar plugin activate woocommerce-admin + + # Back to original dir + cd "$WORKING_DIR" + + echo "::endgroup::" +} + +install_wp +install_test_suite +install_db +install_deps diff --git a/plugins/woocommerce-admin/bin/js_lint_test.sh b/plugins/woocommerce-admin/bin/js_lint_test.sh index d3113db1b6e..a11e6aa9c4f 100755 --- a/plugins/woocommerce-admin/bin/js_lint_test.sh +++ b/plugins/woocommerce-admin/bin/js_lint_test.sh @@ -2,9 +2,8 @@ set -o errexit -if [[ ${RUN_JS} == 1 ]]; then - npm run -s install-if-deps-outdated - npm run lint - npm run build - npm test -fi +npm run -s install-if-deps-outdated +npm run lint +npm run build +npm test + diff --git a/plugins/woocommerce-admin/bin/phpcs.sh b/plugins/woocommerce-admin/bin/phpcs.sh index 2377c497b29..84aaf62e9a4 100755 --- a/plugins/woocommerce-admin/bin/phpcs.sh +++ b/plugins/woocommerce-admin/bin/phpcs.sh @@ -1,11 +1,18 @@ #!/usr/bin/env bash +PHP_FILES_CHANGED="" -if [[ ${RUN_PHPCS} == 1 ]]; then - CHANGED_FILES=`git diff --name-only --diff-filter=ACMR $TRAVIS_COMMIT_RANGE | grep \\\\.php | awk '{print}' ORS=' '` +for FILE in $(echo $CHANGED_FILES | tr ',' '\n') +do + if [[ $FILE =~ ".php" ]]; then + PHP_FILES_CHANGED += "$FILE," + fi +done - if [ "$CHANGED_FILES" != "" ]; then - echo "Running Code Sniffer." - cd "$WP_CORE_DIR/wp-content/plugins/woocommerce-admin/" - ./vendor/bin/phpcs --encoding=utf-8 -n -p $CHANGED_FILES - fi +if [ "$PHP_FILES_CHANGED" != "" ]; then + composer install + echo "Running Code Sniffer." + ./vendor/bin/phpcs --encoding=utf-8 -n -p $PHP_FILES_CHANGED +else + echo "No changed files detected, sniffer not run." fi +