Skip to content

Commit 19e3068

Browse files
desrosjkittenkamalaCrixu
authored
Merge pull request #262 from WordPress/reduce-repitition
Create helper function for parsing runner configuration environment variables Co-authored-by: desrosj <desrosj@git.wordpress.org> Co-authored-by: kittenkamala <amykamala@git.wordpress.org> Co-authored-by: Crixu <crixu@git.wordpress.org>
2 parents a20ef4b + f6785ca commit 19e3068

File tree

6 files changed

+118
-116
lines changed

6 files changed

+118
-116
lines changed

.env.default

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,16 @@ export WPT_SSH_OPTIONS=""
5757
# SSH private key, base64 encoded.
5858
export WPT_SSH_PRIVATE_KEY_BASE64=""
5959

60-
# Output logging
61-
# Use 'verbose' to increase verbosity
62-
export WPT_DEBUG=""
60+
# Whether to enable debug Mode.
61+
#
62+
# Enabling debug mode will output verbose logging and details about each part
63+
# of the test runner.
64+
#
65+
# 0 = Debug mode off
66+
# 1 = Debug mode on
67+
#
68+
# Any other truthy value will also enable debug mode.
69+
export WPT_DEBUG=
6370

6471
# Certificate validation
6572
# Use 1 to validate, and 0 to not validate

cleanup.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,21 @@
1616
check_required_env();
1717

1818
/**
19-
* Retrieves environment variables and sets defaults for test preparation.
20-
* These variables are used to configure SSH connections, file paths, and
21-
* executable commands needed for setting up the test environment.
19+
* Ensure that all environment variables are present with default values.
2220
*/
23-
$WPT_PREPARE_DIR = trim( getenv( 'WPT_PREPARE_DIR' ) );
24-
$WPT_SSH_CONNECT = trim( getenv( 'WPT_SSH_CONNECT' ) );
25-
$WPT_SSH_OPTIONS = trim( getenv( 'WPT_SSH_OPTIONS' ) ) ? : '-o StrictHostKeyChecking=no';
26-
$WPT_TEST_DIR = trim( getenv( 'WPT_TEST_DIR' ) );
27-
$WPT_RM_TEST_DIR_CMD = trim( getenv( 'WPT_RM_TEST_DIR_CMD' ) ) ? : 'rm -r ' . $WPT_TEST_DIR;
21+
$runner_vars = setup_runner_env_vars();
2822

2923
/**
3024
* The directory path of the test preparation directory is assumed to be previously defined.
31-
* For example: $WPT_PREPARE_DIR = '/path/to/your/preparation/dir';
25+
* For example: $runner_vars['WPT_PREPARE_DIR'] = '/path/to/your/preparation/dir';
3226
* Clean up the preparation directory.
3327
* Forcefully deletes only the .git directory and the node_modules cache.
3428
* Afterward, the entire preparation directory is removed to ensure a clean state for the next test run.
3529
*/
3630
perform_operations( array(
37-
'rm -rf ' . escapeshellarg( $WPT_PREPARE_DIR . '/.git' ),
38-
'rm -rf ' . escapeshellarg( $WPT_PREPARE_DIR . '/node_modules/.cache' ),
39-
'rm -r ' . escapeshellarg( $WPT_PREPARE_DIR ),
31+
'rm -rf ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] . '/.git' ),
32+
'rm -rf ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] . '/node_modules/.cache' ),
33+
'rm -r ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ),
4034
) );
4135

