Skip to content

Commit 0932c1c

Browse files
Few more documentation fixes.
This makes the following improvements: - Ensure each function, and file have a proper short description. - Use third-person singular verbs for function summaries. - Enforces the 80 character limit (120 when indentation is present) for DocBlocks. - Code should be self-documenting. Avoid explaining every step of the code. - Multi-line inline comments should start with `/*` not `/**`. Co-Authored-By: Javier Casares <[email protected]>
1 parent be0b307 commit 0932c1c

5 files changed

Lines changed: 241 additions & 251 deletions

File tree

cleanup.php

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,62 @@
11
<?php
22
/**
3-
* This script is responsible for cleaning up the test environment after a run
4-
* of the WordPress PHPUnit Test Runner. It ensures that temporary directories
5-
* and files created during the test process are properly deleted.
3+
* WordPress PHPUnit Test Runner: Cleanup script
4+
*
5+
* This script is responsible for cleaning up the test environment after the
6+
* Test Runner completes.
7+
*
8+
* All files and directories created by the test runner or the PHPUnit test
9+
* suite are removed.
610
*
711
* @link https://github.com/wordpress/phpunit-test-runner/ Original source repository
812
*
913
* @package WordPress
1014
*/
1115
require __DIR__ . '/functions.php';
1216

13-
/**
14-
* Check for the presence of required environment variables. This function
15-
* should be defined in functions.php and should throw an exception or exit if
16-
* any required variables are missing.
17+
/*
18+
* Check for the presence of required environment variables.
19+
*
20+
* This function should be defined in functions.php and should throw an
21+
* exception or exit if any required variables are missing.
1722
*/
1823
check_required_env();
1924

20-
/**
21-
* Retrieves environment variables and sets defaults for test preparation. These
22-
* variables are used to configure SSH connections, file paths, and executable
23-
* commands needed for setting up the test environment.
25+
/*
26+
* Retrieve environment variables falling back to defaults.
27+
*
28+
* These variables are used to configure SSH connections, file paths, and
29+
* executable commands needed for setting up the test environment.
2430
*/
2531
$WPT_PREPARE_DIR = trim( getenv( 'WPT_PREPARE_DIR' ) );
2632
$WPT_SSH_CONNECT = trim( getenv( 'WPT_SSH_CONNECT' ) );
2733
$WPT_SSH_OPTIONS = trim( getenv( 'WPT_SSH_OPTIONS' ) ) ? : '-o StrictHostKeyChecking=no';
2834
$WPT_TEST_DIR = trim( getenv( 'WPT_TEST_DIR' ) );
2935
$WPT_RM_TEST_DIR_CMD = trim( getenv( 'WPT_RM_TEST_DIR_CMD' ) ) ? : 'rm -r ' . $WPT_TEST_DIR;
3036

31-
/**
32-
* The directory path of the test preparation directory is assumed to be previously defined.
33-
* For example: $WPT_PREPARE_DIR = '/path/to/your/preparation/dir';
34-
* Clean up the preparation directory.
35-
* Forcefully deletes only the .git directory and the node_modules cache.
36-
* Afterward, the entire preparation directory is removed to ensure a clean state for the next test run.
37+
/*
38+
* Clean up the test preparation directory.
39+
*
40+
* This ensures a clean slate the next time the test runner is executed.
41+
*
42+
* `WPT_PREPARE_DIR` will exist so long as prepare.php ran correctly.
43+
*
44+
* The following actions are performed:
45+
* - Forcefully deletes only the .git directory and the node_modules cache.
46+
* - Forcefully remove the `node_modules/.cache` directory.
47+
* - Remove the entire preparation directory.
3748
*/
3849
perform_operations( array(
3950
'rm -rf ' . escapeshellarg( $WPT_PREPARE_DIR . '/.git' ),
4051
'rm -rf ' . escapeshellarg( $WPT_PREPARE_DIR . '/node_modules/.cache' ),
4152
'rm -r ' . escapeshellarg( $WPT_PREPARE_DIR ),
4253
) );
4354

