Shorten overlong snapshot filenames (#2047)

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:55:23 +09:00
committed by GitHub
parent 4a66e6888d
commit a1a902dbc7
3 changed files with 69 additions and 10 deletions
+14 -9
View File
@@ -13,6 +13,8 @@ use Pest\TestSuite;
*/
final class SnapshotRepository
{
private const int MAX_FILENAME_BYTES = 255;
private static ?string $key = null;
private static int $ordinal = 0;
@@ -95,15 +97,18 @@ final class SnapshotRepository
$relativePath = substr($testFile, strlen($startPath));
$relativePath = substr($relativePath, 0, (int) strrpos($relativePath, '.'));
return new Snapshot(
sprintf(
'%s/%s%s.snap',
$this->testsPath.'/'.$this->snapshotsPath.$relativePath,
TestSuite::getInstance()->getDescription(),
$suffix,
),
dirname($this->testsPath).'/',
);
$directory = $this->testsPath.'/'.$this->snapshotsPath.$relativePath;
$description = TestSuite::getInstance()->getDescription().$suffix;
$extension = '.snap';
$filename = $description.$extension;
if (strlen($filename) > self::MAX_FILENAME_BYTES && ! is_file($directory.'/'.$filename)) {
$digest = '__'.hash('xxh128', $description);
$prefix = mb_strcut($description, 0, self::MAX_FILENAME_BYTES - strlen($digest.$extension), 'UTF-8');
$filename = $prefix.$digest.$extension;
}
return new Snapshot($directory.'/'.$filename, dirname($this->testsPath).'/');
}
private function synchronize(): void
+4 -1
View File
@@ -401,6 +401,9 @@
✓ it works with nested properties
✓ it works with higher order tests
PASS Tests\Features\Expect\LongSnapshotNames
✓ creates and matches snapshots with long dataset labels
PASS Tests\Features\Expect\SnapshotWriteFailure
✓ reports a missing snapshot that cannot be written
@@ -2526,4 +2529,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, 1832 passed (4156 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1833 passed (4181 assertions)
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
use Tests\Fixtures\Tia\Project;
afterEach(function (): void {
Project::destroyAll();
});
test('creates and matches snapshots with long dataset labels', function (): void {
$project = Project::withoutGit();
$testFile = 'tests/Unit/SnapshotNamesTest.php';
$snapshotDirectory = $project->path('tests/.pest/snapshots/Unit/SnapshotNamesTest');
$project->write($testFile, <<<'FIXTURE'
<?php
$prefix = str_repeat('日本', 135);
test('stores a snapshot', function (string $value): void {
expect($value)->toMatchSnapshot()
->and(strtoupper($value))->toMatchSnapshot(as: 'upper');
})->with([
'short' => ['short'],
$prefix.' one' => ['one'],
$prefix.' two' => ['two'],
]);
FIXTURE);
$first = $project->pest($testFile, '--update-snapshots');
$snapshots = glob($snapshotDirectory.'/*.snap');
expect($first->exitCode)->toBe(0, $first->describe())
->and($snapshots)->toHaveCount(6)
->and(array_map(file_get_contents(...), $snapshots))->toEqualCanonicalizing(['short', 'SHORT', 'one', 'ONE', 'two', 'TWO'])
->and($snapshotDirectory.'/stores_a_snapshot_with_data_set__dataset__short__.snap')->toBeFile()
->and($snapshotDirectory.'/stores_a_snapshot_with_data_set__dataset__short____upper.snap')->toBeFile();
foreach ($snapshots as $snapshot) {
$filename = basename($snapshot);
expect(strlen($filename))->toBeLessThanOrEqual(255)
->and(mb_check_encoding($filename, 'UTF-8'))->toBeTrue();
}
$second = $project->pestWithEnvironment($project->path(), ['CI' => 'true'], $testFile);
expect($second->exitCode)->toBe(0, $second->describe())
->and($second->tally())->toContain('3 passed');
})->group('integration');