diff --git a/src/Repositories/SnapshotRepository.php b/src/Repositories/SnapshotRepository.php index 5da6df99..11c89d36 100644 --- a/src/Repositories/SnapshotRepository.php +++ b/src/Repositories/SnapshotRepository.php @@ -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 diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index fce430b7..5777aa80 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -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) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1833 passed (4181 assertions) \ No newline at end of file diff --git a/tests/Features/Expect/LongSnapshotNames.php b/tests/Features/Expect/LongSnapshotNames.php new file mode 100644 index 00000000..476d3860 --- /dev/null +++ b/tests/Features/Expect/LongSnapshotNames.php @@ -0,0 +1,51 @@ +path('tests/.pest/snapshots/Unit/SnapshotNamesTest'); + + $project->write($testFile, <<<'FIXTURE' + 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');