mirror of
https://github.com/pestphp/pest.git
synced 2026-12-03 22:45:06 +01:00
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:
@@ -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
|
||||
|
||||
@@ -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');
|
||||
Reference in New Issue
Block a user