diff --git a/.github/actions/wordpress/action.yml b/.github/actions/wordpress/action.yml new file mode 100644 index 000000000..6e2a2a4e4 --- /dev/null +++ b/.github/actions/wordpress/action.yml @@ -0,0 +1,35 @@ +name: Prepare WordPress +description: Restore the WordPress repository and its dependencies from the cache, or set them up. +runs: + using: composite + steps: + - name: Resolve WordPress cache key + id: wordpress + shell: bash + env: + # "wp-setup.sh" and the "db.php" source file decide what ends up in the "wordpress" folder. + SETUP_HASH: ${{ hashFiles( 'wp-setup.sh', 'packages/plugin-sqlite-database-integration/db.copy' ) }} + run: | + version="$( sed -n 's/^WP_VERSION="\(.*\)"$/\1/p' wp-setup.sh )" + commit="$( git ls-remote --exit-code https://github.com/WordPress/wordpress-develop.git "refs/heads/$version" | cut -f1 )" + echo "cache-key=wordpress-$( node --version )-$commit-$SETUP_HASH" >> "$GITHUB_OUTPUT" + + - name: Restore WordPress from the cache + id: cache + uses: actions/cache/restore@v6 + with: + path: wordpress + key: ${{ steps.wordpress.outputs.cache-key }} + + - name: Set up WordPress + if: steps.cache.outputs.cache-hit != 'true' + shell: bash + run: composer run wp-test-ensure-php-env + + # Save right after the setup, so that the cache doesn't include anything created by the tests. + - name: Save WordPress to the cache + if: steps.cache.outputs.cache-hit != 'true' + uses: actions/cache/save@v6 + with: + path: wordpress + key: ${{ steps.wordpress.outputs.cache-key }} diff --git a/.github/workflows/end-to-end-tests.yml b/.github/workflows/end-to-end-tests.yml index 635f6d467..79aa72b81 100644 --- a/.github/workflows/end-to-end-tests.yml +++ b/.github/workflows/end-to-end-tests.yml @@ -28,13 +28,9 @@ jobs: echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV - name: Prepare WordPress - run: composer run wp-setup + uses: ./.github/actions/wordpress - name: Run end-to-end tests uses: ./.github/actions/playwright with: test-script: test-e2e - - - name: Stop Docker containers - if: always() - run: composer run wp-test-clean diff --git a/.github/workflows/sqlite-plugin-phpunit-tests.yml b/.github/workflows/sqlite-plugin-phpunit-tests.yml index b23049558..bdf6edd1f 100644 --- a/.github/workflows/sqlite-plugin-phpunit-tests.yml +++ b/.github/workflows/sqlite-plugin-phpunit-tests.yml @@ -31,12 +31,11 @@ jobs: echo "PHP_FPM_UID=$(id -u)" >> $GITHUB_ENV echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV + - name: Prepare WordPress + uses: ./.github/actions/wordpress + - name: Run SQLite plugin single-site PHPUnit tests run: composer run wp-test-sqlite-plugin-php - name: Run SQLite plugin multisite PHPUnit tests run: composer run wp-test-sqlite-plugin-php-multisite - - - name: Stop Docker containers - if: always() - run: composer run wp-test-clean diff --git a/.github/workflows/wp-tests-end-to-end.yml b/.github/workflows/wp-tests-end-to-end.yml index ae57bc179..3d698be25 100644 --- a/.github/workflows/wp-tests-end-to-end.yml +++ b/.github/workflows/wp-tests-end-to-end.yml @@ -28,13 +28,9 @@ jobs: echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV - name: Prepare WordPress - run: composer run wp-setup + uses: ./.github/actions/wordpress - name: Run WordPress end-to-end tests uses: ./.github/actions/playwright with: test-script: wp-test-e2e - - - name: Stop Docker containers - if: always() - run: composer run wp-test-clean diff --git a/.github/workflows/wp-tests-phpunit.yml b/.github/workflows/wp-tests-phpunit.yml index 615ddc7b6..36269fb85 100644 --- a/.github/workflows/wp-tests-phpunit.yml +++ b/.github/workflows/wp-tests-phpunit.yml @@ -27,9 +27,8 @@ jobs: echo "PHP_FPM_UID=$(id -u)" >> $GITHUB_ENV echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV + - name: Prepare WordPress + uses: ./.github/actions/wordpress + - name: Run WordPress PHPUnit tests run: node .github/workflows/wp-tests-phpunit-run.js - - - name: Stop Docker containers - if: always() - run: composer run wp-test-clean diff --git a/composer.json b/composer.json index fa004320b..05ce5adb4 100644 --- a/composer.json +++ b/composer.json @@ -59,13 +59,17 @@ "npm --prefix wordpress run env:cli -- plugin install gutenberg --version=22.3.0", "npm --prefix wordpress run env:cli -- plugin install query-monitor" ], - "wp-test-ensure-env": [ - "if [ ! -f wordpress/src/wp-load.php ]; then composer run wp-setup; fi", + "wp-test-ensure-php-env": [ + "if [ ! -f wordpress/wp-tests-config.php ]; then composer run wp-setup; fi", "@putenv COMPOSE_IGNORE_ORPHANS=true", + "if [ ! -f wordpress/vendor/bin/phpunit ]; then npm --prefix wordpress run env:composer -- update -W; fi" + ], + "wp-test-ensure-env": [ + "@wp-test-ensure-php-env", "cd wordpress && if [ -z \"$(DOTENV_CONFIG_QUIET=true node tools/local-env/scripts/docker.js ps -q)\" ]; then cd ..; composer run wp-test-start; fi" ], "wp-test-php": [ - "@wp-test-ensure-env @no_additional_args", + "@wp-test-ensure-php-env @no_additional_args", "@wp-test-remove-database @no_additional_args", "cd wordpress && node tools/local-env/scripts/docker.js run --rm php php -d opcache.enable_cli=1 -d opcache.memory_consumption=256 -d opcache.jit=tracing -d opcache.jit_buffer_size=64M ./vendor/bin/phpunit @additional_args" ], diff --git a/wp-setup.sh b/wp-setup.sh index 3655f6f0d..6c9432ef6 100755 --- a/wp-setup.sh +++ b/wp-setup.sh @@ -74,3 +74,13 @@ cp "$WP_DIR/.env.example" "$WP_DIR/.env" echo "Installing dependencies..." npm --prefix "$WP_DIR" install npm --prefix "$WP_DIR" run build:dev + +# 9. Create "wp-tests-config.php" like "env:install" does, so that PHPUnit doesn't need the full environment. +echo "Creating the 'wp-tests-config.php' file..." +sed \ + -e "s/youremptytestdbnamehere/wordpress_develop_tests/" \ + -e "s/yourusernamehere/root/" \ + -e "s/yourpasswordhere/password/" \ + -e "s/localhost/mysql/" \ + "$WP_DIR/wp-tests-config-sample.php" > "$WP_DIR/wp-tests-config.php" +printf "\ndefine( 'FS_METHOD', 'direct' );\n" >> "$WP_DIR/wp-tests-config.php"