Skip to content

Commit 3f8fc4f

Browse files
committed
Merge branch 'javiercasares-report' into javiercasares-prepare
2 parents 927ed09 + eba4af6 commit 3f8fc4f

File tree

4 files changed

+235
-98
lines changed

4 files changed

+235
-98
lines changed

cleanup.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,52 @@
11
<?php
22

33
/**
4-
* Cleans up the environment after the test run.
4+
* This script is responsible for cleaning up the test environment after a run of the WordPress PHPUnit Test Runner.
5+
* It ensures that temporary directories and files created during the test process are properly deleted.
6+
*
7+
* @link https://github.com/wordpress/phpunit-test-runner/ Original source repository
8+
* @package WordPress
59
*/
6-
710
require __DIR__ . '/functions.php';
811

9-
// Check required environment variables.
12+
/**
13+
* Check for the presence of required environment variables.
14+
* This function should be defined in functions.php and should throw an
15+
* exception or exit if any required variables are missing.
16+
*/
1017
check_required_env();
1118

12-
// Bring some environment variables into scope.
13-
$WPT_PREPARE_DIR = getenv( 'WPT_PREPARE_DIR' );
14-
$WPT_SSH_CONNECT = getenv( 'WPT_SSH_CONNECT' );
15-
$WPT_SSH_OPTIONS = getenv( 'WPT_SSH_OPTIONS' ) ? : '-o StrictHostKeyChecking=no';
16-
$WPT_TEST_DIR = getenv( 'WPT_TEST_DIR' );
17-
$WPT_RM_TEST_DIR_CMD = getenv( 'WPT_RM_TEST_DIR_CMD' ) ? : 'rm -r ' . $WPT_TEST_DIR;
19+
/**
20+
* Retrieves environment variables and sets defaults for test preparation.
21+
* These variables are used to configure SSH connections, file paths, and
22+
* executable commands needed for setting up the test environment.
23+
*/
24+
$WPT_PREPARE_DIR = trim( getenv( 'WPT_PREPARE_DIR' ) );
25+
$WPT_SSH_CONNECT = trim( getenv( 'WPT_SSH_CONNECT' ) );
26+
$WPT_SSH_OPTIONS = trim( getenv( 'WPT_SSH_OPTIONS' ) ) ? : '-o StrictHostKeyChecking=no';
27+
$WPT_TEST_DIR = trim( getenv( 'WPT_TEST_DIR' ) );
28+
$WPT_RM_TEST_DIR_CMD = trim( getenv( 'WPT_RM_TEST_DIR_CMD' ) ) ? : 'rm -r ' . $WPT_TEST_DIR;
1829

19-
// Clean up the preparation directory.
20-
// Only forcefully delete the .git directory, to prevent disasters otherwise.
30+
/**
31+
* The directory path of the test preparation directory is assumed to be previously defined.
32+
* For example: $WPT_PREPARE_DIR = '/path/to/your/preparation/dir';
33+
* Clean up the preparation directory.
34+
* Forcefully deletes only the .git directory and the node_modules cache.
35+
* Afterward, the entire preparation directory is removed to ensure a clean state for the next test run.
36+
*/
2137
perform_operations( array(
2238
'rm -rf ' . escapeshellarg( $WPT_PREPARE_DIR . '/.git' ),
2339
'rm -rf ' . escapeshellarg( $WPT_PREPARE_DIR . '/node_modules/.cache' ),
2440
'rm -r ' . escapeshellarg( $WPT_PREPARE_DIR ),
2541
) );
2642

27-
// Clean up the test directory in remote environments.
43+
/**
44+
* Cleans up the test directory on a remote server.
45+
* This conditional block checks if an SSH connection string is provided and is not empty.
46+
* If a connection string is present, it triggers a cleanup operation on the remote environment.
47+
* The cleanup operation is executed by the `perform_operations` function which takes an array
48+
* of shell commands as its input.
49+
*/
2850
if ( ! empty( $WPT_SSH_CONNECT ) ) {
2951
perform_operations( array(
3052
'ssh ' . $WPT_SSH_OPTIONS . ' ' . escapeshellarg( $WPT_SSH_CONNECT ) . ' ' . escapeshellarg( $WPT_RM_TEST_DIR_CMD ),

prepare.php

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* The script assumes that certain environment variables are set to configure SSH,
77
* directories, and executables used in the test preparation process.
88
*
9+
* @link https://github.com/wordpress/phpunit-test-runner/ Original source repository
910
* @package WordPress
1011
*/
1112
require __DIR__ . '/functions.php';
@@ -42,6 +43,9 @@
4243
case 'true':
4344
$WPT_DEBUG = true;
4445
break;
46+
case 'verbose':
47+
$WPT_DEBUG = 'verbose';
48+
break;
4549
default:
4650
$WPT_DEBUG = false;
4751
break;
@@ -83,12 +87,12 @@
8387
if( empty( $WPT_SSH_CONNECT ) ) {
8488
perform_operations( array(
8589
'chmod 600 ~/.ssh/id_rsa',
86-
'wp cli info',
90+
'wp cli info'
8791
) );
8892
} else {
8993
perform_operations( array(
9094
'chmod 600 ~/.ssh/id_rsa',
91-
'ssh -q ' . $WPT_SSH_OPTIONS . ' ' . escapeshellarg( $WPT_SSH_CONNECT ) . ' wp cli info',
95+
'ssh -q ' . $WPT_SSH_OPTIONS . ' ' . escapeshellarg( $WPT_SSH_CONNECT ) . ' wp cli info'
9296
) );
9397
}
9498

@@ -115,7 +119,7 @@
115119
'cd ' . escapeshellarg( $WPT_PREPARE_DIR . '/tests/phpunit/data/plugins/' ) . '; unzip wordpress-importer.zip; rm wordpress-importer.zip',
116120

117121
// Change directory to the preparation directory, install npm dependencies, and build the project.
118-
'cd ' . escapeshellarg( $WPT_PREPARE_DIR ) . '; npm install && npm run build',
122+
'cd ' . escapeshellarg( $WPT_PREPARE_DIR ) . '; npm install && npm run build'
119123

120124
) );
121125

@@ -277,21 +281,7 @@
277281
}
278282

