diff --git a/src/PackageVersions/Installer.php b/src/PackageVersions/Installer.php index bead63b..579042d 100644 --- a/src/PackageVersions/Installer.php +++ b/src/PackageVersions/Installer.php @@ -89,16 +89,7 @@ public static function dumpVersionsClass(Event $composerEvent) return; } - $io = $composerEvent->getIO(); - $io->write('ocramius/package-versions: Generating version class...'); - - self::writeVersionClassToFile( - self::generateVersionsClass($versions), - $composer->getConfig(), - $composer->getPackage() - ); - - $io->write('ocramius/package-versions: ...done generating version class'); + self::writeVersionClassToFile(self::generateVersionsClass($versions), $composer, $composerEvent->getIO()); } private static function generateVersionsClass(array $versions) : string @@ -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('ocramius/package-versions: Package not found (probably scheduled for removal); generation of version class skipped.'); + + return; + } + + $io->write('ocramius/package-versions: Generating version class...'); + file_put_contents($installPath, $versionClassSource); chmod($installPath, 0664); + + $io->write('ocramius/package-versions: ...done generating version class'); } /** diff --git a/test/PackageVersionsTest/E2EInstallerTest.php b/test/PackageVersionsTest/E2EInstallerTest.php index 8694edd..61af7a2 100644 --- a/test/PackageVersionsTest/E2EInstallerTest.php +++ b/test/PackageVersionsTest/E2EInstallerTest.php @@ -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(); diff --git a/test/PackageVersionsTest/InstallerTest.php b/test/PackageVersionsTest/InstallerTest.php index 3736c7e..01d0a1d 100644 --- a/test/PackageVersionsTest/InstallerTest.php +++ b/test/PackageVersionsTest/InstallerTest.php @@ -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')) {