Skip to content
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/PhpParser/Printer/FormatPerservingPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function printToFile(SmartFileInfo $fileInfo, array $newStmts, array $old
{
$newContent = $this->printToString($newStmts, $oldStmts, $oldTokens);

FileSystem::write($fileInfo->getRealPath(), $newContent);
FileSystem::write($fileInfo->getRealPath(), $newContent, $fileInfo->getPerms());

return $newContent;
}
Expand Down
42 changes: 42 additions & 0 deletions tests/PhpParser/Printer/FormatPerservingPrinterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

namespace Rector\Tests\PhpParser\Printer;

use Nette\Utils\FileSystem;
use Rector\HttpKernel\RectorKernel;
use Rector\PhpParser\Printer\FormatPerservingPrinter;
use Symplify\PackageBuilder\FileSystem\SmartFileInfo;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;

final class FormatPerservingPrinterTest extends AbstractKernelTestCase
{
/**
* @var FormatPerservingPrinter
*/
private $formatPerservingPrinter;

protected function setUp(): void
{
$this->bootKernel(RectorKernel::class);
$this->formatPerservingPrinter = self::$container->get(FormatPerservingPrinter::class);
}

protected function tearDown(): void
{
FileSystem::delete(__DIR__ . '/Fixture');
}

public function testFileModeIsPreserved(): void
{
mkdir(__DIR__ . '/Fixture');
touch(__DIR__ . '/Fixture/file.php');
$expectedFilemod = 0755;
chmod(__DIR__ . '/Fixture/file.php', $expectedFilemod);

$fileInfo = new SmartFileInfo(__DIR__ . '/Fixture/file.php');

$this->formatPerservingPrinter->printToFile($fileInfo, [], [], []);

$this->assertSame($expectedFilemod, fileperms(__DIR__ . '/Fixture/file.php') & 0777);
}
}