279283
// Log the obtained PHP version for confirmation and debugging purposes.
280-
log_message( "Environment PHP Version: $env_php_version" );
281-
282-
283-
/**
284-
* Ensures compatibility with PHPUnit by adjusting the PHP version if needed.
285-
* If the environment's PHP version is 8.x.x, it overrides the version to 7.4.
286-
* This is because the core PHPUnit tests may not be compatible with PHP 8.x.x.
287-
*/
288-
if ( substr( $env_php_version, 0 , 2 ) === '8.' ) {
289-
// Log the occurrence of PHP version 8.x.x and the decision to use PHPUnit compatible with PHP 7.4 instead.
290-
log_message( 'Version 8.x.x Found. Downloading PHPUnit for PHP 7.4 instead for compatibility.' );
291-
292-
// Override the PHP version to 7.4 for PHPUnit compatibility.
293-
$env_php_version = '7.4';
294-
}
284+
log_message( 'Environment PHP Version: ' . $env_php_version );
295285

296286
/**
297287
* Checks if the detected PHP version is below 7.0.
@@ -300,57 +290,43 @@
300290
*/
301291
if ( version_compare( $env_php_version, '7.0', '<' ) ) {
302292
// Logs an error message indicating the test runner's incompatibility with PHP versions below 7.0.
303-
error_message( "The test runner is not compatible with PHP < 7.0." );
293+
error_message( 'The test runner is not compatible with PHP < 7.0.' );
304294
}
305295

306296
/**
307-
* Installs PHPUnit 5.7 directly if the PHP version is less than 7.1.
308-
* PHPUnit 5.7 is the last version to support PHP 7.0.
297+
* Use Composer to manage PHPUnit and its dependencies.
298+
* This allows for better dependency management and compatibility.
309299
*/
310-
if ( version_compare( $env_php_version, '7.1', '<' ) ) {
311-
312-
// Download the PHPUnit phar file directly using wget.
313-
perform_operations( array(
314-
'wget -O ' . escapeshellarg( $WPT_PREPARE_DIR . '/phpunit.phar' ) . ' https://phar.phpunit.de/phpunit-5.7.phar',
315-
) );
316-
317-
} else {
318-
319-
/**
320-
* If the PHP version is 7.1 or greater, use Composer to manage PHPUnit and its dependencies.
321-
* This allows for better dependency management and compatibility.
322-
*/
323300

324-
// Check if Composer is installed and available in the PATH.
325-
$composer_cmd = 'cd ' . escapeshellarg( $WPT_PREPARE_DIR ) . ' && ';
326-
$retval = 0;
327-
$composer_path = escapeshellarg( system( 'which composer', $retval ) );
328-
329-
if ( $retval === 0 ) {
330-
331-
// If Composer is available, prepare the command to use the Composer binary.
332-
$composer_cmd .= $composer_path . ' ';
301+
// Check if Composer is installed and available in the PATH.
302+
$composer_cmd = 'cd ' . escapeshellarg( $WPT_PREPARE_DIR ) . ' && ';
303+
$retval = 0;
304+
$composer_path = escapeshellarg( system( 'which composer', $retval ) );
333305

334-
} else {
306+
if ( $retval === 0 ) {
335307

336-
// If Composer is not available, download the Composer phar file.
337-
log_message( 'Local Composer not found. Downloading latest stable ...' );
308+
// If Composer is available, prepare the command to use the Composer binary.
309+
$composer_cmd .= $composer_path . ' ';
338310

339-
perform_operations( array(
340-
'wget -O ' . escapeshellarg( $WPT_PREPARE_DIR . '/composer.phar' ) . ' https://getcomposer.org/composer-stable.phar',
341-
) );
311+
} else {
342312

343-
// Update the command to use the downloaded Composer phar file.
344-
$composer_cmd .= 'php composer.phar ';
345-
}
313+
// If Composer is not available, download the Composer phar file.
314+
log_message( 'Local Composer not found. Downloading latest stable ...' );
346315

347-
// Set the PHP version for Composer to ensure compatibility and update dependencies.
348316
perform_operations( array(
349-
$composer_cmd . 'config platform.php ' . escapeshellarg( $env_php_version ),
350-
$composer_cmd . 'update',
317+
'wget -O ' . escapeshellarg( $WPT_PREPARE_DIR . '/composer.phar' ) . ' https://getcomposer.org/composer-stable.phar',
351318
) );
319+
320+
// Update the command to use the downloaded Composer phar file.
321+
$composer_cmd .= 'php composer.phar ';
352322
}
353323

324+
// Set the PHP version for Composer to ensure compatibility and update dependencies.
325+
perform_operations( array(
326+
$composer_cmd . 'config platform.php ' . escapeshellarg( $env_php_version ),
327+
$composer_cmd . 'update',
328+
) );
329+
354330
/**
355331
* If an SSH connection is configured, use rsync to transfer the prepared files to the remote test environment.
356332
* The -r option for rsync enables recursive copying to handle directory structures.

0 commit comments

Comments
 (0)