-
Notifications
You must be signed in to change notification settings - Fork 27
Normalize Windows temp dir in Given a directory step
#330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = Utils\is_windows(); | ||
|
|
||
| $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}" ) ); | ||
|
Comment on lines
+67
to
+70
|
||
|
|
||
| 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 | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for checking if the directory is within the temporary directory is quite repetitive and difficult to read. Since you are checking multiple candidates against the same directory using a conditional comparison function (
striposvsstrpos), you can simplify this by using a loop. This improves maintainability and reduces the risk of errors when adding new candidates.