Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create cache dir if non existing #351

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Loading