Skip to content

Commit

Permalink
Avoid issues when the package is scheduled for removal (fixes #41)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean85 committed Sep 4, 2017
1 parent e9cea37 commit 825042b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/PackageVersions/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,17 @@ public static function dumpVersionsClass(Event $composerEvent)
$io = $composerEvent->getIO();
$io->write('<info>ocramius/package-versions:</info> Generating version class...');

self::writeVersionClassToFile(
self::generateVersionsClass($versions),
$composer->getConfig(),
$composer->getPackage()
);
try {
self::writeVersionClassToFile(
self::generateVersionsClass($versions),
$composer->getConfig(),
$composer->getPackage()
);
} catch (NotInstalledException $exception) {
$io->write('<info>ocramius/package-versions:</info> Package not found (probably scheduled for removal); generation of version class skipped.');

return;
}

$io->write('<info>ocramius/package-versions:</info> ...done generating version class');
}
Expand Down Expand Up @@ -127,6 +133,10 @@ private static function writeVersionClassToFile(
$installPath = self::locateRootPackageInstallPath($composerConfig, $rootPackage)
. '/src/PackageVersions/Versions.php';

if (! file_exists(dirname($installPath))) {
throw new NotInstalledException();
}

file_put_contents($installPath, $versionClassSource);
chmod($installPath, 0664);
}
Expand Down
15 changes: 15 additions & 0 deletions src/PackageVersions/NotInstalledException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace PackageVersions;

use Throwable;

class NotInstalledException extends \RuntimeException
{
const MESSAGE = "Folder of ocramius/package-versions not found, it seems that it's not installed";

public function __construct($message = self::MESSAGE, $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
37 changes: 37 additions & 0 deletions test/PackageVersionsTest/InstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,43 @@ public function testVersionsAreNotDumpedIfPackageVersionsNotExplicitlyRequired()
$this->rmDir($vendorDir);
}

public function testVersionsAreNotDumpedIfPackageIsScheduledForRemoval()
{
$config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
$locker = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock();
$package = $this->createMock(RootPackageInterface::class);

$vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true);
$expectedPath = $vendorDir . '/ocramius/package-versions/src/PackageVersions';

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

$this->composer->expects(self::any())->method('getConfig')->willReturn($config);
$this->composer->expects(self::any())->method('getLocker')->willReturn($locker);
$this->composer->expects(self::any())->method('getPackage')->willReturn($package);

$config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir);

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

self::assertFileNotExists($expectedPath . '/Versions.php');
self::assertFileNotExists($expectedPath . '/Versions.php');
}

public function testGeneratedVersionFileAccessRights()
{
if (0 === strpos(\PHP_OS, 'WIN')) {
Expand Down

0 comments on commit 825042b

Please sign in to comment.