Skip to content

Commit

Permalink
Merge pull request #79 from superjobru/chmod-fix
Browse files Browse the repository at this point in the history
Fixed writeVersionClassToFile if file already exists and not writable
  • Loading branch information
Ocramius committed Feb 21, 2019
2 parents e1c5061 + b54f349 commit a4d4b60
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/PackageVersions/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
use function file_exists;
use function file_put_contents;
use function iterator_to_array;
use function rename;
use function sprintf;
use function uniqid;
use function var_export;

final class Installer implements PluginInterface, EventSubscriberInterface
Expand Down Expand Up @@ -137,8 +139,10 @@ private static function writeVersionClassToFile(string $versionClassSource, Comp

$io->write('<info>ocramius/package-versions:</info> Generating version class...');

file_put_contents($installPath, $versionClassSource);
chmod($installPath, 0664);
$installPathTmp = $installPath . '_' . uniqid('tmp', true);
file_put_contents($installPathTmp, $versionClassSource);
chmod($installPathTmp, 0664);
rename($installPathTmp, $installPath);

$io->write('<info>ocramius/package-versions:</info> ...done generating version class');
}
Expand Down
53 changes: 53 additions & 0 deletions test/PackageVersionsTest/InstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
use const PHP_OS;
use function array_filter;
use function array_map;
use function chmod;
use function file_get_contents;
use function file_put_contents;
use function fileperms;
use function in_array;
use function is_dir;
Expand Down Expand Up @@ -93,6 +95,57 @@ public function testGetSubscribedEvents() : void
}
}

public function testDumpVersionsClassIfExistingFileIsNotWritable() : void
{
$config = $this->createMock(Config::class);
$locker = $this->createMock(Locker::class);
$repositoryManager = $this->createMock(RepositoryManager::class);
$installManager = $this->createMock(InstallationManager::class);
$repository = $this->createMock(InstalledRepositoryInterface::class);

$vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true);

$expectedPath = $vendorDir . '/ocramius/package-versions/src/PackageVersions';

/** @noinspection MkdirRaceConditionInspection */
mkdir($expectedPath, 0777, true);

$expectedFileName = $expectedPath . '/Versions.php';
file_put_contents($expectedFileName, 'NOT PHP!');
chmod($expectedFileName, 0444);

$locker
->method('getLockData')
->willReturn([
'packages' => [
[
'name' => 'ocramius/package-versions',
'version' => '1.0.0',
],
],
]);

$repositoryManager->method('getLocalRepository')->willReturn($repository);

$this->composer->method('getConfig')->willReturn($config);
$this->composer->method('getLocker')->willReturn($locker);
$this->composer->method('getRepositoryManager')->willReturn($repositoryManager);
$this->composer->method('getPackage')->willReturn($this->getRootPackageMock());
$this->composer->method('getInstallationManager')->willReturn($installManager);

$config->method('get')->with('vendor-dir')->willReturn($vendorDir);

Installer::dumpVersionsClass(new Event(
'post-install-cmd',
$this->composer,
$this->io
));

self::assertStringStartsWith('<?php', file_get_contents($expectedFileName));

$this->rmDir($vendorDir);
}

public function testDumpVersionsClass() : void
{
$config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
Expand Down

0 comments on commit a4d4b60

Please sign in to comment.