Fail when a snapshot cannot be written (#2048)

Co-authored-by: haranishi <223786623+haranishi@users.noreply.github.com>
Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
Ikki Nishihara
2026-11-20 05:29:51 +09:00
committed by GitHub
parent bb91916d0d
commit 9990d0ee36
3 changed files with 39 additions and 2 deletions
+4 -1
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pest\Repositories;
use Pest\Exceptions\ShouldNotHappen;
use RuntimeException;
/**
* @internal
@@ -48,7 +49,9 @@ final readonly class Snapshot
@mkdir($directory, 0755, true);
}
file_put_contents($this->filename, $contents);
if (@file_put_contents($this->filename, $contents) !== strlen($contents)) {
throw new RuntimeException(sprintf('Unable to write snapshot at [%s].', $this->path()));
}
return $this;
}
+4 -1
View File
@@ -401,6 +401,9 @@
✓ it works with nested properties
✓ it works with higher order tests
PASS Tests\Features\Expect\SnapshotWriteFailure
✓ reports a missing snapshot that cannot be written
PASS Tests\Features\Expect\each
✓ an exception is thrown if the the type is not iterable
✓ it expects on each item
@@ -2523,4 +2526,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, 1831 passed (4152 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1832 passed (4156 assertions)
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
use Tests\Fixtures\Tia\Project;
afterEach(function (): void {
Project::destroyAll();
});
test('reports a missing snapshot that cannot be written', function (): void {
$project = Project::withoutGit();
$testFile = 'tests/Unit/BlockedSnapshotTest.php';
$directory = 'tests/.pest/snapshots/Unit/BlockedSnapshotTest';
$project->write($directory, 'An existing file.');
$project->write($testFile, <<<'PHP'
<?php
test('stores a snapshot', function (): void {
expect('content')->toMatchSnapshot();
});
PHP);
$run = $project->pest($testFile, '--update-snapshots');
expect($run->exitCode)->toBe(2, $run->describe())
->and(str_replace('\\', '/', $run->output))->toContain("Unable to write snapshot at [$directory/stores_a_snapshot.snap].")
->not->toContain('Snapshot created at')
->and(file_get_contents($project->path($directory)))->toBe('An existing file.');
})->group('integration');