[e2e tests] Add environment info rest route and environment reporter (#49988)

This commit is contained in:
Adrian Moldovan 2024-07-25 19:07:25 +01:00 committed by GitHub
parent e74464fa17
commit 1b2e06a6f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 109 additions and 8 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
E2E tests: add environment reporter

View File

@ -73,7 +73,8 @@
"update-wp-env": "php ./tests/e2e-pw/bin/update-wp-env.php",
"watch:build": "pnpm --if-present --workspace-concurrency=Infinity --filter=\"$npm_package_name...\" --parallel '/^watch:build:project:.*$/'",
"watch:build:project": "pnpm --if-present run '/^watch:build:project:.*$/'",
"watch:build:project:copy-assets": "wireit"
"watch:build:project:copy-assets": "wireit",
"wp-env": "wp-env"
},
"lint-staged": {
"*.php": [

View File

@ -34,6 +34,16 @@ function register_helper_api() {
'permission_callback' => 'is_allowed',
)
);
register_rest_route(
'e2e-environment',
'/info',
array(
'methods' => 'GET',
'callback' => 'get_environment_info',
'permission_callback' => 'is_allowed',
)
);
}
add_action( 'rest_api_init', 'register_helper_api' );
@ -107,3 +117,22 @@ function api_update_option( WP_REST_Request $request ) {
function is_allowed() {
return current_user_can( 'manage_options' );
}
/**
* Get environment info
* @return WP_REST_Response
*/
function get_environment_info() {
$data['Core'] = get_bloginfo( 'version' );
$data['PHP'] = sprintf( '%s.%s', PHP_MAJOR_VERSION, PHP_MINOR_VERSION );
$all_plugins = get_plugins();
foreach ( $all_plugins as $plugin_file => $plugin_data ) {
if ( is_plugin_active( $plugin_file ) ) {
$data[ $plugin_data['Name'] ] = $plugin_data['Version'];
}
}
return new WP_REST_Response( $data, 200 );
}

View File

@ -3,6 +3,11 @@ require( 'dotenv' ).config( { path: __dirname + '/.env' } );
const testsRootPath = __dirname;
const testsResultsPath = `${ testsRootPath }/test-results`;
if ( ! process.env.BASE_URL ) {
console.log( 'BASE_URL is not set. Using default.' );
process.env.BASE_URL = 'http://localhost:8086';
}
const {
ALLURE_RESULTS_DIR,
BASE_URL,
@ -23,12 +28,6 @@ const reporter = [
`${ testsRootPath }/test-results/allure-results`,
detail: true,
suiteTitle: true,
environmentInfo: {
Node: process.version,
OS: process.platform,
WP: process.env.WP_VERSION,
CI: process.env.CI,
},
},
],
[
@ -37,6 +36,10 @@ const reporter = [
outputFile: `${ testsRootPath }/test-results/test-results-${ Date.now() }.json`,
},
],
[
`${ testsRootPath }/reporters/environment-reporter.js`,
{ outputFolder: `${ testsRootPath }/test-results/allure-results` },
],
];
if ( process.env.CI ) {
@ -71,7 +74,7 @@ const config = {
reporter,
maxFailures: E2E_MAX_FAILURES ? Number( E2E_MAX_FAILURES ) : 0,
use: {
baseURL: BASE_URL ?? 'http://localhost:8086',
baseURL: BASE_URL,
screenshot: { mode: 'only-on-failure', fullPage: true },
stateDir: `${ testsRootPath }/.state/`,
trace:

View File

@ -0,0 +1,64 @@
require( '@playwright/test/reporter' );
const { request } = require( '@playwright/test' );
const fs = require( 'fs' );
const path = require( 'path' );
const { admin } = require( '../test-data/data' );
class EnvironmentReporter {
constructor( options ) {
this.reportOptions = options;
}
async onEnd() {
console.log( 'Getting environment details' );
const { outputFolder } = this.reportOptions;
if ( ! outputFolder ) {
console.log( 'No output folder specified!' );
return;
}
const { BASE_URL, CI } = process.env;
let environmentData = '';
if ( CI ) {
environmentData += `CI=${ CI }`;
}
try {
const wpApi = await request.newContext( {
baseURL: BASE_URL,
extraHTTPHeaders: {
Authorization: `Basic ${ Buffer.from(
`${ admin.username }:${ admin.password }`
).toString( 'base64' ) }`,
},
} );
const info = await wpApi.get( `/wp-json/e2e-environment/info` );
if ( info.ok() ) {
const data = await info.json();
for ( const [ key, value ] of Object.entries( data ) ) {
// We need to format the values to be compatible with the Java properties file format
environmentData += `\n${ key
.replace( / /g, '\\u0020' )
.replace( /:/g, '-' ) }=${ value }`;
}
}
} catch ( err ) {
console.error( `Error getting environment info: ${ err }` );
}
const filePath = path.resolve( outputFolder, 'environment.properties' );
try {
console.log( environmentData );
fs.writeFileSync( filePath, environmentData );
} catch ( err ) {
console.error( `Error writing environment.properties: ${ err }` );
}
}
}
module.exports = EnvironmentReporter;