44-
/**
45-
* Cleans up the test directory on a remote server.
46-
* This conditional block checks if an SSH connection string is provided and is not empty.
47-
* If a connection string is present, it triggers a cleanup operation on the remote environment.
48-
* The cleanup operation is executed by the `perform_operations` function which takes an array
49-
* Performs a series of operations to clean up the test environment.
50-
* This includes deleting specific directories and, if provided, cleaning up
51-
* remote directories via SSH.
55+
/*
56+
* Clean up the test directory on a remote server.
57+
*
58+
* This ensures a clean slate on the remote server the next time the test
59+
* runner is executed.
5260
*/
5361
if ( ! empty( $WPT_SSH_CONNECT ) ) {
5462
perform_operations( array(

functions.php

Lines changed: 71 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<?php
22
/**
3-
* Validates the presence of essential environment variables necessary for the application to run correctly.
4-
* Specifically checks for variables related to directories and database configuration. It also ensures that
5-
* the test and preparation directories are the same when running locally without SSH connection requirements.
3+
* Confirms the presence of required environment variables.
64
*
7-
* This function will issue error messages through `error_message()` for any missing environment variables
8-
* and logs a message upon successful validation of all required variables.
5+
* For the test runner to function correctly, a few requirements must be met:
6+
* - A database configuration must be provided using the documented environment
7+
* variables.
8+
* - The preparation and test directories must be the same when running
9+
* locally (not making use of an SSH connection).
910
*
10-
* @param bool $check_db Optional. Whether to include database-related environment variables in the check. Defaults to true.
11-
* If set to false, database variables (prefixed with 'WPT_DB_') are not checked.
11+
* @param bool $check_db Optional. Whether to confirm `WPT_DB_*` environment
12+
* variables are present. Default true.
1213
*
13-
* @return void This function does not return a value but will halt execution if any required environment variable is missing.
14+
* @return void This function does not return a value but will halt execution
15+
* if any required environment variable is missing.
1416
*
1517
* @uses getenv() to retrieve environment variable values.
1618
* @uses error_message() to display error messages for missing variables.
@@ -36,21 +38,21 @@ function check_required_env( $check_db = true ) {
3638
}
3739

3840
if ( empty( getenv( 'WPT_SSH_CONNECT' ) )
39-
&& getenv( 'WPT_TEST_DIR' ) !== getenv( 'WPT_PREPARE_DIR' ) ) {
41+
&& getenv( 'WPT_TEST_DIR' ) !== getenv( 'WPT_PREPARE_DIR' ) ) {
4042
error_message( 'WPT_TEST_DIR must be the same as WPT_PREPARE_DIR when running locally.' );
4143
}
4244

4345
log_message( 'Environment variables pass checks.' );
4446
}
45-
4647
/**
47-
* Executes a series of shell commands provided in the operations array. Each
48-
* operation is logged before execution. If any command fails (indicated by a
49-
* non-zero return code), an error message is displayed. This function is useful
50-
* for automating batch shell tasks within a PHP script, with error handling for
51-
* each operation.
48+
* Executes a set of shell commands.
49+
*
50+
* Each command is logged before being executed.
51+
*
52+
* When a non-zero return code is encountered, the error message is displayed
53+
* and the runner will fail.
5254
*
53-
* @param array $operations An array of shell commands (strings) to be executed.
55+
* @param array $operations A list of shell commands (strings) to execute.
5456
* Each command should be a valid shell command and properly escaped for safety.
5557
* The commands are executed in the order they appear in the array.
5658
*
@@ -83,15 +85,12 @@ function perform_operations( $operations ) {
8385
}
8486

8587
/**
86-
* Writes a message followed by a newline to the standard output (STDOUT). This
87-
* function is commonly used for logging purposes, providing feedback during
88-
* script execution, or debugging. The message is appended with the PHP
89-
* end-of-line constant (PHP_EOL) to ensure proper line breaks on different
90-
* operating systems.
88+
* Writes a message to the standard output (STDOUT).
9189
*
92-
* @param string $message The message to be logged. This should be a string, and
93-
* it will be output exactly as provided, followed by a system-specific newline
94-
* character.
90+
* The message is appended with PHP_EOL to ensure proper line breaks on
91+
* different operating systems.
92+
*
93+
* @param string $message The message to be logged.
9594
*
9695
* @return void This function does not return a value. It directly writes the
9796
* message to STDOUT, which is typically visible in the console or terminal
@@ -106,11 +105,13 @@ function log_message( $message ) {
106105
}
107106

108107
/**
109-
* Writes an error message prefixed with "Error: " to the standard error output
110-
* (STDERR) and terminates the script with a status code of 1. This function is
111-
* typically used to report errors during script execution. The message is
112-
* appended with the PHP end-of-line constant (PHP_EOL) to ensure it is properly
113-
* displayed on all operating systems.
108+
* Displays an error message and terminates the test runner execution.
109+
*
110+
* The error message is prefixed with "Error: " and appended with PHP_EOL
111+
* before being written to the standard output (STDOUT).
112+
*
113+
* After outputting the error message, the script will be terminated with a
114+
* status code of 1.
114115
*
115116
* @param string $message The error message to be logged. This string will be
116117
* output as provided, but prefixed with "Error: " to indicate its nature,
@@ -130,12 +131,11 @@ function error_message( $message ) {
130131
}
131132

132133
/**
133-
* Ensures a single trailing slash is present at the end of a given string. This
134-
* function first removes any existing trailing slashes from the input string to
135-
* avoid duplication and then appends a single slash. It's commonly used to
136-
* normalize file paths or URLs to ensure consistency in format, especially when
137-
* concatenating paths or performing file system operations that expect a
138-
* trailing slash.
134+
* Ensures a single trailing slash is present at the end of a given string.
135+
*
136+
* File system operations often expect a single trailing slash when referring
137+
* to directories or paths. This ensures that only one trailing slash is
138+
* present at the end of a given string.
139139
*
140140
* @param string $string The input string to which a trailing slash will be
141141
* added. This could be a file path, URL, or any other string that requires a
@@ -155,14 +155,16 @@ function trailingslashit( $string ) {
155155
}
156156

157157
/**
158-
* Parses JUnit XML formatted string to extract test results, focusing
159-
* specifically on test failures and errors.
160-
* The function converts the XML data into a structured JSON format that
161-
* summarizes the overall test outcomes, including the total number of tests,
162-
* failures, errors, and execution time. Only test suites and cases that contain
163-
* failures or errors are included in the final JSON output. This function is
164-
* useful for automated test result analysis, continuous integration reporting,
165-
* or any scenario where a quick summary of test failures and errors is needed.
158+
* Extracts test results from a JUnit XML string.
159+
*
160+
* This extracts the relevant information from the test results into a format
161+
* accepted and understood by the WordPress Test Reporter plugin.
162+
*
163+
* The data specifically extracted is:
164+
* - Total number of tests.
165+
* - Number of failures.
166+
* - Number of errors.
167+
* - Overall execution time.
166168
*
167169
* @param string $xml_string The JUnit XML data as a string. This should be
168170
* well-formed XML representing the results of test executions, typically
@@ -240,13 +242,13 @@ function process_junit_xml( $xml_string )
240242
}
241243

242244
/**
243-
* Submits test results along with associated metadata to a specified reporting
244-
* API. The function constructs a POST request containing the test results, SVN
245-
* revision, SVN message, environment data, and uses an API key for
246-
* authentication. The reporting API's URL is retrieved from an environment
247-
* variable; if not found, a default URL is used. This function is typically
248-
* used to automate the reporting of test outcomes to a centralized system for
249-
* analysis, tracking, and historical record-keeping.
245+
* Submits test results to a reporting API endpoint.
246+
*
247+
* This submits test results and related metadata to a site running the
248+
* WordPress Test Reporter plugin using cURL.
249+
*
250+
* Reports are always submitted to WordPress.org Unless the WPT_REPORT_URL
251+
* environment variable is set.
250252
*
251253
* @param string $results The test results in a processed format (e.g., JSON)
252254
* ready for submission to the reporting API.
@@ -314,30 +316,29 @@ function upload_results( $results, $rev, $message, $env, $api_key ) {
314316
}
315317

316318
/**
317-
* Collects and returns an array of key environment details relevant to the
318-
* application's context. This includes the PHP version, installed PHP modules
319-
* with their versions, system utilities like curl and OpenSSL versions, MySQL
320-
* version, and operating system details. This function is useful for diagnostic
321-
* purposes, ensuring compatibility, or for reporting system configurations in
322-
* debugging or error logs.
323-
* The function checks for the availability of specific PHP modules and system
324-
* utilities and captures their versions. It uses shell commands to retrieve
325-
* system information, which requires the PHP environment to have access to
326-
* these commands and appropriate permissions.
327-
*
328-
* @return array An associative array containing detailed environment information. The array includes:
329-
* - 'php_version': The current PHP version.
330-
* - 'php_modules': An associative array of selected PHP modules and their versions.
331-
* - 'system_utils': Versions of certain system utilities such as 'curl', 'imagemagick', 'graphicsmagick', and 'openssl'.
332-
* - 'mysql_version': The version of MySQL installed.
333-
* - 'os_name': The name of the operating system.
334-
* - 'os_version': The version of the operating system.
319+
Collects details about the testing environment.
320+
*
321+
* The versions of PHP, PHP modules, database software, and system utilities
322+
* can impact the results of the test suite. This gathers the relevant details
323+
* to include in test report submissions.
324+
*
325+
* @return array An associative array containing detailed environment
326+
* information. The array includes:
327+
* - 'php_version': The current PHP version.
328+
* - 'php_modules': An associative array of selected PHP modules and their versions.
329+
* - 'system_utils': Versions of certain system utilities such as 'curl', 'imagemagick',
330+
* 'graphicsmagick', and 'openssl'.
331+
* - 'mysql_version': The version of MySQL installed.
332+
* - 'os_name': The name of the operating system.
333+
* - 'os_version': The version of the operating system.
335334
*
336335
* @uses phpversion() to get the PHP version and module versions.
337336
*
338-
* @uses shell_exec() to execute system commands for retrieving MySQL version, OS details, and versions of utilities like curl and OpenSSL.
337+
* @uses shell_exec() to execute system commands for retrieving MySQL version,
338+
* OS details, and versions of utilities like curl and OpenSSL.
339339
*
340-
* @uses class_exists() to check for the availability of the Imagick and Gmagick classes for version detection.
340+
* @uses class_exists() to check for the availability of the Imagick and Gmagick
341+
* classes for version detection.
341342
*/
342343
function get_env_details() {
343344

0 commit comments

Comments
 (0)