Skip to content

Commit

Permalink
make sure temp files can be cleaned up on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Sep 11, 2024
1 parent 6d29dd9 commit 71fefa2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@ public function dumpFile(string $filename, $content)
$this->rename($tmpFile, $filename, true);
} finally {
if (file_exists($tmpFile)) {
if ('\\' === \DIRECTORY_SEPARATOR && !is_writable($tmpFile)) {
self::box('chmod', $tmpFile, self::box('fileperms', $tmpFile) | 0200);
}

self::box('unlink', $tmpFile);
}
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,22 @@ public function testDumpKeepsExistingPermissionsWhenOverwritingAnExistingFile()
$this->assertFilePermissions(745, $filename);
}

public function testDumpFileCleansUpAfterFailure()
{
$targetFile = $this->workspace.'/dump-file';
$this->filesystem->touch($targetFile);
$this->filesystem->chmod($targetFile, 0444);

try {
$this->filesystem->dumpFile($targetFile, 'any content');
} catch (IOException $e) {
} finally {
$this->filesystem->chmod($targetFile, 0666);
}

$this->assertSame([$targetFile], glob($this->workspace.'/*'));
}

public function testCopyShouldKeepExecutionPermission()
{
$this->markAsSkippedIfChmodIsMissing();
Expand Down

0 comments on commit 71fefa2

Please sign in to comment.