Skip to content

Commit

Permalink
Merge branch 'fix/#41-#46-avoid-generating-when-package-scheduled-for…
Browse files Browse the repository at this point in the history
…-removal'

Close #41
Close #46
  • Loading branch information
Ocramius committed Sep 6, 2017
2 parents e9cea37 + 13a5686 commit c094b15
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/PackageVersions/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,7 @@ public static function dumpVersionsClass(Event $composerEvent)
return;
}

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

self::writeVersionClassToFile(
self::generateVersionsClass($versions),
$composer->getConfig(),
$composer->getPackage()
);

$io->write('<info>ocramius/package-versions:</info> ...done generating version class');
self::writeVersionClassToFile(self::generateVersionsClass($versions), $composer, $composerEvent->getIO());
}

private static function generateVersionsClass(array $versions) : string
Expand All @@ -111,24 +102,31 @@ private static function generateVersionsClass(array $versions) : string
}

/**
* @param string $versionClassSource
* @param Config $composerConfig
* @param RootPackageInterface $rootPackage
* @param string $versionClassSource
* @param Composer $composer
* @param IOInterface $io
*
* @return void
*
* @throws \RuntimeException
*/
private static function writeVersionClassToFile(
string $versionClassSource,
Config $composerConfig,
RootPackageInterface $rootPackage
) {
$installPath = self::locateRootPackageInstallPath($composerConfig, $rootPackage)
private static function writeVersionClassToFile(string $versionClassSource, Composer $composer, IOInterface $io)
{
$installPath = self::locateRootPackageInstallPath($composer->getConfig(), $composer->getPackage())
. '/src/PackageVersions/Versions.php';

if (! file_exists(dirname($installPath))) {
$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> Generating version class...');

file_put_contents($installPath, $versionClassSource);
chmod($installPath, 0664);

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

/**
Expand Down
40 changes: 40 additions & 0 deletions test/PackageVersionsTest/E2EInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,46 @@ public function testRemovingPluginDoesNotAttemptToGenerateVersions()
);
}

/**
* @group #41
* @group #46
*/
public function testRemovingPluginWithNoDevDoesNotAttemptToGenerateVersions()
{
$this->createPackageVersionsArtifact();
$this->createArtifact();

$this->writeComposerJsonFile(
[
'name' => 'package-versions/e2e-local',
'require-dev' => [
'ocramius/package-versions' => '1.0.0'
],
'repositories' => [
[
'packagist' => false,
],
[
'type' => 'artifact',
'url' => $this->tempArtifact,
]
]
],
$this->tempLocalComposerHome
);

$this->execComposerInDir('update', $this->tempLocalComposerHome);
$this->assertFileExists(
$this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php'
);

$this->execComposerInDir('install --no-dev', $this->tempLocalComposerHome);

$this->assertFileNotExists(
$this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions/Versions.php'
);
}

private function createPackageVersionsArtifact()
{
$zip = new ZipArchive();
Expand Down
40 changes: 40 additions & 0 deletions test/PackageVersionsTest/InstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,46 @@ public function testVersionsAreNotDumpedIfPackageVersionsNotExplicitlyRequired()
$this->rmDir($vendorDir);
}

/**
* @group #41
* @group #46
*/
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 c094b15

Please sign in to comment.