From b8286e535beaa6dd82e7fe09227c481e2fb3dc8c Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Mon, 25 Aug 2025 20:12:05 -0400 Subject: [PATCH 1/7] Create a helper function for processing env vars This introduces `setup_runner_env_vars()` to process environment variables used to configure the test runner to avoid repeated code. --- cleanup.php | 22 ++++++++-------------- functions.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ prepare.php | 43 ++++++++++++++++++------------------------- report.php | 34 ++++++++++++++-------------------- test.php | 15 +++++---------- 5 files changed, 95 insertions(+), 69 deletions(-) diff --git a/cleanup.php b/cleanup.php index e584c6c..adad651 100644 --- a/cleanup.php +++ b/cleanup.php @@ -16,27 +16,21 @@ check_required_env(); /** - * Retrieves environment variables and sets defaults for test preparation. - * These variables are used to configure SSH connections, file paths, and - * executable commands needed for setting up the test environment. + * Ensure that all environment variables are present with default values. */ -$WPT_PREPARE_DIR = trim( getenv( 'WPT_PREPARE_DIR' ) ); -$WPT_SSH_CONNECT = trim( getenv( 'WPT_SSH_CONNECT' ) ); -$WPT_SSH_OPTIONS = trim( getenv( 'WPT_SSH_OPTIONS' ) ) ? : '-o StrictHostKeyChecking=no'; -$WPT_TEST_DIR = trim( getenv( 'WPT_TEST_DIR' ) ); -$WPT_RM_TEST_DIR_CMD = trim( getenv( 'WPT_RM_TEST_DIR_CMD' ) ) ? : 'rm -r ' . $WPT_TEST_DIR; +$runner_vars = setup_runner_env_vars(); /** * The directory path of the test preparation directory is assumed to be previously defined. - * For example: $WPT_PREPARE_DIR = '/path/to/your/preparation/dir'; + * For example: $runner_vars['WPT_PREPARE_DIR'] = '/path/to/your/preparation/dir'; * Clean up the preparation directory. * Forcefully deletes only the .git directory and the node_modules cache. * Afterward, the entire preparation directory is removed to ensure a clean state for the next test run. */ perform_operations( array( - 'rm -rf ' . escapeshellarg( $WPT_PREPARE_DIR . '/.git' ), - 'rm -rf ' . escapeshellarg( $WPT_PREPARE_DIR . '/node_modules/.cache' ), - 'rm -r ' . escapeshellarg( $WPT_PREPARE_DIR ), + 'rm -rf ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] . '/.git' ), + 'rm -rf ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] . '/node_modules/.cache' ), + 'rm -r ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ), ) ); /** @@ -46,8 +40,8 @@ * The cleanup operation is executed by the `perform_operations` function which takes an array * of shell commands as its input. */ -if ( ! empty( $WPT_SSH_CONNECT ) ) { +if ( ! empty( $runner_vars['WPT_SSH_CONNECT'] ) ) { perform_operations( array( - 'ssh ' . $WPT_SSH_OPTIONS . ' ' . escapeshellarg( $WPT_SSH_CONNECT ) . ' ' . escapeshellarg( $WPT_RM_TEST_DIR_CMD ), + 'ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' ' . escapeshellarg( $WPT_RM_TEST_DIR_CMD ), ) ); } diff --git a/functions.php b/functions.php index a9613aa..3cbd825 100644 --- a/functions.php +++ b/functions.php @@ -43,6 +43,56 @@ function check_required_env( $check_db = true ) { log_message( 'Environment variables pass checks.' ); } +/** + * Parses environment variables used to configure the test runner. + * + * @return array[] { + * Test runner configuration options. + * + * @type array ...$0 { + * An associative array of test runner configuration options. + * + * @type string $WPT_TEST_DIR Path to the directory where wordpress-develop is placed for testing + * after being prepared. + * @type string $WPT_PREPARE_DIR Path to the temporary directory where wordpress-develop is cloned + * and configured. + * @type string $WPT_SSH_CONNECT List of inner blocks. An array of arrays that + * have the same structure as this one. + * @type string $WPT_SSH_OPTIONS HTML from inside block comment delimiters. + * @type string $WPT_PHP_EXECUTABLE List of string fragments and null markers where + * inner blocks were found. + * @type string $WPT_RM_TEST_DIR_CMD Command for removing the test directory. + * @type string $WPT_REPORT_API_KEY API key for submitting test results. + * @type string $WPT_CERTIFICATE_VALIDATION List of string fragments and null markers where + * } + * } + */ +function setup_runner_env_vars() { + // Get test directory first as it's needed for the default rm command + $runner_configuration = array( + 'WPT_TEST_DIR' => trim( getenv( 'WPT_TEST_DIR' ) ), + ); + + return array_merge( + $runner_configuration, + array( + // Directory configuration + 'WPT_PREPARE_DIR' => trim( getenv( 'WPT_PREPARE_DIR' ) ), + // SSH connection configuration + 'WPT_SSH_CONNECT' => trim( getenv( 'WPT_SSH_CONNECT' ) ), + 'WPT_SSH_OPTIONS' => trim( getenv( 'WPT_SSH_OPTIONS' ) ) ?: '-o StrictHostKeyChecking=no', + // Test execution configuration + 'WPT_PHP_EXECUTABLE' => trim( getenv( 'WPT_PHP_EXECUTABLE' ) ) ?: 'php', + // Cleanup configuration + 'WPT_RM_TEST_DIR_CMD' => trim( getenv( 'WPT_RM_TEST_DIR_CMD' ) ) ?: 'rm -r ' . $runner_configuration['WPT_TEST_DIR'], + // Reporting configuration + 'WPT_REPORT_API_KEY' => trim( getenv( 'WPT_REPORT_API_KEY' ) ), + // Miscellaneous + 'WPT_CERTIFICATE_VALIDATION' => trim( getenv( 'WPT_CERTIFICATE_VALIDATION' ) ), + ) + ); +} + /** * Executes a series of shell commands provided in the operations array. Each operation is logged before execution. * If any command fails (indicated by a non-zero return code), an error message is displayed. This function is diff --git a/prepare.php b/prepare.php index bea7cf4..c69ff01 100644 --- a/prepare.php +++ b/prepare.php @@ -18,16 +18,9 @@ check_required_env(); /** - * Retrieves environment variables and sets defaults for test preparation. - * These variables are used to configure SSH connections, file paths, and - * executable commands needed for setting up the test environment. + * Ensure that optional environment variables are present with default values. */ -$WPT_PREPARE_DIR = trim( getenv( 'WPT_PREPARE_DIR' ) ); -$WPT_SSH_CONNECT = trim( getenv( 'WPT_SSH_CONNECT' ) ); -$WPT_SSH_OPTIONS = trim( getenv( 'WPT_SSH_OPTIONS' ) ) ? : '-o StrictHostKeyChecking=no'; -$WPT_TEST_DIR = trim( getenv( 'WPT_TEST_DIR' ) ); -$WPT_PHP_EXECUTABLE = trim( getenv( 'WPT_PHP_EXECUTABLE' ) ) ? : 'php'; -$WPT_CERTIFICATE_VALIDATION = trim( getenv( 'WPT_CERTIFICATE_VALIDATION' ) ); +$runner_vars = setup_runner_env_vars(); /** * Determines if the debug mode is enabled based on the 'WPT_DEBUG' environment variable. @@ -82,7 +75,7 @@ // If no SSH connection string is provided, add a local operation to the array. // If an SSH connection string is provided, add a remote operation to the array. // Execute the operations defined in the operations array. - if( empty( $WPT_SSH_CONNECT ) ) { + if( empty( $runner_vars['WPT_SSH_CONNECT'] ) ) { perform_operations( array( 'chmod 600 ~/.ssh/id_rsa', 'wp cli info' @@ -90,7 +83,7 @@ } else { perform_operations( array( 'chmod 600 ~/.ssh/id_rsa', - 'ssh -q ' . $WPT_SSH_OPTIONS . ' ' . escapeshellarg( $WPT_SSH_CONNECT ) . ' wp cli info' + 'ssh -q ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' wp cli info' ) ); } @@ -101,7 +94,7 @@ * Useful for local environments */ $certificate_validation = ''; -if( ! $WPT_CERTIFICATE_VALIDATION ) { +if( ! $runner_vars['WPT_CERTIFICATE_VALIDATION'] ) { $certificate_validation .= ' --no-check-certificate'; } @@ -113,14 +106,14 @@ perform_operations( array( // Create the preparation directory if it doesn't exist. The '-p' flag creates intermediate directories as required. - 'mkdir -p ' . escapeshellarg( $WPT_PREPARE_DIR ), + 'mkdir -p ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ), // Clone the WordPress develop repository from GitHub into the preparation directory. // The '--depth=1' flag creates a shallow clone with a history truncated to the last commit. - 'git clone --depth=1 https://github.com/WordPress/wordpress-develop.git ' . escapeshellarg( $WPT_PREPARE_DIR ), + 'git clone --depth=1 https://github.com/WordPress/wordpress-develop.git ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ), // Change directory to the preparation directory, install npm dependencies, and build the project. - 'cd ' . escapeshellarg( $WPT_PREPARE_DIR ) . '; npm install && npm run build' + 'cd ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . '; npm install && npm run build' ) ); @@ -132,7 +125,7 @@ * This file contains template placeholders that need to be replaced with actual values * from environment variables to configure the WordPress test environment. */ -$contents = file_get_contents( $WPT_PREPARE_DIR . '/wp-tests-config-sample.php' ); +$contents = file_get_contents( $runner_vars['WPT_PREPARE_DIR'] . '/wp-tests-config-sample.php' ); /** * Prepares a script to log system information relevant to the testing environment. @@ -240,7 +233,7 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve $system_logger = $logger_replace_string . $system_logger; // Define a string that will set the 'WP_PHP_BINARY' constant to the path of the PHP executable. -$php_binary_string = 'define( \'WP_PHP_BINARY\', \''. $WPT_PHP_EXECUTABLE . '\' );'; +$php_binary_string = 'define( \'WP_PHP_BINARY\', \''. $runner_vars['WPT_PHP_EXECUTABLE'] . '\' );'; /** * An associative array mapping configuration file placeholders to environment-specific values. @@ -261,22 +254,22 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve $contents = str_replace( array_keys( $search_replace ), array_values( $search_replace ), $contents ); // Write the modified content to the wp-tests-config.php file, which will be used by the test suite. -file_put_contents( $WPT_PREPARE_DIR . '/wp-tests-config.php', $contents ); +file_put_contents( $runner_vars['WPT_PREPARE_DIR'] . '/wp-tests-config.php', $contents ); /** * Determines the PHP version of the test environment to ensure the correct version of PHPUnit is installed. * It constructs a command that prints out the PHP version in a format compatible with PHPUnit's version requirements. */ -$php_version_cmd = $WPT_PHP_EXECUTABLE . " -r \"print PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION;\""; +$php_version_cmd = $runner_vars['WPT_PHP_EXECUTABLE'] . " -r \"print PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION;\""; /** * If an SSH connection string is provided, the command to determine the PHP version is modified * to execute remotely over SSH. This is required if the test environment is not the local machine. */ -if ( ! empty( $WPT_SSH_CONNECT ) ) { +if ( ! empty( $runner_vars['WPT_SSH_CONNECT'] ) ) { // The PHP version check command is prefixed with the SSH command, including SSH options, // and the connection string, ensuring the command is executed on the remote machine. - $php_version_cmd = 'ssh ' . $WPT_SSH_OPTIONS . ' ' . escapeshellarg( $WPT_SSH_CONNECT ) . ' ' . escapeshellarg( $php_version_cmd ); + $php_version_cmd = 'ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' ' . escapeshellarg( $php_version_cmd ); } // Initialize return value variable for the exec function call. @@ -313,7 +306,7 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve */ // Check if Composer is installed and available in the PATH. -$composer_cmd = 'cd ' . escapeshellarg( $WPT_PREPARE_DIR ) . ' && '; +$composer_cmd = 'cd ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . ' && '; $retval = 0; $composer_path = escapeshellarg( system( 'which composer', $retval ) ); @@ -328,7 +321,7 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve log_message( 'Local Composer not found. Downloading latest stable ...' ); perform_operations( array( - 'wget -O ' . escapeshellarg( $WPT_PREPARE_DIR . '/composer.phar' ) . ' https://getcomposer.org/composer-stable.phar', + 'wget -O ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] . '/composer.phar' ) . ' https://getcomposer.org/composer-stable.phar', ) ); // Update the command to use the downloaded Composer phar file. @@ -346,7 +339,7 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve * The -r option for rsync enables recursive copying to handle directory structures. * Additional rsync options may be included for more verbose output if debugging is enabled. */ -if ( ! empty( $WPT_SSH_CONNECT ) ) { +if ( ! empty( $runner_vars['WPT_SSH_CONNECT'] ) ) { // Initialize rsync options with recursive copying. $rsync_options = '-r'; @@ -359,7 +352,7 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve // This operation synchronizes the test environment with the prepared files, excluding version control directories // and other non-essential files for test execution. perform_operations( array( - '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 ), + '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'] ), ) ); } diff --git a/report.php b/report.php index b690f76..998deca 100644 --- a/report.php +++ b/report.php @@ -18,15 +18,9 @@ check_required_env( false ); /** - * Retrieves environment variables and sets defaults for test preparation. - * These variables are used to configure SSH connections, file paths, and - * executable commands needed for setting up the test environment. + * Ensure that optional environment variables are present with default values. */ -$WPT_SSH_CONNECT = trim( getenv( 'WPT_SSH_CONNECT' ) ); -$WPT_TEST_DIR = trim( getenv( 'WPT_TEST_DIR' ) ); -$WPT_PREPARE_DIR = trim( getenv( 'WPT_PREPARE_DIR' ) ); -$WPT_SSH_OPTIONS = trim( getenv( 'WPT_SSH_OPTIONS' ) ); -$WPT_REPORT_API_KEY = trim( getenv( 'WPT_REPORT_API_KEY' ) ); +$runner_vars = setup_runner_env_vars(); /** * Determines if the debug mode is enabled based on the 'WPT_DEBUG' environment variable. @@ -57,7 +51,7 @@ * and extracts the SVN revision number using a combination of grep and cut commands. */ log_message('Getting SVN Revision'); -$rev = exec('git --git-dir=' . escapeshellarg( $WPT_PREPARE_DIR ) . '/.git log -1 --pretty=%B | grep "git-svn-id:" | cut -d " " -f 2 | cut -d "@" -f 2'); +$rev = exec('git --git-dir=' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . '/.git log -1 --pretty=%B | grep "git-svn-id:" | cut -d " " -f 2 | cut -d "@" -f 2'); /** * Retrieves the latest SVN commit message from the git repository log. @@ -66,7 +60,7 @@ * fetches the latest commit message, and trims any whitespace from the message. */ log_message('Getting SVN message'); -$message = trim( exec('git --git-dir=' . escapeshellarg( $WPT_PREPARE_DIR ) . '/.git log -1 --pretty=%B | head -1') ); +$message = trim( exec('git --git-dir=' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . '/.git log -1 --pretty=%B | head -1') ); /** * Prepares the file path for copying the junit.xml results. @@ -76,7 +70,7 @@ * safely used in shell commands. */ log_message('Copying junit.xml results'); -$junit_location = escapeshellarg( $WPT_TEST_DIR ) . '/tests/phpunit/build/logs/*'; +$junit_location = escapeshellarg( $runner_vars['WPT_TEST_DIR'] ) . '/tests/phpunit/build/logs/*'; /** * Modifies the junit.xml results file path for a remote location if an SSH connection is available. @@ -85,8 +79,8 @@ * command and options for accessing the remote file system. It concatenates SSH options with the * remote path to ensure that the junit.xml results can be accessed or copied over SSH. */ -if ( ! empty( $WPT_SSH_CONNECT ) ) { - $junit_location = '-e "ssh ' . $WPT_SSH_OPTIONS . '" ' . escapeshellarg( $WPT_SSH_CONNECT . ':' . $junit_location ); +if ( ! empty( $runner_vars['WPT_SSH_CONNECT'] ) ) { + $junit_location = '-e "ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . '" ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] . ':' . $junit_location ); } /** @@ -108,7 +102,7 @@ * then passed to the `perform_operations` function, which executes the command to synchronize * the junit.xml files from the source to the destination directory. */ -$junit_exec = 'rsync ' . $rsync_options . ' ' . $junit_location . ' ' . escapeshellarg( $WPT_PREPARE_DIR ); +$junit_exec = 'rsync ' . $rsync_options . ' ' . $junit_location . ' ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ); perform_operations( array( $junit_exec, ) ); @@ -121,7 +115,7 @@ * it for upload or to extract relevant test run information. */ log_message( 'Processing and uploading junit.xml' ); -$xml = file_get_contents( $WPT_PREPARE_DIR . '/junit.xml' ); +$xml = file_get_contents( $runner_vars['WPT_PREPARE_DIR'] . '/junit.xml' ); $results = process_junit_xml( $xml ); /** @@ -132,9 +126,9 @@ * are generated by calling a function that retrieves these details, then encoded into JSON format. */ $env = ''; -if ( file_exists( $WPT_PREPARE_DIR . '/env.json' ) ) { - $env = file_get_contents( $WPT_PREPARE_DIR . '/env.json' ); -} elseif ( $WPT_PREPARE_DIR === $WPT_TEST_DIR ) { +if ( file_exists( $runner_vars['WPT_PREPARE_DIR'] . '/env.json' ) ) { + $env = file_get_contents( $runner_vars['WPT_PREPARE_DIR'] . '/env.json' ); +} elseif ( $runner_vars['WPT_PREPARE_DIR'] === $runner_vars['WPT_TEST_DIR'] ) { $env = json_encode( get_env_details(), JSON_PRETTY_PRINT ); } @@ -146,10 +140,10 @@ * message is logged along with the HTTP status. If no API key is provided, it logs the test results * and environment details locally. */ -if( ! empty( $WPT_REPORT_API_KEY ) ) { +if( ! empty( $runner_vars['WPT_REPORT_API_KEY'] ) ) { // Upload the results and capture the HTTP status and response body - list( $http_status, $response_body ) = upload_results( $results, $rev, $message, $env, $WPT_REPORT_API_KEY ); + list( $http_status, $response_body ) = upload_results( $results, $rev, $message, $env, $runner_vars['WPT_REPORT_API_KEY'] ); // Decode the JSON response body $response = json_decode( $response_body, true ); diff --git a/test.php b/test.php index 18b3f4f..c2b56c7 100644 --- a/test.php +++ b/test.php @@ -17,14 +17,9 @@ check_required_env(); /** - * Retrieves environment variables and sets defaults for test preparation. - * These variables are used to configure SSH connections, file paths, and - * executable commands needed for setting up the test environment. + * Ensure that all environment variables are present with default values. */ -$WPT_SSH_CONNECT = trim( getenv( 'WPT_SSH_CONNECT' ) ); -$WPT_TEST_DIR = trim( getenv( 'WPT_TEST_DIR' ) ); -$WPT_SSH_OPTIONS = trim( getenv( 'WPT_SSH_OPTIONS' ) ) ? : '-o StrictHostKeyChecking=no'; -$WPT_PHP_EXECUTABLE = trim( getenv( 'WPT_PHP_EXECUTABLE' ) ) ? : 'php'; +$runner_vars = setup_runner_env_vars(); // Uses the flavor (usually to test WordPress Multisite) $WPT_FLAVOR_INI = trim( getenv( 'WPT_FLAVOR' ) ); @@ -71,12 +66,12 @@ */ $WPT_PHPUNIT_CMD = trim( getenv( 'WPT_PHPUNIT_CMD' ) ); if( empty( $WPT_PHPUNIT_CMD ) ) { - $WPT_PHPUNIT_CMD = 'cd ' . escapeshellarg( $WPT_TEST_DIR ) . ' && ' . $WPT_PHP_EXECUTABLE . ' ./vendor/phpunit/phpunit/phpunit --dont-report-useless-tests' . $WPT_FLAVOR_TXT . $WPT_EXTRATESTS_TXT; + $WPT_PHPUNIT_CMD = 'cd ' . escapeshellarg( $runner_vars['WPT_TEST_DIR'] ) . ' && ' . $runner_vars['WPT_PHP_EXECUTABLE'] . ' ./vendor/phpunit/phpunit/phpunit --dont-report-useless-tests' . $WPT_FLAVOR_TXT . $WPT_EXTRATESTS_TXT; } // If an SSH connection string is provided, prepend the SSH command to the PHPUnit execution command. -if ( ! empty( $WPT_SSH_CONNECT ) ) { - $WPT_PHPUNIT_CMD = 'ssh ' . $WPT_SSH_OPTIONS . ' ' . escapeshellarg( $WPT_SSH_CONNECT ) . ' ' . escapeshellarg( $WPT_PHPUNIT_CMD ); +if ( ! empty( $runner_vars['WPT_SSH_CONNECT'] ) ) { + $WPT_PHPUNIT_CMD = 'ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' ' . escapeshellarg( $WPT_PHPUNIT_CMD ); } // Execute the PHPUnit command. From 654066811dffa378b9cc3fab8b26eaff39dae028 Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Mon, 25 Aug 2025 20:32:40 -0400 Subject: [PATCH 2/7] Detect debug mode for all scripts --- functions.php | 17 +++++++++++++++++ prepare.php | 23 +---------------------- report.php | 23 +---------------------- 3 files changed, 19 insertions(+), 44 deletions(-) diff --git a/functions.php b/functions.php index 3cbd825..db279bc 100644 --- a/functions.php +++ b/functions.php @@ -54,6 +54,7 @@ function check_required_env( $check_db = true ) { * * @type string $WPT_TEST_DIR Path to the directory where wordpress-develop is placed for testing * after being prepared. + * @type string $WPT_DEBUG_MODE Whether debug mode is enabled. Default false. * @type string $WPT_PREPARE_DIR Path to the temporary directory where wordpress-develop is cloned * and configured. * @type string $WPT_SSH_CONNECT List of inner blocks. An array of arrays that @@ -73,6 +74,22 @@ function setup_runner_env_vars() { 'WPT_TEST_DIR' => trim( getenv( 'WPT_TEST_DIR' ) ), ); + $WPT_DEBUG_INI = getenv( 'WPT_DEBUG' ); + switch( $WPT_DEBUG_INI ) { + case 0: + case 'false': + $runner_configuration['WPT_DEBUG'] = false; + break; + case 1: + case 'true': + case 'verbose': + $runner_configuration['WPT_DEBUG'] = 'verbose'; + break; + default: + $runner_configuration['WPT_DEBUG'] = false; + break; + } + return array_merge( $runner_configuration, array( diff --git a/prepare.php b/prepare.php index c69ff01..a263b9f 100644 --- a/prepare.php +++ b/prepare.php @@ -22,27 +22,6 @@ */ $runner_vars = setup_runner_env_vars(); -/** - * Determines if the debug mode is enabled based on the 'WPT_DEBUG' environment variable. - * The debug mode can affect error reporting and other debug-related settings. - */ -$WPT_DEBUG_INI = getenv( 'WPT_DEBUG' ); -switch( $WPT_DEBUG_INI ) { - case 0: - case 'false': - $WPT_DEBUG = false; - break; - case 1: - case 'true': - case 'verbose': - $WPT_DEBUG = 'verbose'; - break; - default: - $WPT_DEBUG = false; - break; -} -unset( $WPT_DEBUG_INI ); - /** * Sets up the SSH private key for use in the test environment if provided. * The private key is expected to be in base64-encoded form in the environment variable 'WPT_SSH_PRIVATE_KEY_BASE64'. @@ -344,7 +323,7 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve $rsync_options = '-r'; // If debug mode is set to verbose, append 'v' to rsync options for verbose output. - if ( 'verbose' === $WPT_DEBUG ) { + if ( 'verbose' === $runner_vars['WPT_DEBUG'] ) { $rsync_options = $rsync_options . 'v'; } diff --git a/report.php b/report.php index 998deca..d14ff99 100644 --- a/report.php +++ b/report.php @@ -22,27 +22,6 @@ */ $runner_vars = setup_runner_env_vars(); -/** - * Determines if the debug mode is enabled based on the 'WPT_DEBUG' environment variable. - * The debug mode can affect error reporting and other debug-related settings. - */ -$WPT_DEBUG_INI = getenv( 'WPT_DEBUG' ); -switch( $WPT_DEBUG_INI ) { - case 0: - case 'false': - $WPT_DEBUG = false; - break; - case 1: - case 'true': - case 'verbose': - $WPT_DEBUG = 'verbose'; - break; - default: - $WPT_DEBUG = false; - break; -} -unset( $WPT_DEBUG_INI ); - /** * Retrieves the SVN revision number from the git repository log. * Logs a message indicating the start of the SVN revision retrieval process. @@ -91,7 +70,7 @@ */ $rsync_options = '-r'; -if ( 'verbose' === $WPT_DEBUG ) { +if ( 'verbose' === $runner_vars['WPT_DEBUG'] ) { $rsync_options = $rsync_options . 'v'; } From 8ae1f97f70c504251f0ecd15a0a75be4a3d9eefd Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Mon, 25 Aug 2025 20:46:40 -0400 Subject: [PATCH 3/7] Simplify debug mode This switches to handling debug mode as a boolean matching truthy values, which simplifies things. --- .env.default | 7 +++++-- functions.php | 19 ++----------------- prepare.php | 2 +- report.php | 2 +- 4 files changed, 9 insertions(+), 21 deletions(-) diff --git a/.env.default b/.env.default index 84de84c..4266120 100644 --- a/.env.default +++ b/.env.default @@ -57,8 +57,11 @@ export WPT_SSH_OPTIONS= # SSH private key, base64 encoded. export WPT_SSH_PRIVATE_KEY_BASE64= -# Output logging -# Use 'verbose' to increase verbosity +# Whether to enable debug Mode. +# +# Enabling debug mode will output verbose logging and details about each part of the test runner. +# +# Any truthy value will enable debug mode. export WPT_DEBUG= # Certificate validation diff --git a/functions.php b/functions.php index db279bc..d9abbf3 100644 --- a/functions.php +++ b/functions.php @@ -54,7 +54,6 @@ function check_required_env( $check_db = true ) { * * @type string $WPT_TEST_DIR Path to the directory where wordpress-develop is placed for testing * after being prepared. - * @type string $WPT_DEBUG_MODE Whether debug mode is enabled. Default false. * @type string $WPT_PREPARE_DIR Path to the temporary directory where wordpress-develop is cloned * and configured. * @type string $WPT_SSH_CONNECT List of inner blocks. An array of arrays that @@ -65,6 +64,7 @@ function check_required_env( $check_db = true ) { * @type string $WPT_RM_TEST_DIR_CMD Command for removing the test directory. * @type string $WPT_REPORT_API_KEY API key for submitting test results. * @type string $WPT_CERTIFICATE_VALIDATION List of string fragments and null markers where + * @type bool $WPT_DEBUG_MODE Whether debug mode is enabled. Default false. * } * } */ @@ -74,22 +74,6 @@ function setup_runner_env_vars() { 'WPT_TEST_DIR' => trim( getenv( 'WPT_TEST_DIR' ) ), ); - $WPT_DEBUG_INI = getenv( 'WPT_DEBUG' ); - switch( $WPT_DEBUG_INI ) { - case 0: - case 'false': - $runner_configuration['WPT_DEBUG'] = false; - break; - case 1: - case 'true': - case 'verbose': - $runner_configuration['WPT_DEBUG'] = 'verbose'; - break; - default: - $runner_configuration['WPT_DEBUG'] = false; - break; - } - return array_merge( $runner_configuration, array( @@ -106,6 +90,7 @@ function setup_runner_env_vars() { 'WPT_REPORT_API_KEY' => trim( getenv( 'WPT_REPORT_API_KEY' ) ), // Miscellaneous 'WPT_CERTIFICATE_VALIDATION' => trim( getenv( 'WPT_CERTIFICATE_VALIDATION' ) ), + 'WPT_DEBUG' => (bool) getenv( 'WPT_DEBUG' ), ) ); } diff --git a/prepare.php b/prepare.php index a263b9f..4ceae13 100644 --- a/prepare.php +++ b/prepare.php @@ -323,7 +323,7 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve $rsync_options = '-r'; // If debug mode is set to verbose, append 'v' to rsync options for verbose output. - if ( 'verbose' === $runner_vars['WPT_DEBUG'] ) { + if ( $runner_vars['WPT_DEBUG'] ) { $rsync_options = $rsync_options . 'v'; } diff --git a/report.php b/report.php index d14ff99..1a15d6c 100644 --- a/report.php +++ b/report.php @@ -70,7 +70,7 @@ */ $rsync_options = '-r'; -if ( 'verbose' === $runner_vars['WPT_DEBUG'] ) { +if ( $runner_vars['WPT_DEBUG'] ) { $rsync_options = $rsync_options . 'v'; } From f87ee39126f77e458790fe8dfcc9d88c1b6e391f Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Mon, 25 Aug 2025 20:47:18 -0400 Subject: [PATCH 4/7] Handle `WPT_CERTIFICATE_VALIDATION` as a bool --- functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions.php b/functions.php index d9abbf3..575856c 100644 --- a/functions.php +++ b/functions.php @@ -63,7 +63,7 @@ function check_required_env( $check_db = true ) { * inner blocks were found. * @type string $WPT_RM_TEST_DIR_CMD Command for removing the test directory. * @type string $WPT_REPORT_API_KEY API key for submitting test results. - * @type string $WPT_CERTIFICATE_VALIDATION List of string fragments and null markers where + * @type bool $WPT_CERTIFICATE_VALIDATION Whether to validate TLS certificates. Default true. * @type bool $WPT_DEBUG_MODE Whether debug mode is enabled. Default false. * } * } @@ -89,7 +89,7 @@ function setup_runner_env_vars() { // Reporting configuration 'WPT_REPORT_API_KEY' => trim( getenv( 'WPT_REPORT_API_KEY' ) ), // Miscellaneous - 'WPT_CERTIFICATE_VALIDATION' => trim( getenv( 'WPT_CERTIFICATE_VALIDATION' ) ), + 'WPT_CERTIFICATE_VALIDATION' => (bool) trim( getenv( 'WPT_CERTIFICATE_VALIDATION' ) ), 'WPT_DEBUG' => (bool) getenv( 'WPT_DEBUG' ), ) ); From 565f26c71612686e791497aedf6bbe50e0222dc6 Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Mon, 25 Aug 2025 21:06:52 -0400 Subject: [PATCH 5/7] Ensure sane default values Co-Authored-By: Javier Casares --- cleanup.php | 2 +- functions.php | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cleanup.php b/cleanup.php index adad651..a0d8ad0 100644 --- a/cleanup.php +++ b/cleanup.php @@ -42,6 +42,6 @@ */ if ( ! empty( $runner_vars['WPT_SSH_CONNECT'] ) ) { perform_operations( array( - 'ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' ' . escapeshellarg( $WPT_RM_TEST_DIR_CMD ), + 'ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' ' . escapeshellarg( $runner_vars['WPT_RM_TEST_DIR_CMD'] ), ) ); } diff --git a/functions.php b/functions.php index 575856c..b7f06be 100644 --- a/functions.php +++ b/functions.php @@ -53,9 +53,9 @@ function check_required_env( $check_db = true ) { * An associative array of test runner configuration options. * * @type string $WPT_TEST_DIR Path to the directory where wordpress-develop is placed for testing - * after being prepared. + * after being prepared. Default '/tmp/wp-test-runner'. * @type string $WPT_PREPARE_DIR Path to the temporary directory where wordpress-develop is cloned - * and configured. + * and configured. Default '/tmp/wp-test-runner'. * @type string $WPT_SSH_CONNECT List of inner blocks. An array of arrays that * have the same structure as this one. * @type string $WPT_SSH_OPTIONS HTML from inside block comment delimiters. @@ -64,21 +64,21 @@ function check_required_env( $check_db = true ) { * @type string $WPT_RM_TEST_DIR_CMD Command for removing the test directory. * @type string $WPT_REPORT_API_KEY API key for submitting test results. * @type bool $WPT_CERTIFICATE_VALIDATION Whether to validate TLS certificates. Default true. - * @type bool $WPT_DEBUG_MODE Whether debug mode is enabled. Default false. + * @type bool $WPT_DEBUG_MODE Whether debug mode is enabled. * } * } */ function setup_runner_env_vars() { - // Get test directory first as it's needed for the default rm command + // Set the test directory first as it's needed for processing other variables. $runner_configuration = array( - 'WPT_TEST_DIR' => trim( getenv( 'WPT_TEST_DIR' ) ), + 'WPT_TEST_DIR' => trim( getenv( 'WPT_TEST_DIR' ) ) ?: '/tmp/wp-test-runner', ); return array_merge( $runner_configuration, array( // Directory configuration - 'WPT_PREPARE_DIR' => trim( getenv( 'WPT_PREPARE_DIR' ) ), + 'WPT_PREPARE_DIR' => trim( getenv( 'WPT_PREPARE_DIR' ) ) ?: '/tmp/wp-test-runner', // SSH connection configuration 'WPT_SSH_CONNECT' => trim( getenv( 'WPT_SSH_CONNECT' ) ), 'WPT_SSH_OPTIONS' => trim( getenv( 'WPT_SSH_OPTIONS' ) ) ?: '-o StrictHostKeyChecking=no', From 5d9d60cbab6330d79dfc5ed97c46e96472017cf1 Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Mon, 25 Aug 2025 23:30:29 -0400 Subject: [PATCH 6/7] Better handling for WPT_CERTIFICATE_VALIDATION. When this is not defined at all, assume a misconfiguration and verify certificates. --- functions.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/functions.php b/functions.php index b7f06be..88b005d 100644 --- a/functions.php +++ b/functions.php @@ -74,6 +74,16 @@ function setup_runner_env_vars() { 'WPT_TEST_DIR' => trim( getenv( 'WPT_TEST_DIR' ) ) ?: '/tmp/wp-test-runner', ); + /* + * When no value is provided for WPT_CERTIFICATE_VALIDATION, assume that the default of true (validate certificates) + * is desired. + */ + if ( false === getenv( 'WPT_CERTIFICATE_VALIDATION' ) ) { + $runner_configuration['WPT_CERTIFICATE_VALIDATION'] = true; + } else { + $runner_configuration['WPT_CERTIFICATE_VALIDATION'] = (bool) getenv( 'WPT_CERTIFICATE_VALIDATION' ); + } + return array_merge( $runner_configuration, array( @@ -89,7 +99,6 @@ function setup_runner_env_vars() { // Reporting configuration 'WPT_REPORT_API_KEY' => trim( getenv( 'WPT_REPORT_API_KEY' ) ), // Miscellaneous - 'WPT_CERTIFICATE_VALIDATION' => (bool) trim( getenv( 'WPT_CERTIFICATE_VALIDATION' ) ), 'WPT_DEBUG' => (bool) getenv( 'WPT_DEBUG' ), ) ); From 4b04a18d2706aeb4f37521570ee1ab002f15500c Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Tue, 26 Aug 2025 00:18:23 -0400 Subject: [PATCH 7/7] Adjust inline docs for `WPT_DEBUG` --- .env.default | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.env.default b/.env.default index 4266120..35677a7 100644 --- a/.env.default +++ b/.env.default @@ -61,7 +61,10 @@ export WPT_SSH_PRIVATE_KEY_BASE64= # # Enabling debug mode will output verbose logging and details about each part of the test runner. # -# Any truthy value will enable debug mode. +# 0 = Debug mode off +# 1 = Debug mode on +# +# Any other truthy value will also enable debug mode. export WPT_DEBUG= # Certificate validation