Skip to content

Commit 68fa0a0

Browse files
authored
Merge pull request #127 from aaemnnosttv/pr-behat-stat-cache-fix
Update file/folder existence check for cache safety
2 parents 6c012c7 + 184bd49 commit 68fa0a0

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/Context/ThenStepDefinitions.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,25 +196,32 @@ public function then_a_specific_file_folder_should_exist( $path, $type, $action,
196196
$path = $this->variables['RUN_DIR'] . "/$path";
197197
}
198198

199-
if ( 'file' === $type ) {
200-
$test = 'file_exists';
201-
} elseif ( 'directory' === $type ) {
202-
$test = 'is_dir';
203-
}
199+
$exists = function ( $path ) use ( $type ) {
200+
// Clear the stat cache for the path first to avoid
201+
// potentially inaccurate results when files change outside of PHP.
202+
// See https://www.php.net/manual/en/function.clearstatcache.php
203+
clearstatcache( false, $path );
204+
205+
if ( 'directory' === $type ) {
206+
return is_dir( $path );
207+
}
208+
209+
return file_exists( $path );
210+
};
204211

205212
switch ( $action ) {
206213
case 'exist':
207-
if ( ! $test( $path ) ) {
214+
if ( ! $exists( $path ) ) {
208215
throw new Exception( "$path doesn't exist." );
209216
}
210217
break;
211218
case 'not exist':
212-
if ( $test( $path ) ) {
219+
if ( $exists( $path ) ) {
213220
throw new Exception( "$path exists." );
214221
}
215222
break;
216223
default:
217-
if ( ! $test( $path ) ) {
224+
if ( ! $exists( $path ) ) {
218225
throw new Exception( "$path doesn't exist." );
219226
}
220227
$action = substr( $action, 0, -1 );

0 commit comments

Comments
 (0)