Skip to content

Commit

Permalink
fix(filesystem): call clearstatcache after writing to a file to reset…
Browse files Browse the repository at this point in the history
… file stat (#262)
  • Loading branch information
dragosprotung authored Nov 9, 2021
1 parent 9fdb771 commit de6f130
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Psl/Filesystem/Internal/write_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Psl\Internal;
use Psl\Str;

use function clearstatcache;
use function file_put_contents;

use const FILE_APPEND;
Expand Down Expand Up @@ -64,4 +65,6 @@ function write_file(string $file, string $content, bool $append): void
));
}
// @codeCoverageIgnoreEnd

clearstatcache();
}
15 changes: 15 additions & 0 deletions tests/unit/Filesystem/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ public function testWriteFile(): void
Filesystem\delete_file($file);
}

public function testWriteFileClearsFileStat(): void
{
$file = Filesystem\create_temporary_file();

Filesystem\write_file($file, 'Hello');

static::assertSame(5, Filesystem\file_size($file));

Filesystem\append_file($file, ', World!');

static::assertSame(13, Filesystem\file_size($file));

Filesystem\delete_file($file);
}

public function testWriteFileThrowsForDirectories(): void
{
$this->expectException(InvariantViolationException::class);
Expand Down

0 comments on commit de6f130

Please sign in to comment.