- Implement cache size limit of 8 entries and age limit of 30 days - Add markUsedAndPrune method to handle cache cleanup when accessing entries - Create getCacheEntries method to retrieve and sort cache entries by modification time - Implement removeCacheDirectory method for recursive directory deletion - Add unit tests covering expired and excess cache entry pruning scenarios - Include cstring header in global headers and remove duplicate inclusion in generated code - Update version from 1097 to 1098pull/43/head
parent
21291453f8
commit
682b17f73f
5 changed files with 148 additions and 3 deletions
@ -0,0 +1,88 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace TypePhp\Tests\Build; |
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase; |
||||||
|
use TypePhp\Backend\CompilerBackend; |
||||||
|
use TypePhp\Build\CompileOptions; |
||||||
|
use TypePhp\Build\NativeBuilder; |
||||||
|
use TypePhp\Build\PrecompiledHeaderManager; |
||||||
|
|
||||||
|
final class PrecompiledHeaderManagerTest extends TestCase |
||||||
|
{ |
||||||
|
private string $cacheDirectory; |
||||||
|
|
||||||
|
protected function setUp(): void |
||||||
|
{ |
||||||
|
$this->cacheDirectory = sys_get_temp_dir() . '/typephp_pch_' . bin2hex(random_bytes(8)); |
||||||
|
mkdir($this->cacheDirectory, 0777, true); |
||||||
|
} |
||||||
|
|
||||||
|
protected function tearDown(): void |
||||||
|
{ |
||||||
|
$this->removeDirectory($this->cacheDirectory); |
||||||
|
} |
||||||
|
|
||||||
|
public function testPreparePrunesExpiredAndExcessCacheEntries(): void |
||||||
|
{ |
||||||
|
$oldDirectory = $this->createCacheEntry(1, time() - 31 * 86400); |
||||||
|
for ($i = 2; $i <= 10; $i++) { |
||||||
|
$this->createCacheEntry($i, time() - (10 - $i)); |
||||||
|
} |
||||||
|
$unmanagedDirectory = $this->cacheDirectory . '/keep-me'; |
||||||
|
mkdir($unmanagedDirectory); |
||||||
|
|
||||||
|
$backend = $this->createMock(CompilerBackend::class); |
||||||
|
$backend->method('supportsPrecompiledHeaders')->willReturn(true); |
||||||
|
$backend->method('getName')->willReturn('test'); |
||||||
|
$backend->method('getCompilerCommand')->willReturn('true'); |
||||||
|
$backend->method('getPrecompiledHeaderArtifact') |
||||||
|
->willReturnCallback(static fn(string $header): string => $header . '.gch'); |
||||||
|
$backend->method('buildNativeCompileCommand') |
||||||
|
->willReturnCallback( |
||||||
|
static fn(string $source, string $object): string => 'touch ' . escapeshellarg($object), |
||||||
|
); |
||||||
|
|
||||||
|
$result = (new PrecompiledHeaderManager($backend, new NativeBuilder($backend)))->prepare( |
||||||
|
['cstring', 'phpx.h'], |
||||||
|
[], |
||||||
|
$this->cacheDirectory, |
||||||
|
new CompileOptions([]), |
||||||
|
); |
||||||
|
|
||||||
|
$this->assertFileExists($result['artifact']); |
||||||
|
$this->assertStringContainsString("#include <cstring>\n#include <phpx.h>\n", file_get_contents($result['header'])); |
||||||
|
$this->assertDirectoryDoesNotExist($oldDirectory); |
||||||
|
$this->assertDirectoryExists($unmanagedDirectory); |
||||||
|
|
||||||
|
$managedEntries = array_filter( |
||||||
|
scandir($this->cacheDirectory), |
||||||
|
static fn(string $entry): bool => preg_match('/^[a-f0-9]{24}$/D', $entry) === 1, |
||||||
|
); |
||||||
|
$this->assertCount(8, $managedEntries); |
||||||
|
} |
||||||
|
|
||||||
|
private function createCacheEntry(int $number, int $mtime): string |
||||||
|
{ |
||||||
|
$directory = $this->cacheDirectory . '/' . sprintf('%024x', $number); |
||||||
|
mkdir($directory); |
||||||
|
file_put_contents($directory . '/typephp_pch.hpp.gch', 'cached'); |
||||||
|
touch($directory, $mtime); |
||||||
|
return $directory; |
||||||
|
} |
||||||
|
|
||||||
|
private function removeDirectory(string $directory): void |
||||||
|
{ |
||||||
|
if (!is_dir($directory)) { |
||||||
|
return; |
||||||
|
} |
||||||
|
$iterator = new \RecursiveIteratorIterator( |
||||||
|
new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS), |
||||||
|
\RecursiveIteratorIterator::CHILD_FIRST, |
||||||
|
); |
||||||
|
foreach ($iterator as $entry) { |
||||||
|
$entry->isDir() ? rmdir($entry->getPathname()) : unlink($entry->getPathname()); |
||||||
|
} |
||||||
|
rmdir($directory); |
||||||
|
} |
||||||
|
} |
||||||
@ -1 +1 @@ |
|||||||
1097 |
1098 |
||||||
Loading…
Reference in new issue