Skip to content

Commit c1be927

Browse files
committed
more debugging
1 parent 2614802 commit c1be927

File tree

1 file changed

+55
-25
lines changed

1 file changed

+55
-25
lines changed

src/Context/FeatureContext.php

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,26 @@ public static function get_bin_path(): ?string {
406406
self::get_framework_dir() . DIRECTORY_SEPARATOR . 'bin',
407407
];
408408

409-
$bin = Utils\is_windows() ? 'wp.bat' : 'wp';
410-
411-
foreach ( $bin_paths as $path ) {
412-
$full_bin_path = $path . DIRECTORY_SEPARATOR . $bin;
413-
if ( is_file( $full_bin_path ) && ( Utils\is_windows() || is_executable( $full_bin_path ) ) ) {
414-
return $path;
409+
if ( Utils\is_windows() ) {
410+
foreach ( $bin_paths as $path ) {
411+
$wp_script_path = $path . DIRECTORY_SEPARATOR . 'wp';
412+
if ( is_file( $wp_script_path ) ) {
413+
$wp_bat_path = $path . DIRECTORY_SEPARATOR . 'wp.bat';
414+
if ( ! is_file( $wp_bat_path ) ) {
415+
$bat_content = '@ECHO OFF' . PHP_EOL;
416+
// Use the currently running PHP executable to avoid PATH issues.
417+
$bat_content .= '"' . PHP_BINARY . '" "' . realpath( $wp_script_path ) . '" %*';
418+
file_put_contents( $wp_bat_path, $bat_content );
419+
}
420+
return $path;
421+
}
422+
}
423+
} else {
424+
foreach ( $bin_paths as $path ) {
425+
$full_bin_path = $path . DIRECTORY_SEPARATOR . 'wp';
426+
if ( is_file( $full_bin_path ) && is_executable( $full_bin_path ) ) {
427+
return $path;
428+
}
415429
}
416430
}
417431

@@ -439,19 +453,10 @@ private static function get_process_env_variables(): array {
439453

440454
wp_cli_behat_env_debug( "WP-CLI binary path: {$bin_path}" );
441455

442-
$bin = $bin_path . DIRECTORY_SEPARATOR . ( Utils\is_windows() ? 'wp.bat' : 'wp' );
443-
444-
if ( ! file_exists( $bin ) ) {
445-
wp_cli_behat_env_debug( "WARNING: File $bin not found." );
446-
}
447-
448-
if ( ! is_executable( $bin ) ) {
449-
wp_cli_behat_env_debug( "WARNING: File $bin is not executable." );
450-
}
451-
452-
$path_separator = Utils\is_windows() ? ';' : ':';
453-
$env = [
454-
'PATH' => $bin_path . $path_separator . getenv( 'PATH' ),
456+
$path_separator = Utils\is_windows() ? ';' : ':';
457+
$php_binary_path = dirname( PHP_BINARY );
458+
$env = [
459+
'PATH' => $php_binary_path . $path_separator . $bin_path . $path_separator . getenv( 'PATH' ),
455460
'BEHAT_RUN' => 1,
456461
'HOME' => sys_get_temp_dir() . '/wp-cli-home',
457462
'TEST_RUN_DIR' => self::$behat_run_dir,
@@ -1241,6 +1246,10 @@ public function proc( $command, $assoc_args = [], $path = '' ): Process {
12411246
$cwd = null;
12421247
}
12431248

1249+
wp_cli_behat_env_debug( "Running command: {$command}" );
1250+
wp_cli_behat_env_debug( "In directory: {$cwd}" );
1251+
wp_cli_behat_env_debug( "With PATH: {$env['PATH']}" );
1252+
12441253
return Process::create( $command, $cwd, $env );
12451254
}
12461255

@@ -1250,20 +1259,41 @@ public function proc( $command, $assoc_args = [], $path = '' ): Process {
12501259
* @param string $cmd
12511260
*/
12521261
public function background_proc( $cmd ): void {
1253-
$descriptors = [
1254-
0 => STDIN,
1255-
1 => [ 'pipe', 'w' ],
1256-
2 => [ 'pipe', 'w' ],
1257-
];
1262+
if ( Utils\is_windows() ) {
1263+
// On Windows, leaving pipes open can cause hangs.
1264+
// Redirect output to files and close stdin.
1265+
$stdout_file = tempnam( sys_get_temp_dir(), 'behat-stdout-' );
1266+
$stderr_file = tempnam( sys_get_temp_dir(), 'behat-stderr-' );
1267+
$descriptors = [
1268+
0 => [ 'pipe', 'r' ],
1269+
1 => [ 'file', $stdout_file, 'a' ],
1270+
2 => [ 'file', $stderr_file, 'a' ],
1271+
];
1272+
} else {
1273+
$descriptors = [
1274+
0 => STDIN,
1275+
1 => [ 'pipe', 'w' ],
1276+
2 => [ 'pipe', 'w' ],
1277+
];
1278+
}
12581279

12591280
$proc = proc_open( $cmd, $descriptors, $pipes, $this->variables['RUN_DIR'], self::get_process_env_variables() );
12601281

1282+
if ( Utils\is_windows() ) {
1283+
fclose( $pipes[0] );
1284+
}
1285+
12611286
sleep( 1 );
12621287

12631288
$status = proc_get_status( $proc );
12641289

12651290
if ( ! $status['running'] ) {
1266-
$stderr = is_resource( $pipes[2] ) ? ( ': ' . stream_get_contents( $pipes[2] ) ) : '';
1291+
if ( Utils\is_windows() ) {
1292+
$stderr = file_get_contents( $stderr_file );
1293+
$stderr = $stderr ? ': ' . $stderr : '';
1294+
} else {
1295+
$stderr = is_resource( $pipes[2] ) ? ( ': ' . stream_get_contents( $pipes[2] ) ) : '';
1296+
}
12671297
throw new RuntimeException( sprintf( "Failed to start background process '%s'%s.", $cmd, $stderr ) );
12681298
}
12691299

0 commit comments

Comments
 (0)