Skip to content

Commit 290dd8c

Browse files
committed
Normalize Windows temp dir in Given a directory step
1 parent 70f8de0 commit 290dd8c

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

src/Context/GivenStepDefinitions.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,28 @@ public function given_a_specific_directory( $empty_or_nonexistent, $dir ): void
5353
// Mac OS X can prefix the `/var` folder to turn it into `/private/var`.
5454
$dir = preg_replace( '|^/private/var/|', '/var/', $dir );
5555

56-
$temp_dir = realpath( sys_get_temp_dir() );
57-
$temp_dir = $temp_dir ? Path::normalize( $temp_dir ) : Path::normalize( sys_get_temp_dir() );
58-
$dir = Path::normalize( $dir );
56+
$temp_dir_original = Path::normalize( sys_get_temp_dir() );
57+
$temp_dir_real = realpath( sys_get_temp_dir() );
58+
$temp_dir_real = $temp_dir_real ? Path::normalize( $temp_dir_real ) : $temp_dir_original;
59+
$dir = Path::normalize( $dir );
5960

6061
// Also normalize temp dir for Mac OS X.
61-
$temp_dir = preg_replace( '|^/private/var/|', '/var/', $temp_dir );
62+
$temp_dir_original = preg_replace( '|^/private/var/|', '/var/', $temp_dir_original );
63+
$temp_dir_real = preg_replace( '|^/private/var/|', '/var/', $temp_dir_real );
6264

63-
// Also check for temp dir prefixed with `/private` for Mac OS X.
64-
if ( 0 !== strpos( $dir, $temp_dir ) && 0 !== strpos( $dir, "/private{$temp_dir}" ) ) {
65+
$is_windows = DIRECTORY_SEPARATOR === '\\';
66+
67+
$in_temp = 0 === ( $is_windows ? stripos( $dir, $temp_dir_original ) : strpos( $dir, $temp_dir_original ) )
68+
|| 0 === ( $is_windows ? stripos( $dir, $temp_dir_real ) : strpos( $dir, $temp_dir_real ) )
69+
|| 0 === ( $is_windows ? stripos( $dir, "/private{$temp_dir_original}" ) : strpos( $dir, "/private{$temp_dir_original}" ) )
70+
|| 0 === ( $is_windows ? stripos( $dir, "/private{$temp_dir_real}" ) : strpos( $dir, "/private{$temp_dir_real}" ) );
71+
72+
if ( ! $in_temp ) {
6573
throw new RuntimeException(
6674
sprintf(
6775
"Attempted to delete directory '%s' that is not in the temp directory '%s'. " . __FILE__ . ':' . __LINE__,
6876
$dir,
69-
$temp_dir
77+
$temp_dir_real
7078
)
7179
);
7280
}

0 commit comments

Comments
 (0)