From 290dd8c5fe0052f8d3c2271c94ceac8a9e8e0a4f Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 15 Apr 2026 10:15:43 +0200 Subject: [PATCH] Normalize Windows temp dir in `Given a directory` step --- src/Context/GivenStepDefinitions.php | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Context/GivenStepDefinitions.php b/src/Context/GivenStepDefinitions.php index 9c8b949b..b0357e1c 100644 --- a/src/Context/GivenStepDefinitions.php +++ b/src/Context/GivenStepDefinitions.php @@ -53,20 +53,28 @@ public function given_a_specific_directory( $empty_or_nonexistent, $dir ): void // Mac OS X can prefix the `/var` folder to turn it into `/private/var`. $dir = preg_replace( '|^/private/var/|', '/var/', $dir ); - $temp_dir = realpath( sys_get_temp_dir() ); - $temp_dir = $temp_dir ? Path::normalize( $temp_dir ) : Path::normalize( sys_get_temp_dir() ); - $dir = Path::normalize( $dir ); + $temp_dir_original = Path::normalize( sys_get_temp_dir() ); + $temp_dir_real = realpath( sys_get_temp_dir() ); + $temp_dir_real = $temp_dir_real ? Path::normalize( $temp_dir_real ) : $temp_dir_original; + $dir = Path::normalize( $dir ); // Also normalize temp dir for Mac OS X. - $temp_dir = preg_replace( '|^/private/var/|', '/var/', $temp_dir ); + $temp_dir_original = preg_replace( '|^/private/var/|', '/var/', $temp_dir_original ); + $temp_dir_real = preg_replace( '|^/private/var/|', '/var/', $temp_dir_real ); - // Also check for temp dir prefixed with `/private` for Mac OS X. - if ( 0 !== strpos( $dir, $temp_dir ) && 0 !== strpos( $dir, "/private{$temp_dir}" ) ) { + $is_windows = DIRECTORY_SEPARATOR === '\\'; + + $in_temp = 0 === ( $is_windows ? stripos( $dir, $temp_dir_original ) : strpos( $dir, $temp_dir_original ) ) + || 0 === ( $is_windows ? stripos( $dir, $temp_dir_real ) : strpos( $dir, $temp_dir_real ) ) + || 0 === ( $is_windows ? stripos( $dir, "/private{$temp_dir_original}" ) : strpos( $dir, "/private{$temp_dir_original}" ) ) + || 0 === ( $is_windows ? stripos( $dir, "/private{$temp_dir_real}" ) : strpos( $dir, "/private{$temp_dir_real}" ) ); + + if ( ! $in_temp ) { throw new RuntimeException( sprintf( "Attempted to delete directory '%s' that is not in the temp directory '%s'. " . __FILE__ . ':' . __LINE__, $dir, - $temp_dir + $temp_dir_real ) ); }