Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ class FeatureContext implements Context {
*/
private $running_procs = [];

/**
* Array of temporary file paths created for background processes on Windows. Used to clean them up at the end of the scenario.
*
* @var array<string>
*/
private $temp_files = [];

/**
* Array of variables available as {VARIABLE_NAME}. Some are always set: CORE_CONFIG_SETTINGS, DB_USER, DB_PASSWORD, DB_HOST, SRC_DIR, CACHE_DIR, WP_VERSION-version-latest.
* Some are step-dependent: RUN_DIR, SUITE_CACHE_DIR, COMPOSER_LOCAL_REPOSITORY, PHAR_PATH. One is set on use: INVOKE_WP_CLI_WITH_PHP_ARGS-args.
Expand Down Expand Up @@ -767,6 +774,10 @@ public function afterScenario( AfterScenarioScope $scope ): void {
self::terminate_proc( $status['pid'] );
}

// Clean up temporary files created for background processes on Windows.
$this->cleanup_temp_files( ...$this->temp_files );
$this->temp_files = [];

if ( self::$log_run_times ) {
self::log_run_times_after_scenario( $scope );
}
Expand Down Expand Up @@ -1355,13 +1366,34 @@ public function background_proc( $cmd ): void {
if ( Utils\is_windows() ) {
$stderr = (string) file_get_contents( $stderr_file );
$stderr = $stderr ? ': ' . $stderr : '';
// Clean up temporary files.
$this->cleanup_temp_files( $stdout_file, $stderr_file );
} else {
$stderr = is_resource( $pipes[2] ) ? ( ': ' . stream_get_contents( $pipes[2] ) ) : '';
}
throw new RuntimeException( sprintf( "Failed to start background process '%s'%s.", $cmd, $stderr ) );
}

$this->running_procs[] = $proc;

// Track temporary files for cleanup at the end of the scenario.
if ( Utils\is_windows() ) {
$this->temp_files[] = $stdout_file;
$this->temp_files[] = $stderr_file;
}
}

/**
* Clean up temporary files safely.
*
* @param string ...$files File paths to clean up.
*/
private function cleanup_temp_files( ...$files ): void {
foreach ( $files as $file ) {
if ( file_exists( $file ) ) {
unlink( $file );
}
}
}

/**
Expand Down