diff --git a/.github/workflows/end2end-test.yml b/.github/workflows/end2end-test.yml index 29a365ebbbea18..141b7d7420fa7a 100644 --- a/.github/workflows/end2end-test.yml +++ b/.github/workflows/end2end-test.yml @@ -42,16 +42,8 @@ jobs: npx playwright install chromium firefox webkit --with-deps - name: Start the server - run: | - npx @wp-playground/cli server \ - --wp=nightly \ - --mount=./:/wordpress/wp-content/plugins/gutenberg \ - --mount=./test/emptytheme:/wordpress/wp-content/themes/emptytheme \ - --mount=./test/gutenberg-test-themes:/wordpress/wp-content/themes/gutenberg-test-themes \ - --mount=./packages/e2e-tests/mu-plugins:/wordpress/wp-content/mu-plugins \ - --mount=./packages/e2e-tests/plugins:/wordpress/wp-content/plugins/gutenberg-test-plugins \ - --blueprint=./test/e2e/playground.blueprint.json \ - & while ! (jobs -p | xargs -r tail -n 0 -f 2>/dev/null | grep -q "WordPress is running"); do sleep 1; done + run: node playground-server.js + - name: Run the tests env: PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1 diff --git a/playground-server.js b/playground-server.js new file mode 100755 index 00000000000000..5c6563a600eb28 --- /dev/null +++ b/playground-server.js @@ -0,0 +1,76 @@ +/** + * External dependencies + */ +const { spawn } = require( 'child_process' ); +const fs = require( 'fs' ); + +// Function to start the server and wait until it is provisioned +function startWPServer() { + return new Promise( ( resolve, reject ) => { + const wpServer = spawn( + 'npx', + [ + '@wp-playground/cli', + 'server', + '--wp=nightly', + '--mount=./:/wordpress/wp-content/plugins/gutenberg', + '--mount=./test/emptytheme:/wordpress/wp-content/themes/emptytheme', + '--mount=./test/gutenberg-test-themes:/wordpress/wp-content/themes/gutenberg-test-themes', + '--mount=./packages/e2e-tests/mu-plugins:/wordpress/wp-content/mu-plugins', + '--mount=./packages/e2e-tests/plugins:/wordpress/wp-content/plugins/gutenberg-test-plugins', + '--blueprint=./test/e2e/playground.blueprint.json', + ], + { + detached: true, + stdio: [ 'ignore', 'pipe', 'pipe' ], // Capture stdout and stderr + } + ); + + // Write the process ID to a file for later management + fs.writeFileSync( 'wp-server.pid', wpServer.pid.toString(), 'utf-8' ); + + // Listen to stdout and wait for the "WordPress is running" message + wpServer.stdout.on( 'data', ( data ) => { + const output = data.toString(); + // eslint-disable-next-line no-console + console.log( output ); // Optional: log the output + + if ( + output.includes( + 'WordPress is running on http://127.0.0.1:9400' + ) + ) { + resolve( wpServer ); // Resolve the promise when server is ready + } + } ); + + // Handle errors from stderr + wpServer.stderr.on( 'data', ( data ) => { + reject( new Error( `Server error: ${ data.toString() }` ) ); + } ); + + // Handle close event + wpServer.on( 'close', ( code ) => { + if ( code !== 0 ) { + reject( + new Error( + `WP Playground server exited with code ${ code }` + ) + ); + } + } ); + + // Detach the process + wpServer.unref(); + } ); +} + +// Usage of the promisified function +startWPServer() + .then( () => { + process.exit( 0 ); // Exit the Node.js process once the server is provisioned + } ) + .catch( ( error ) => { + // eslint-disable-next-line no-console + console.error( error ); // Log the error + } );