|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | /** |
4 | | - * Reports the test run results to WordPress.org |
| 4 | + * This script is responsible for reporting the results of the PHPUnit test runs to WordPress.org. |
| 5 | + * It gathers necessary information such as the SVN revision, test run messages, and the junit.xml |
| 6 | + * file containing the results. It then uploads these details using the WordPress.org API if an API |
| 7 | + * key is provided, or logs the results for later use. |
| 8 | + * |
| 9 | + * @link https://github.com/wordpress/phpunit-test-runner/ Original source repository |
| 10 | + * @package WordPress |
5 | 11 | */ |
6 | | - |
7 | 12 | require __DIR__ . '/functions.php'; |
8 | 13 |
|
9 | | -// Check required environment variables. |
| 14 | +/** |
| 15 | + * Check for the presence of required environment variables. |
| 16 | + * This function should be defined in functions.php and should throw an |
| 17 | + * exception or exit if any required variables are missing. |
| 18 | + */ |
10 | 19 | check_required_env( false ); |
11 | 20 |
|
12 | | -$WPT_SSH_CONNECT = getenv( 'WPT_SSH_CONNECT' ); |
13 | | -$WPT_TEST_DIR = getenv( 'WPT_TEST_DIR' ); |
14 | | -$WPT_PREPARE_DIR = getenv( 'WPT_PREPARE_DIR' ); |
15 | | -$WPT_SSH_OPTIONS = getenv( 'WPT_SSH_OPTIONS' ); |
16 | | -$WPT_REPORT_API_KEY = getenv( 'WPT_REPORT_API_KEY' ); |
17 | | -$WPT_DEBUG = getenv( 'WPT_DEBUG' ); |
| 21 | +/** |
| 22 | + * Retrieves environment variables and sets defaults for test preparation. |
| 23 | + * These variables are used to configure SSH connections, file paths, and |
| 24 | + * executable commands needed for setting up the test environment. |
| 25 | + */ |
| 26 | +$WPT_SSH_CONNECT = trim( getenv( 'WPT_SSH_CONNECT' ) ); |
| 27 | +$WPT_TEST_DIR = trim( getenv( 'WPT_TEST_DIR' ) ); |
| 28 | +$WPT_PREPARE_DIR = trim( getenv( 'WPT_PREPARE_DIR' ) ); |
| 29 | +$WPT_SSH_OPTIONS = trim( getenv( 'WPT_SSH_OPTIONS' ) ); |
| 30 | +$WPT_REPORT_API_KEY = trim( getenv( 'WPT_REPORT_API_KEY' ) ); |
| 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 | + $WPT_DEBUG = true; |
| 45 | + break; |
| 46 | + case 'verbose': |
| 47 | + $WPT_DEBUG = 'verbose'; |
| 48 | + break; |
| 49 | + default: |
| 50 | + $WPT_DEBUG = false; |
| 51 | + break; |
| 52 | +} |
| 53 | +unset( $WPT_DEBUG_INI ); |
18 | 54 |
|
| 55 | +/** |
| 56 | + * Retrieves the SVN revision number from the git repository log. |
| 57 | + * Logs a message indicating the start of the SVN revision retrieval process. |
| 58 | + * Executes a shell command that accesses the git directory specified by the |
| 59 | + * WPT_PREPARE_DIR environment variable, retrieves the latest commit message, |
| 60 | + * and extracts the SVN revision number using a combination of grep and cut commands. |
| 61 | + */ |
19 | 62 | log_message('Getting SVN Revision'); |
20 | 63 | $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'); |
21 | 64 |
|
| 65 | +/** |
| 66 | + * Retrieves the latest SVN commit message from the git repository log. |
| 67 | + * Logs a message to indicate the retrieval of the SVN commit message. Executes a shell command |
| 68 | + * that accesses the git directory specified by the WPT_PREPARE_DIR environment variable, |
| 69 | + * fetches the latest commit message, and trims any whitespace from the message. |
| 70 | + */ |
22 | 71 | log_message('Getting SVN message'); |
23 | 72 | $message = trim( exec('git --git-dir=' . escapeshellarg( $WPT_PREPARE_DIR ) . '/.git log -1 --pretty=%B | head -1') ); |
24 | 73 |
|
| 74 | +/** |
| 75 | + * Prepares the file path for copying the junit.xml results. |
| 76 | + * Logs a message indicating the start of the operation to copy junit.xml results. |
| 77 | + * Constructs the file path to the junit.xml file(s) located in the test directory, |
| 78 | + * making use of the WPT_TEST_DIR environment variable. The path is sanitized to be |
| 79 | + * safely used in shell commands. |
| 80 | + */ |
25 | 81 | log_message('Copying junit.xml results'); |
26 | 82 | $junit_location = escapeshellarg( $WPT_TEST_DIR ) . '/tests/phpunit/build/logs/*'; |
27 | 83 |
|
| 84 | +/** |
| 85 | + * Modifies the junit.xml results file path for a remote location if an SSH connection is available. |
| 86 | + * If the WPT_SSH_CONNECT environment variable is not empty, indicating that an SSH connection |
| 87 | + * is configured, this snippet adapts the junit_location variable to include the necessary SSH |
| 88 | + * command and options for accessing the remote file system. It concatenates SSH options with the |
| 89 | + * remote path to ensure that the junit.xml results can be accessed or copied over SSH. |
| 90 | + */ |
28 | 91 | if ( ! empty( $WPT_SSH_CONNECT ) ) { |
29 | 92 | $junit_location = '-e "ssh ' . $WPT_SSH_OPTIONS . '" ' . escapeshellarg( $WPT_SSH_CONNECT . ':' . $junit_location ); |
30 | 93 | } |
31 | 94 |
|
| 95 | +/** |
| 96 | + * Sets the options for the rsync command based on the debug mode. |
| 97 | + * Initializes the rsync options with the recursive flag. If the debug mode is set to 'verbose', |
| 98 | + * appends the 'v' flag to the rsync options to enable verbose output during the rsync operation, |
| 99 | + * providing more detailed information about the file transfer process. |
| 100 | + */ |
32 | 101 | $rsync_options = '-r'; |
33 | 102 |
|
34 | 103 | if ( 'verbose' === $WPT_DEBUG ) { |
35 | 104 | $rsync_options = $rsync_options . 'v'; |
36 | 105 | } |
37 | 106 |
|
| 107 | +/** |
| 108 | + * Constructs the rsync command for executing the synchronization of junit.xml files. |
| 109 | + * Concatenates the rsync command with the previously defined options and the source and |
| 110 | + * destination paths. The destination path is sanitized for shell execution. This command is |
| 111 | + * then passed to the `perform_operations` function, which executes the command to synchronize |
| 112 | + * the junit.xml files from the source to the destination directory. |
| 113 | + */ |
38 | 114 | $junit_exec = 'rsync ' . $rsync_options . ' ' . $junit_location . ' ' . escapeshellarg( $WPT_PREPARE_DIR ); |
39 | 115 | perform_operations( array( |
40 | 116 | $junit_exec, |
41 | 117 | ) ); |
42 | 118 |
|
| 119 | +/** |
| 120 | + * Processes and uploads the junit.xml file. |
| 121 | + * First, a log message is recorded to indicate the start of processing the junit.xml file. |
| 122 | + * Then, the contents of the junit.xml file are read from the prepared directory into a string. |
| 123 | + * This XML string is then passed to a function that processes the XML data, presumably to prepare |
| 124 | + * it for upload or to extract relevant test run information. |
| 125 | + */ |
43 | 126 | log_message( 'Processing and uploading junit.xml' ); |
44 | | - |
45 | 127 | $xml = file_get_contents( $WPT_PREPARE_DIR . '/junit.xml' ); |
46 | 128 | $results = process_junit_xml( $xml ); |
47 | 129 |
|
| 130 | +/** |
| 131 | + * Retrieves environment details from a JSON file or generates them if not available. |
| 132 | + * Initializes the environment details string. If an 'env.json' file exists in the prepared |
| 133 | + * directory, its contents are read into the environment details string. If the file doesn't |
| 134 | + * exist but the prepared directory is the same as the test directory, the environment details |
| 135 | + * are generated by calling a function that retrieves these details, then encoded into JSON format. |
| 136 | + */ |
48 | 137 | $env = ''; |
49 | 138 | if ( file_exists( $WPT_PREPARE_DIR . '/env.json' ) ) { |
50 | 139 | $env = file_get_contents( $WPT_PREPARE_DIR . '/env.json' ); |
51 | 140 | } elseif ( $WPT_PREPARE_DIR === $WPT_TEST_DIR ) { |
52 | 141 | $env = json_encode( get_env_details(), JSON_PRETTY_PRINT ); |
53 | 142 | } |
54 | 143 |
|
55 | | -list( $http_status, $response_body ) = upload_results( $results, $rev, $message, $env, $WPT_REPORT_API_KEY ); |
| 144 | +/** |
| 145 | + * Attempts to upload test results if an API key is available, otherwise logs the results locally. |
| 146 | + * Checks if an API key for reporting is present. If so, it attempts to upload the test results |
| 147 | + * using the `upload_results` function and processes the HTTP response. A success message is logged |
| 148 | + * if the upload is successful, indicated by a 20x HTTP status code. If the upload fails, an error |
| 149 | + * message is logged along with the HTTP status. If no API key is provided, it logs the test results |
| 150 | + * and environment details locally. |
| 151 | + */ |
| 152 | +if( ! empty( $WPT_REPORT_API_KEY ) ) { |
| 153 | + |
| 154 | + // Upload the results and capture the HTTP status and response body |
| 155 | + list( $http_status, $response_body ) = upload_results( $results, $rev, $message, $env, $WPT_REPORT_API_KEY ); |
| 156 | + |
| 157 | + // Decode the JSON response body |
| 158 | + $response = json_decode( $response_body, true ); |
| 159 | + if ( 20 == substr( $http_status, 0, 2 ) ) { |
| 160 | + |
| 161 | + // Construct and log a success message with a link if provided in the response |
| 162 | + $message = 'Results successfully uploaded'; |
| 163 | + $message .= isset( $response['link'] ) ? ': ' . $response['link'] : ''; |
| 164 | + log_message( $message ); |
| 165 | + |
| 166 | + } else { |
| 167 | + |
| 168 | + // Construct and log an error message with additional details if provided in the response |
| 169 | + $message = 'Error uploading results'; |
| 170 | + $message .= isset( $response['message'] ) ? ': ' . $response['message'] : ''; |
| 171 | + $message .= ' (HTTP status ' . (int) $http_status . ')'; |
| 172 | + error_message( $message ); |
| 173 | + |
| 174 | + } |
56 | 175 |
|
57 | | -$response = json_decode( $response_body, true ); |
58 | | -if ( 20 == substr( $http_status, 0, 2 ) ) { |
59 | | - $message = 'Results successfully uploaded'; |
60 | | - $message .= isset( $response['link'] ) ? ': ' . $response['link'] : ''; |
61 | | - log_message( $message ); |
62 | 176 | } else { |
63 | | - $message = 'Error uploading results'; |
64 | | - $message .= isset( $response['message'] ) ? ': ' . $response['message'] : ''; |
65 | | - $message .= ' (HTTP status ' . (int) $http_status . ')'; |
66 | | - error_message( $message ); |
| 177 | + |
| 178 | + // Log the test results and environment details locally if no API key is provided |
| 179 | + log_message( '[+] TEST RESULTS' . "\n\n" . $results. "\n\n" ); |
| 180 | + log_message( '[+] ENVIRONMENT' . "\n\n" . $env . "\n\n" ); |
| 181 | + |
67 | 182 | } |
0 commit comments