From 25f4f70942fc81b26dbbf11e5049bfbee59141cb Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 8 May 2025 18:09:38 +0200 Subject: [PATCH 1/4] Dump a new autoloader when generating a Phar --- src/Context/FeatureContext.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Context/FeatureContext.php b/src/Context/FeatureContext.php index df28bb3dd..e2e36cce9 100644 --- a/src/Context/FeatureContext.php +++ b/src/Context/FeatureContext.php @@ -921,6 +921,13 @@ public function build_phar( $version = 'same' ) { $make_phar_path = self::get_vendor_dir() . '/../utils/make-phar.php'; } + // Temporarily modify the Composer autoloader used within the Phar + // so that it doesn't clash if autoloading is already happening outside of it, + // for example when generating code coverage. + // This modifies composer.json. + $this->composer_command( 'config autoloader-suffix "WpCliTestsPhar" --working-dir=' . dirname( self::get_vendor_dir() ) ); + $this->composer_command( 'dump-autoload --working-dir=' . dirname( self::get_vendor_dir() ) ); + $this->proc( Utils\esc_cmd( 'php -dphar.readonly=0 %1$s %2$s --version=%3$s && chmod +x %2$s', @@ -929,6 +936,10 @@ public function build_phar( $version = 'same' ) { $version ) )->run_check(); + + // Revert the suffix change again + $this->composer_command( 'config autoloader-suffix "WpCliBundle" --working-dir=' . dirname( self::get_vendor_dir() ) ); + $this->composer_command( 'dump-autoload --working-dir=' . dirname( self::get_vendor_dir() ) ); } public function download_phar( $version = 'same' ) { From 690bf024f947004a3260f9b847cd5ea0e7c1436d Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 9 May 2025 09:54:40 +0200 Subject: [PATCH 2/4] Only modify autoloader if inside bundle --- src/Context/FeatureContext.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Context/FeatureContext.php b/src/Context/FeatureContext.php index e2e36cce9..ae88f31ef 100644 --- a/src/Context/FeatureContext.php +++ b/src/Context/FeatureContext.php @@ -913,11 +913,15 @@ public function create_run_dir() { public function build_phar( $version = 'same' ) { $this->variables['PHAR_PATH'] = $this->variables['RUN_DIR'] . '/' . uniqid( 'wp-cli-build-', true ) . '.phar'; + $is_bundle = false; + // Test running against a package installed as a WP-CLI dependency // WP-CLI bundle installed as a project dependency $make_phar_path = self::get_vendor_dir() . '/wp-cli/wp-cli-bundle/utils/make-phar.php'; if ( ! file_exists( $make_phar_path ) ) { // Running against WP-CLI bundle proper + $is_bundle = true; + $make_phar_path = self::get_vendor_dir() . '/../utils/make-phar.php'; } @@ -925,8 +929,10 @@ public function build_phar( $version = 'same' ) { // so that it doesn't clash if autoloading is already happening outside of it, // for example when generating code coverage. // This modifies composer.json. - $this->composer_command( 'config autoloader-suffix "WpCliTestsPhar" --working-dir=' . dirname( self::get_vendor_dir() ) ); - $this->composer_command( 'dump-autoload --working-dir=' . dirname( self::get_vendor_dir() ) ); + if ( $is_bundle ) { + $this->composer_command( 'config autoloader-suffix "WpCliTestsPhar" --working-dir=' . dirname( self::get_vendor_dir() ) ); + $this->composer_command( 'dump-autoload --working-dir=' . dirname( self::get_vendor_dir() ) ); + } $this->proc( Utils\esc_cmd( @@ -938,8 +944,10 @@ public function build_phar( $version = 'same' ) { )->run_check(); // Revert the suffix change again - $this->composer_command( 'config autoloader-suffix "WpCliBundle" --working-dir=' . dirname( self::get_vendor_dir() ) ); - $this->composer_command( 'dump-autoload --working-dir=' . dirname( self::get_vendor_dir() ) ); + if ( $is_bundle ) { + $this->composer_command( 'config autoloader-suffix "WpCliBundle" --working-dir=' . dirname( self::get_vendor_dir() ) ); + $this->composer_command( 'dump-autoload --working-dir=' . dirname( self::get_vendor_dir() ) ); + } } public function download_phar( $version = 'same' ) { From 01bca4b66f6aba7962162d04582a5a160c1a0e80 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 9 May 2025 09:58:36 +0200 Subject: [PATCH 3/4] And only run if actually collecting coverage --- src/Context/FeatureContext.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Context/FeatureContext.php b/src/Context/FeatureContext.php index ae88f31ef..bbf750712 100644 --- a/src/Context/FeatureContext.php +++ b/src/Context/FeatureContext.php @@ -188,12 +188,21 @@ public static function forget_feature( AfterFeatureScope $scope ) { } /** - * @AfterSuite + * Whether tests are currently running with code coverage collection. + * + * @return bool */ - public static function merge_coverage_reports() { + private static function running_with_code_coverage() { $with_code_coverage = (string) getenv( 'WP_CLI_TEST_COVERAGE' ); - if ( ! \in_array( $with_code_coverage, [ 'true', '1' ], true ) ) { + return \in_array( $with_code_coverage, [ 'true', '1' ], true ); + } + + /** + * @AfterSuite + */ + public static function merge_coverage_reports() { + if ( self::running_with_code_coverage() ) { return; } @@ -354,9 +363,7 @@ private static function get_process_env_variables() { 'TEST_RUN_DIR' => self::$behat_run_dir, ]; - $with_code_coverage = (string) getenv( 'WP_CLI_TEST_COVERAGE' ); - - if ( \in_array( $with_code_coverage, [ 'true', '1' ], true ) ) { + if ( self::running_with_code_coverage() ) { $has_coverage_driver = ( new Runtime() )->hasXdebug() || ( new Runtime() )->hasPCOV(); if ( ! $has_coverage_driver ) { @@ -929,7 +936,7 @@ public function build_phar( $version = 'same' ) { // so that it doesn't clash if autoloading is already happening outside of it, // for example when generating code coverage. // This modifies composer.json. - if ( $is_bundle ) { + if ( $is_bundle && self::running_with_code_coverage() ) { $this->composer_command( 'config autoloader-suffix "WpCliTestsPhar" --working-dir=' . dirname( self::get_vendor_dir() ) ); $this->composer_command( 'dump-autoload --working-dir=' . dirname( self::get_vendor_dir() ) ); } @@ -944,7 +951,7 @@ public function build_phar( $version = 'same' ) { )->run_check(); // Revert the suffix change again - if ( $is_bundle ) { + if ( $is_bundle && self::running_with_code_coverage() ) { $this->composer_command( 'config autoloader-suffix "WpCliBundle" --working-dir=' . dirname( self::get_vendor_dir() ) ); $this->composer_command( 'dump-autoload --working-dir=' . dirname( self::get_vendor_dir() ) ); } From bf9ea5ebcce8fda79359ba3f28af7992d501356c Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 9 May 2025 10:11:18 +0200 Subject: [PATCH 4/4] Add back missing exclamation mark --- src/Context/FeatureContext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Context/FeatureContext.php b/src/Context/FeatureContext.php index bbf750712..07ef0a86a 100644 --- a/src/Context/FeatureContext.php +++ b/src/Context/FeatureContext.php @@ -202,7 +202,7 @@ private static function running_with_code_coverage() { * @AfterSuite */ public static function merge_coverage_reports() { - if ( self::running_with_code_coverage() ) { + if ( ! self::running_with_code_coverage() ) { return; }