@@ -269,12 +269,21 @@ public static function forget_feature( AfterFeatureScope $scope ): void {
269269 }
270270
271271 /**
272- * @AfterSuite
272+ * Whether tests are currently running with code coverage collection.
273+ *
274+ * @return bool
273275 */
274- public static function merge_coverage_reports (): void {
276+ private static function running_with_code_coverage () {
275277 $ with_code_coverage = (string ) getenv ( 'WP_CLI_TEST_COVERAGE ' );
276278
277- if ( ! \in_array ( $ with_code_coverage , [ 'true ' , '1 ' ], true ) ) {
279+ return \in_array ( $ with_code_coverage , [ 'true ' , '1 ' ], true );
280+ }
281+
282+ /**
283+ * @AfterSuite
284+ */
285+ public static function merge_coverage_reports (): void {
286+ if ( ! self ::running_with_code_coverage () ) {
278287 return ;
279288 }
280289
@@ -436,9 +445,7 @@ private static function get_process_env_variables(): array {
436445 'TEST_RUN_DIR ' => self ::$ behat_run_dir ,
437446 ];
438447
439- $ with_code_coverage = (string ) getenv ( 'WP_CLI_TEST_COVERAGE ' );
440-
441- if ( \in_array ( $ with_code_coverage , [ 'true ' , '1 ' ], true ) ) {
448+ if ( self ::running_with_code_coverage () ) {
442449 $ has_coverage_driver = ( new Runtime () )->hasXdebug () || ( new Runtime () )->hasPCOV ();
443450
444451 if ( ! $ has_coverage_driver ) {
@@ -1021,14 +1028,27 @@ public function create_run_dir(): void {
10211028 public function build_phar ( $ version = 'same ' ): void {
10221029 $ this ->variables ['PHAR_PATH ' ] = $ this ->variables ['RUN_DIR ' ] . '/ ' . uniqid ( 'wp-cli-build- ' , true ) . '.phar ' ;
10231030
1031+ $ is_bundle = false ;
1032+
10241033 // Test running against a package installed as a WP-CLI dependency
10251034 // WP-CLI bundle installed as a project dependency
10261035 $ make_phar_path = self ::get_vendor_dir () . '/wp-cli/wp-cli-bundle/utils/make-phar.php ' ;
10271036 if ( ! file_exists ( $ make_phar_path ) ) {
10281037 // Running against WP-CLI bundle proper
1038+ $ is_bundle = true ;
1039+
10291040 $ make_phar_path = self ::get_vendor_dir () . '/../utils/make-phar.php ' ;
10301041 }
10311042
1043+ // Temporarily modify the Composer autoloader used within the Phar
1044+ // so that it doesn't clash if autoloading is already happening outside of it,
1045+ // for example when generating code coverage.
1046+ // This modifies composer.json.
1047+ if ( $ is_bundle && self ::running_with_code_coverage () ) {
1048+ $ this ->composer_command ( 'config autoloader-suffix "WpCliTestsPhar" --working-dir= ' . dirname ( self ::get_vendor_dir () ) );
1049+ $ this ->composer_command ( 'dump-autoload --working-dir= ' . dirname ( self ::get_vendor_dir () ) );
1050+ }
1051+
10321052 $ this ->proc (
10331053 Utils \esc_cmd (
10341054 'php -dphar.readonly=0 %1$s %2$s --version=%3$s && chmod +x %2$s ' ,
@@ -1037,6 +1057,12 @@ public function build_phar( $version = 'same' ): void {
10371057 $ version
10381058 )
10391059 )->run_check ();
1060+
1061+ // Revert the suffix change again
1062+ if ( $ is_bundle && self ::running_with_code_coverage () ) {
1063+ $ this ->composer_command ( 'config autoloader-suffix "WpCliBundle" --working-dir= ' . dirname ( self ::get_vendor_dir () ) );
1064+ $ this ->composer_command ( 'dump-autoload --working-dir= ' . dirname ( self ::get_vendor_dir () ) );
1065+ }
10401066 }
10411067
10421068 /**
0 commit comments