4236
/**
@@ -46,8 +40,8 @@
4640
* The cleanup operation is executed by the `perform_operations` function which takes an array
4741
* of shell commands as its input.
4842
*/
49-
if ( ! empty( $WPT_SSH_CONNECT ) ) {
43+
if ( ! empty( $runner_vars['WPT_SSH_CONNECT'] ) ) {
5044
perform_operations( array(
51-
'ssh ' . $WPT_SSH_OPTIONS . ' ' . escapeshellarg( $WPT_SSH_CONNECT ) . ' ' . escapeshellarg( $WPT_RM_TEST_DIR_CMD ),
45+
'ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' ' . escapeshellarg( $runner_vars['WPT_RM_TEST_DIR_CMD'] ),
5246
) );
5347
}

functions.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,67 @@ function check_required_env( $check_db = true ) {
4343
log_message( 'Environment variables pass checks.' );
4444
}
4545

46+
/**
47+
* Parses environment variables used to configure the test runner.
48+
*
49+
* @return array[] {
50+
* Test runner configuration options.
51+
*
52+
* @type array ...$0 {
53+
* An associative array of test runner configuration options.
54+
*
55+
* @type string $WPT_TEST_DIR Path to the directory where wordpress-develop is placed for testing
56+
* after being prepared. Default '/tmp/wp-test-runner'.
57+
* @type string $WPT_PREPARE_DIR Path to the temporary directory where wordpress-develop is cloned
58+
* and configured. Default '/tmp/wp-test-runner'.
59+
* @type string $WPT_SSH_CONNECT List of inner blocks. An array of arrays that
60+
* have the same structure as this one.
61+
* @type string $WPT_SSH_OPTIONS HTML from inside block comment delimiters.
62+
* @type string $WPT_PHP_EXECUTABLE List of string fragments and null markers where
63+
* inner blocks were found.
64+
* @type string $WPT_RM_TEST_DIR_CMD Command for removing the test directory.
65+
* @type string $WPT_REPORT_API_KEY API key for submitting test results.
66+
* @type bool $WPT_CERTIFICATE_VALIDATION Whether to validate TLS certificates. Default true.
67+
* @type bool $WPT_DEBUG_MODE Whether debug mode is enabled.
68+
* }
69+
* }
70+
*/
71+
function setup_runner_env_vars() {
72+
// Set the test directory first as it's needed for processing other variables.
73+
$runner_configuration = array(
74+
'WPT_TEST_DIR' => trim( getenv( 'WPT_TEST_DIR' ) ) ?: '/tmp/wp-test-runner',
75+
);
76+
77+
/*
78+
* When no value is provided for WPT_CERTIFICATE_VALIDATION, assume that the default of true (validate certificates)
79+
* is desired.
80+
*/
81+
if ( false === getenv( 'WPT_CERTIFICATE_VALIDATION' ) ) {
82+
$runner_configuration['WPT_CERTIFICATE_VALIDATION'] = true;
83+
} else {
84+
$runner_configuration['WPT_CERTIFICATE_VALIDATION'] = (bool) getenv( 'WPT_CERTIFICATE_VALIDATION' );
85+
}
86+
87+
return array_merge(
88+
$runner_configuration,
89+
array(
90+
// Directory configuration
91+
'WPT_PREPARE_DIR' => trim( getenv( 'WPT_PREPARE_DIR' ) ) ?: '/tmp/wp-test-runner',
92+
// SSH connection configuration
93+
'WPT_SSH_CONNECT' => trim( getenv( 'WPT_SSH_CONNECT' ) ),
94+
'WPT_SSH_OPTIONS' => trim( getenv( 'WPT_SSH_OPTIONS' ) ) ?: '-o StrictHostKeyChecking=no',
95+
// Test execution configuration
96+
'WPT_PHP_EXECUTABLE' => trim( getenv( 'WPT_PHP_EXECUTABLE' ) ) ?: 'php',
97+
// Cleanup configuration
98+
'WPT_RM_TEST_DIR_CMD' => trim( getenv( 'WPT_RM_TEST_DIR_CMD' ) ) ?: 'rm -r ' . $runner_configuration['WPT_TEST_DIR'],
99+
// Reporting configuration
100+
'WPT_REPORT_API_KEY' => trim( getenv( 'WPT_REPORT_API_KEY' ) ),
101+
// Miscellaneous
102+
'WPT_DEBUG' => (bool) getenv( 'WPT_DEBUG' ),
103+
)
104+
);
105+
}
106+
46107
/**
47108
* Executes a series of shell commands provided in the operations array. Each operation is logged before execution.
48109
* If any command fails (indicated by a non-zero return code), an error message is displayed. This function is

prepare.php

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,9 @@
1818
check_required_env();
1919

2020
/**
21-
* Retrieves environment variables and sets defaults for test preparation.
22-
* These variables are used to configure SSH connections, file paths, and
23-
* executable commands needed for setting up the test environment.
21+
* Ensure that optional environment variables are present with default values.
2422
*/
25-
$WPT_PREPARE_DIR = trim( getenv( 'WPT_PREPARE_DIR' ) );
26-
$WPT_SSH_CONNECT = trim( getenv( 'WPT_SSH_CONNECT' ) );
27-
$WPT_SSH_OPTIONS = trim( getenv( 'WPT_SSH_OPTIONS' ) ) ? : '-o StrictHostKeyChecking=no';
28-
$WPT_TEST_DIR = trim( getenv( 'WPT_TEST_DIR' ) );
29-
$WPT_PHP_EXECUTABLE = trim( getenv( 'WPT_PHP_EXECUTABLE' ) ) ? : 'php';
30-
$WPT_CERTIFICATE_VALIDATION = trim( getenv( 'WPT_CERTIFICATE_VALIDATION' ) );
31-
32-
/**
33-
* Determines if the debug mode is enabled based on the 'WPT_DEBUG' environment variable.
34-
* The debug mode can affect error reporting and other debug-related settings.
35-
*/
36-
$WPT_DEBUG_INI = getenv( 'WPT_DEBUG' );
37-
switch( $WPT_DEBUG_INI ) {
38-
case 0:
39-
case 'false':
40-
$WPT_DEBUG = false;
41-
break;
42-
case 1:
43-
case 'true':
44-
case 'verbose':
45-
$WPT_DEBUG = 'verbose';
46-
break;
47-
default:
48-
$WPT_DEBUG = false;
49-
break;
50-
}
51-
unset( $WPT_DEBUG_INI );
23+
$runner_vars = setup_runner_env_vars();
5224

5325
/**
5426
* Sets up the SSH private key for use in the test environment if provided.
@@ -82,15 +54,15 @@
8254
// If no SSH connection string is provided, add a local operation to the array.
8355
// If an SSH connection string is provided, add a remote operation to the array.
8456
// Execute the operations defined in the operations array.
85-
if( empty( $WPT_SSH_CONNECT ) ) {
57+
if( empty( $runner_vars['WPT_SSH_CONNECT'] ) ) {
8658
perform_operations( array(
8759
'chmod 600 ~/.ssh/id_rsa',
8860
'wp cli info'
8961
) );
9062
} else {
9163
perform_operations( array(
9264
'chmod 600 ~/.ssh/id_rsa',
93-
'ssh -q ' . $WPT_SSH_OPTIONS . ' ' . escapeshellarg( $WPT_SSH_CONNECT ) . ' wp cli info'
65+
'ssh -q ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' wp cli info'
9466
) );
9567
}
9668

@@ -101,7 +73,7 @@
10173
* Useful for local environments
10274
*/
10375
$certificate_validation = '';
104-
if( ! $WPT_CERTIFICATE_VALIDATION ) {
76+
if( ! $runner_vars['WPT_CERTIFICATE_VALIDATION'] ) {
10577
$certificate_validation .= ' --no-check-certificate';
10678
}
10779

@@ -113,14 +85,14 @@
11385
perform_operations( array(
11486

11587
// Create the preparation directory if it doesn't exist. The '-p' flag creates intermediate directories as required.
116-
'mkdir -p ' . escapeshellarg( $WPT_PREPARE_DIR ),
88+
'mkdir -p ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ),
11789

11890
// Clone the WordPress develop repository from GitHub into the preparation directory.
11991
// The '--depth=1' flag creates a shallow clone with a history truncated to the last commit.
120-
'git clone --depth=1 https://github.com/WordPress/wordpress-develop.git ' . escapeshellarg( $WPT_PREPARE_DIR ),
92+
'git clone --depth=1 https://github.com/WordPress/wordpress-develop.git ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ),
12193

12294
// Change directory to the preparation directory, install npm dependencies, and build the project.
123-
'cd ' . escapeshellarg( $WPT_PREPARE_DIR ) . '; npm install && npm run build'
95+
'cd ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . '; npm install && npm run build'
12496

12597
) );
12698

@@ -132,7 +104,7 @@
132104
* This file contains template placeholders that need to be replaced with actual values
133105
* from environment variables to configure the WordPress test environment.
134106
*/
135-
$contents = file_get_contents( $WPT_PREPARE_DIR . '/wp-tests-config-sample.php' );
107+
$contents = file_get_contents( $runner_vars['WPT_PREPARE_DIR'] . '/wp-tests-config-sample.php' );
136108

137109
/**
138110
* Prepares a script to log system information relevant to the testing environment.
@@ -240,7 +212,7 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve
240212
$system_logger = $logger_replace_string . $system_logger;
241213

242214
// Define a string that will set the 'WP_PHP_BINARY' constant to the path of the PHP executable.
243-
$php_binary_string = 'define( \'WP_PHP_BINARY\', \''. $WPT_PHP_EXECUTABLE . '\' );';
215+
$php_binary_string = 'define( \'WP_PHP_BINARY\', \''. $runner_vars['WPT_PHP_EXECUTABLE'] . '\' );';
244216

245217
/**
246218
* An associative array mapping configuration file placeholders to environment-specific values.
@@ -261,22 +233,22 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve
261233
$contents = str_replace( array_keys( $search_replace ), array_values( $search_replace ), $contents );
262234

263235
// Write the modified content to the wp-tests-config.php file, which will be used by the test suite.
264-
file_put_contents( $WPT_PREPARE_DIR . '/wp-tests-config.php', $contents );
236+
file_put_contents( $runner_vars['WPT_PREPARE_DIR'] . '/wp-tests-config.php', $contents );
265237

266238
/**
267239
* Determines the PHP version of the test environment to ensure the correct version of PHPUnit is installed.
268240
* It constructs a command that prints out the PHP version in a format compatible with PHPUnit's version requirements.
269241
*/
270-
$php_version_cmd = $WPT_PHP_EXECUTABLE . " -r \"print PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION;\"";
242+
$php_version_cmd = $runner_vars['WPT_PHP_EXECUTABLE'] . " -r \"print PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION;\"";
271243

272244
/**
273245
* If an SSH connection string is provided, the command to determine the PHP version is modified
274246
* to execute remotely over SSH. This is required if the test environment is not the local machine.
275247
*/
276-
if ( ! empty( $WPT_SSH_CONNECT ) ) {
248+
if ( ! empty( $runner_vars['WPT_SSH_CONNECT'] ) ) {
277249
// The PHP version check command is prefixed with the SSH command, including SSH options,
278250
// and the connection string, ensuring the command is executed on the remote machine.
279-
$php_version_cmd = 'ssh ' . $WPT_SSH_OPTIONS . ' ' . escapeshellarg( $WPT_SSH_CONNECT ) . ' ' . escapeshellarg( $php_version_cmd );
251+
$php_version_cmd = 'ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' ' . escapeshellarg( $php_version_cmd );
280252
}
281253

282254
// Initialize return value variable for the exec function call.
@@ -313,7 +285,7 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve
313285
*/
314286

315287
// Check if Composer is installed and available in the PATH.
316-
$composer_cmd = 'cd ' . escapeshellarg( $WPT_PREPARE_DIR ) . ' && ';
288+
$composer_cmd = 'cd ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . ' && ';
317289
$retval = 0;
318290
$composer_path = escapeshellarg( system( 'which composer', $retval ) );
319291

@@ -328,7 +300,7 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve
328300
log_message( 'Local Composer not found. Downloading latest stable ...' );
329301

330302
perform_operations( array(
331-
'wget -O ' . escapeshellarg( $WPT_PREPARE_DIR . '/composer.phar' ) . ' https://getcomposer.org/composer-stable.phar',
303+
'wget -O ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] . '/composer.phar' ) . ' https://getcomposer.org/composer-stable.phar',
332304
) );
333305

334306
// Update the command to use the downloaded Composer phar file.
@@ -346,20 +318,20 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve
346318
* The -r option for rsync enables recursive copying to handle directory structures.
347319
* Additional rsync options may be included for more verbose output if debugging is enabled.
348320
*/
349-
if ( ! empty( $WPT_SSH_CONNECT ) ) {
321+
if ( ! empty( $runner_vars['WPT_SSH_CONNECT'] ) ) {
350322
// Initialize rsync options with recursive copying.
351323
$rsync_options = '-r';
352324

353325
// If debug mode is set to verbose, append 'v' to rsync options for verbose output.
354-
if ( 'verbose' === $WPT_DEBUG ) {
326+
if ( $runner_vars['WPT_DEBUG'] ) {
355327
$rsync_options = $rsync_options . 'v';
356328
}
357329

358330
// Perform the rsync operation with the configured options and exclude patterns.
359331
// This operation synchronizes the test environment with the prepared files, excluding version control directories
360332
// and other non-essential files for test execution.
361333
perform_operations( array(
362-
'rsync ' . $rsync_options . ' --exclude=".git/" --exclude="node_modules/" --exclude="composer.phar" -e "ssh ' . $WPT_SSH_OPTIONS . '" ' . escapeshellarg( trailingslashit( $WPT_PREPARE_DIR ) ) . ' ' . escapeshellarg( $WPT_SSH_CONNECT . ':' . $WPT_TEST_DIR ),
334+
'rsync ' . $rsync_options . ' --exclude=".git/" --exclude="node_modules/" --exclude="composer.phar" -e "ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . '" ' . escapeshellarg( trailingslashit( $runner_vars['WPT_PREPARE_DIR'] ) ) . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] . ':' . $runner_vars['WPT_TEST_DIR'] ),
363335
) );
364336
}
365337

0 commit comments

Comments
 (0)