Skip to content

Commit

Permalink
Create cache dir if non existing (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Jan 27, 2025
1 parent 0ab7a81 commit 767b2c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Cache/FileHandler/CacheFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function write(Cache $cache): void
} else {
$dir = \dirname($this->file);

if (!is_dir($dir)) {
if (!is_dir($dir) && !@mkdir($dir, recursive: true)) {
throw CannotWriteCacheException::missingDirectory($this->file);
}

Expand Down
18 changes: 16 additions & 2 deletions tests/Cache/FileHandler/CacheFileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public function testReadSuccess(): void

public function testWriteFailureMissingDirectory(): void
{
$cacheFileHandler = new CacheFileHandler('/fakeDir/foo.php');
$cacheFileHandler = new CacheFileHandler('/fake:Dir/foo.php');

$this->expectExceptionObject(CannotWriteCacheException::missingDirectory('/fakeDir/foo.php'));
$this->expectExceptionObject(CannotWriteCacheException::missingDirectory('/fake:Dir/foo.php'));
$cacheFileHandler->write(new Cache(new Signature('8.0', '1', [])));
}

Expand Down Expand Up @@ -97,4 +97,18 @@ public function testWriteSuccess(): void
static::assertNotFalse($permissions);
static::assertSame('0666', substr(\sprintf('%o', $permissions), -4));
}

public function testWriteSuccessMissingDirectory(): void
{
$file = $this->getTmpPath(__DIR__.'/Fixtures/fakeDir/foo.php');
$cacheFileHandler = new CacheFileHandler($file);

$cacheFileHandler->write(new Cache(new Signature('8.0', '1', [])));
$content = file_get_contents($file);
static::assertSame('{"php_version":"8.0","fixer_version":"1","rules":[],"hashes":[]}', $content);

$permissions = fileperms($file);
static::assertNotFalse($permissions);
static::assertSame('0666', substr(\sprintf('%o', $permissions), -4));
}
}

0 comments on commit 767b2c8

Please sign in to comment.