diff --git a/.github/actions/playwright/action.yml b/.github/actions/playwright/action.yml new file mode 100644 index 000000000..7b619248a --- /dev/null +++ b/.github/actions/playwright/action.yml @@ -0,0 +1,63 @@ +name: Run Playwright tests +description: Run E2E tests with browsers from the matching official Playwright image. +inputs: + test-script: + description: Composer script that runs the E2E tests. + required: true +runs: + using: composite + steps: + - name: Resolve Playwright version + id: playwright + shell: bash + working-directory: wordpress + run: | + node <<'NODE' >> "$GITHUB_ENV" + const { createRequire } = require( 'node:module' ); + const { realpathSync } = require( 'node:fs' ); + const path = require( 'node:path' ); + const testRequire = createRequire( require.resolve( '@playwright/test/package.json' ) ); + const playwright = testRequire.resolve( 'playwright/package.json' ); + console.log( `PLAYWRIGHT_VERSION=${require( playwright ).version}` ); + console.log( `PLAYWRIGHT_PACKAGE=${path.dirname( playwright )}` ); + console.log( `PLAYWRIGHT_NODE_MODULES=${realpathSync( 'node_modules' )}` ); + NODE + + - name: Start Playwright browser server + shell: bash + run: | + # Pull the WordPress images meanwhile, so the test script can start them sooner. + npm --prefix wordpress run env:pull -- --quiet & + wordpress_pull=$! + + # Bound each attempt, as a stalled pull would otherwise run into the job timeout. + for attempt in 1 2 3; do + timeout 3m docker compose -f "$GITHUB_ACTION_PATH/docker-compose.yml" pull && break + if [ "$attempt" -eq 3 ]; then + echo "Failed to pull the Playwright image after $attempt attempts." >&2 + exit 1 + fi + echo "Pulling the Playwright image failed. Retrying in $(( attempt * 10 )) seconds..." + sleep $(( attempt * 10 )) + done + + docker compose -f "$GITHUB_ACTION_PATH/docker-compose.yml" up --wait --wait-timeout 60 + wait "$wordpress_pull" + + - name: Run E2E tests + shell: bash + env: + TEST_SCRIPT: ${{ inputs.test-script }} + PW_TEST_CONNECT_WS_ENDPOINT: ws://127.0.0.1:3000/ + PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1' + run: composer run "$TEST_SCRIPT" + + - name: Show browser server logs + if: ( failure() || cancelled() ) && steps.playwright.outcome == 'success' + shell: bash + run: docker compose -f "$GITHUB_ACTION_PATH/docker-compose.yml" logs + + - name: Stop Playwright browser server + if: always() && steps.playwright.outcome == 'success' + shell: bash + run: docker compose -f "$GITHUB_ACTION_PATH/docker-compose.yml" down diff --git a/.github/actions/playwright/docker-compose.yml b/.github/actions/playwright/docker-compose.yml new file mode 100644 index 000000000..a56fb7006 --- /dev/null +++ b/.github/actions/playwright/docker-compose.yml @@ -0,0 +1,22 @@ +name: sqlite-playwright +services: + playwright: + image: mcr.microsoft.com/playwright:v${PLAYWRIGHT_VERSION}-noble + init: true + network_mode: host + ipc: host + volumes: + - ${PLAYWRIGHT_NODE_MODULES}:${PLAYWRIGHT_NODE_MODULES}:ro + entrypoint: node + command: [ '${PLAYWRIGHT_PACKAGE}/cli.js', run-server, --host, '127.0.0.1', --port, '3000' ] + healthcheck: + test: + - CMD + - node + - -e + - "fetch('http://127.0.0.1:3000/').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))" + start_period: 30s + start_interval: 1s + interval: 30s + timeout: 5s + retries: 3 diff --git a/.github/workflows/end-to-end-tests.yml b/.github/workflows/end-to-end-tests.yml index 6af5496a1..635f6d467 100644 --- a/.github/workflows/end-to-end-tests.yml +++ b/.github/workflows/end-to-end-tests.yml @@ -22,19 +22,18 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Set UID and GID for PHP in WordPress images run: | echo "PHP_FPM_UID=$(id -u)" >> $GITHUB_ENV echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV - - name: Install Playwright browsers - run: npx playwright install --with-deps + - name: Prepare WordPress + run: composer run wp-setup - name: Run end-to-end tests - run: composer run test-e2e + uses: ./.github/actions/playwright + with: + test-script: test-e2e - name: Stop Docker containers if: always() diff --git a/.github/workflows/sqlite-plugin-phpunit-tests.yml b/.github/workflows/sqlite-plugin-phpunit-tests.yml index cc0ff84d4..b23049558 100644 --- a/.github/workflows/sqlite-plugin-phpunit-tests.yml +++ b/.github/workflows/sqlite-plugin-phpunit-tests.yml @@ -26,9 +26,6 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Set UID and GID for PHP in WordPress images run: | echo "PHP_FPM_UID=$(id -u)" >> $GITHUB_ENV diff --git a/.github/workflows/wp-tests-end-to-end.yml b/.github/workflows/wp-tests-end-to-end.yml index 338140bbb..ae57bc179 100644 --- a/.github/workflows/wp-tests-end-to-end.yml +++ b/.github/workflows/wp-tests-end-to-end.yml @@ -22,19 +22,18 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Set UID and GID for PHP in WordPress images run: | echo "PHP_FPM_UID=$(id -u)" >> $GITHUB_ENV echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV - - name: Install Playwright browsers - run: npx playwright install --with-deps + - name: Prepare WordPress + run: composer run wp-setup - name: Run WordPress end-to-end tests - run: composer run wp-test-e2e + uses: ./.github/actions/playwright + with: + test-script: wp-test-e2e - name: Stop Docker containers if: always() diff --git a/.github/workflows/wp-tests-phpunit.yml b/.github/workflows/wp-tests-phpunit.yml index 1b791045f..615ddc7b6 100644 --- a/.github/workflows/wp-tests-phpunit.yml +++ b/.github/workflows/wp-tests-phpunit.yml @@ -22,9 +22,6 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Set UID and GID for PHP in WordPress images run: | echo "PHP_FPM_UID=$(id -u)" >> $GITHUB_ENV diff --git a/composer.json b/composer.json index 81bc4657d..fa004320b 100644 --- a/composer.json +++ b/composer.json @@ -67,7 +67,7 @@ "wp-test-php": [ "@wp-test-ensure-env @no_additional_args", "@wp-test-remove-database @no_additional_args", - "npm --prefix wordpress run test:php -- @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" ], "wp-test-sqlite-plugin-php": [ "@wp-test-php /var/www/tests-sqlite-database-integration/phpunit" diff --git a/wp-setup.sh b/wp-setup.sh index a0e9f1267..3655f6f0d 100755 --- a/wp-setup.sh +++ b/wp-setup.sh @@ -66,7 +66,11 @@ sed -i.bak "s#class WpdbExposedMethodsForTesting extends wpdb {#class WpdbExpose echo "Disabling the PHP OPcache revalidation delay..." printf '\nopcache.revalidate_freq = 0\n' >> "$WP_DIR/tools/local-env/php-config.ini" -# 7. Install dependencies. +# 7. Create the ".env" file early, so that Docker commands before "env:start" use it too. +echo "Creating the '.env' file..." +cp "$WP_DIR/.env.example" "$WP_DIR/.env" + +# 8. Install dependencies. echo "Installing dependencies..." npm --prefix "$WP_DIR" install npm --prefix "$WP_DIR" run build:dev