[5.x] TIA: stat once in ReadCapture::url_stat() (#2038)

* Stat once in ReadCapture url_stat

* Keep quiet url_stat probes away from error handlers

---------

Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
Jeremy Braband
2026-11-20 13:03:18 -06:00
committed by GitHub
parent 776a44a391
commit 996b5ee8f5
4 changed files with 41 additions and 15 deletions
+5 -13
View File
@@ -286,21 +286,13 @@ final class ReadCapture
public function url_stat(string $path, int $flags): array|false
{
return $this->withOriginalWrapper(function () use ($path, $flags): array|false {
$isQuiet = ($flags & STREAM_URL_STAT_QUIET) !== 0;
set_error_handler(static fn (): bool => true);
if (($flags & STREAM_URL_STAT_LINK) !== 0) {
if ($isQuiet && ! is_link($path) && ! file_exists($path)) {
return false;
}
return @lstat($path);
try {
return ($flags & STREAM_URL_STAT_LINK) !== 0 ? lstat($path) : stat($path);
} finally {
restore_error_handler();
}
if ($isQuiet && ! file_exists($path)) {
return false;
}
return @stat($path);
});
}
+2 -1
View File
@@ -2079,6 +2079,7 @@
✓ the file wrapper → it is restored when the recorder resets
✓ the file wrapper → it passes filesystem operations through while a test records
✓ the file wrapper → it writes with LOCK_EX while a test records
✓ the file wrapper → it reports a missing path to the error handler as plain PHP does while a test records
✓ the file wrapper → it reports symlinks as links while a test records with dataset "a link to an existing file"
✓ the file wrapper → it reports symlinks as links while a test records with dataset "a dangling link"
✓ a wrapper already registered for file:// → it opens reads through it and still links them
@@ -2601,4 +2602,4 @@
✓ pass with dataset with ('my-datas-set-value')
✓ within describe → pass with dataset with ('my-datas-set-value')
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1893 passed (4339 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1894 passed (4340 assertions)
+33
View File
@@ -205,6 +205,39 @@ describe('the file wrapper', function (): void {
}
});
it('reports a missing path to the error handler as plain PHP does while a test records', function (): void {
$missing = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-read-capture-missing-'.bin2hex(random_bytes(8));
$warnings = [];
$results = [];
set_error_handler(function (int $level, string $message) use (&$warnings): bool {
$warnings[] = $message;
return true;
});
try {
tiaReadCaptureLinks(function () use ($missing, &$warnings, &$results): void {
clearstatcache();
$results['quiet'] = [file_exists($missing), is_file($missing), is_dir($missing), is_link($missing)];
$results['warnings after quiet probes'] = count($warnings);
$results['stat'] = stat($missing);
$results['warnings after stat'] = count($warnings);
});
} finally {
restore_error_handler();
}
expect($results)->toBe([
'quiet' => [false, false, false, false],
'warnings after quiet probes' => 0,
'stat' => false,
'warnings after stat' => 1,
]);
});
it('reports symlinks as links while a test records', function (bool $targetExists, bool $expectedExists): void {
$directory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-read-capture-'.bin2hex(random_bytes(8));
mkdir($directory);
+1 -1
View File
@@ -21,7 +21,7 @@ test('parallel', function () use ($run): void {
$output = implode("\n", array_slice(explode("\n", (string) $output), -10));
$profileOutput = $run('tests/Fixtures/Suites/SuccessOnly.php', '--profile');
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 30 skipped, 1766 passed (3814 assertions)';
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 30 skipped, 1767 passed (3815 assertions)';
expect($output)
->toContain("Tests: {$expected}")