Files
pest/tests/Features/Tia/ReadCapture.php
T
Jeremy Braband 38c79452c1 fix(tia): link files a test reads as text, not only files it executes (#2006)
* fix(tia): link files a test reads as text, not only files it executes

* fix(tia): report dangling symlinks as links while reads are captured

`is_link()` stats with STREAM_URL_STAT_LINK | STREAM_URL_STAT_QUIET.
The `file_exists()` precheck in `ReadCapture::url_stat()` follows the
link, so a dangling symlink returned false before reaching `lstat()`,
and `is_link()` reported false while a test recorded.

Handle STREAM_URL_STAT_LINK first and always answer it through
`lstat()`. A quiet stat of a path that is neither a link nor a file
still returns false without a warning.

The regression test covers a link to an existing file and a dangling
link.

* test(tia): skip the read-capture feature tests without a coverage driver

The fixture runs record a graph from coverage, so with CI's
`coverage: none` all five cases failed. Skip them when
`Coverage::isAvailable()` is false, as CoveragePiggyback does.

* chore: style

* Refresh read capture test snapshots

---------

Co-authored-by: Punyapal Shah <53343069+MrPunyapal@users.noreply.github.com>
Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
2026-11-15 00:54:24 +05:30

111 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
use Pest\Support\Coverage;
use Tests\Fixtures\Tia\Project;
afterEach(function (): void {
Project::destroyAll();
});
function tiaRecordWithSourceReadingTest(Project $project, string ...$arguments): void
{
$project->write('tests/Unit/GreeterSourceTest.php', <<<'PHP'
<?php
declare(strict_types=1);
test('keeps the greeting format', function (): void {
expect(file_get_contents(__DIR__.'/../../app/Greeter.php'))->toContain("'Hello, %s!'");
});
PHP);
$project->git()->commit('add a test that reads the greeter');
$project->pest('--tia', ...$arguments);
}
test('a test that reads a file records an edge to it', function (array $arguments): void {
$project = Project::make('master');
tiaRecordWithSourceReadingTest($project, ...$arguments);
$graph = $project->graph() ?? [];
$files = array_map(
fn (int $id): string => $graph['files'][$id],
$graph['edges']['tests/Unit/GreeterSourceTest.php'] ?? [],
);
expect($files)->toContain('app/Greeter.php');
})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows()->skip(! Coverage::isAvailable(), 'Coverage is not available');
test('a changed file selects the test that reads it', function (array $arguments): void {
$project = Project::make('master');
tiaRecordWithSourceReadingTest($project, ...$arguments);
$project->write('app/Greeter.php', <<<'PHP'
<?php
declare(strict_types=1);
namespace Fixture\App;
final class Greeter
{
public function greet(string $name): string
{
return sprintf('Hello, %s!', $name);
}
public function shout(string $name): string
{
return strtoupper($this->greet($name));
}
}
PHP);
$result = $project->pest('--tia', ...$arguments);
expect($result->exitCode)->toBe(0, $result->describe())
->and($result->affected())->toBe(3, $result->describe())
->and($result->replayed())->toBe(4, $result->describe());
})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows()->skip(! Coverage::isAvailable(), 'Coverage is not available');
test('a file a test reads does not select it when another file changes', function (): void {
$project = Project::make('master');
tiaRecordWithSourceReadingTest($project);
$project->write('app/Calculator.php', <<<'PHP'
<?php
declare(strict_types=1);
namespace Fixture\App;
final class Calculator
{
public function add(int $a, int $b): int
{
return $a + $b;
}
public function subtract(int $a, int $b): int
{
return $a - $b;
}
public function multiply(int $a, int $b): int
{
return $a * $b;
}
}
PHP);
$result = $project->pest('--tia');
expect($result->affected())->toBe(4, $result->describe())
->and($result->replayed())->toBe(3, $result->describe());
})->skipOnWindows()->skip(! Coverage::isAvailable(), 'Coverage is not available');