Skip to content

Commit 5915cb0

Browse files
schlesseraclaude
andcommitted
Launch WP-CLI processes with cheaper PHP settings
Turn Xdebug off for the processes launched by the tests unless code coverage is collected or a mode or session is requested explicitly, and keep the code of WP-CLI and the project under test in a file-based opcode cache. Files below the temporary directory, where scenarios create and rewrite their files, are never cached. WP_CLI_TEST_PHP_TUNING=off launches the processes with the PHP configuration as it is. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9298472 commit 5915cb0

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

‎features/behat-steps.feature‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,30 @@ Feature: Test that WP-CLI Behat steps work as expected
2727
Given a non-existent test-dir directory
2828
Then the test-dir directory should not exist
2929

30+
Scenario: Always run the current version of files created by a scenario
31+
Given an empty directory
32+
And a script.php file:
33+
"""
34+
<?php echo 'first';
35+
"""
36+
37+
When I run `wp --require=script.php --skip-wordpress eval ";"`
38+
Then STDOUT should be:
39+
"""
40+
first
41+
"""
42+
43+
Given a script.php file:
44+
"""
45+
<?php echo 'other';
46+
"""
47+
48+
When I run `wp --require=script.php --skip-wordpress eval ";"`
49+
Then STDOUT should be:
50+
"""
51+
other
52+
"""
53+
3054
Scenario: Test "Given an empty cache" step
3155
Given a WP installation
3256
And an empty cache

‎src/Context/FeatureContext.php‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ private static function get_process_env_variables(): array {
506506
$env['WP_CLI_REQUIRE'] = $updated;
507507
}
508508

509+
$env = array_merge( $env, self::get_php_tuning_env_variables() );
510+
509511
$config_path = getenv( 'WP_CLI_CONFIG_PATH' );
510512
if ( false !== $config_path ) {
511513
$env['WP_CLI_CONFIG_PATH'] = $config_path;
@@ -555,6 +557,65 @@ private static function get_process_env_variables(): array {
555557
return $env;
556558
}
557559

560+
/**
561+
* Get the environment variables that make the PHP processes launched by the tests cheaper.
562+
*
563+
* Xdebug is switched off unless code coverage is collected or a mode or session is requested
564+
* explicitly. The code of WP-CLI and of the project under test is kept in a file-based opcode
565+
* cache shared by all processes. Everything below the temporary directory is excluded from that
566+
* cache, as scenarios create and rewrite PHP files there and must always run the current version.
567+
*
568+
* Set WP_CLI_TEST_PHP_TUNING=off to launch the processes with the PHP configuration as it is.
569+
*
570+
* @return array<string, string>
571+
*/
572+
private static function get_php_tuning_env_variables(): array {
573+
if ( 'off' === getenv( 'WP_CLI_TEST_PHP_TUNING' ) || self::running_with_code_coverage() ) {
574+
return [];
575+
}
576+
577+
$env = [];
578+
579+
if ( false === getenv( 'XDEBUG_MODE' ) && false === getenv( 'XDEBUG_SESSION' ) && false === getenv( 'XDEBUG_CONFIG' ) ) {
580+
$env['XDEBUG_MODE'] = 'off';
581+
}
582+
583+
// The file cache cannot be used on its own on Windows.
584+
if ( Utils\is_windows() ) {
585+
return $env;
586+
}
587+
588+
$temp_dir = rtrim( sys_get_temp_dir(), '/' );
589+
$config_dir = "{$temp_dir}/wp-cli-test-php-config";
590+
$cache_dir = "{$temp_dir}/wp-cli-test-opcache";
591+
592+
foreach ( [ $config_dir, $cache_dir ] as $dir ) {
593+
if ( ! is_dir( $dir ) && ! @mkdir( $dir, 0777, true ) && ! is_dir( $dir ) ) {
594+
return $env;
595+
}
596+
}
597+
598+
$excluded = array_unique( array_filter( [ $temp_dir, realpath( $temp_dir ) ] ) );
599+
file_put_contents( "{$config_dir}/opcache-blacklist.txt", implode( "/\n", $excluded ) . "/\n" );
600+
601+
$ini = [
602+
'opcache.enable_cli=1',
603+
"opcache.file_cache={$cache_dir}",
604+
'opcache.file_cache_only=1',
605+
"opcache.blacklist_filename={$config_dir}/opcache-blacklist.txt",
606+
];
607+
file_put_contents( "{$config_dir}/wp-cli-tests.ini", implode( "\n", $ini ) . "\n" );
608+
609+
// Add to the directories PHP scans for configuration files rather than replacing them.
610+
$scan_dirs = getenv( 'PHP_INI_SCAN_DIR' );
611+
if ( false === $scan_dirs ) {
612+
$scan_dirs = PHP_CONFIG_FILE_SCAN_DIR;
613+
}
614+
$env['PHP_INI_SCAN_DIR'] = ( '' !== $scan_dirs ? $scan_dirs . PATH_SEPARATOR : '' ) . $config_dir;
615+
616+
return $env;
617+
}
618+
558619
/**
559620
* Get the internal variables to use within tests.
560621
*

0 commit comments

Comments
 (0)