Skip to content

Commit

Permalink
Merge e4834f6 into ed4df24
Browse files Browse the repository at this point in the history
  • Loading branch information
0x450x6c committed Jun 20, 2020
2 parents ed4df24 + e4834f6 commit 4d9276a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/PackageVersions/Installer.php
Expand Up @@ -22,6 +22,7 @@
use function dirname;
use function file_exists;
use function file_put_contents;
use function is_writable;
use function iterator_to_array;
use function rename;
use function sprintf;
Expand Down Expand Up @@ -145,12 +146,24 @@ private static function writeVersionClassToFile(string $versionClassSource, Comp
$installPath = self::locateRootPackageInstallPath($composer->getConfig(), $composer->getPackage())
. '/src/PackageVersions/Versions.php';

if (! file_exists(dirname($installPath))) {
$installDir = dirname($installPath);
if (! file_exists($installDir)) {
$io->write('<info>ocramius/package-versions:</info> Package not found (probably scheduled for removal); generation of version class skipped.');

return;
}

if (! is_writable($installDir)) {
$io->write(
sprintf(
'<info>ocramius/package-versions:</info> %s is not writable; generation of version class skipped.',
$installDir
)
);

return;
}

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

$installPathTmp = $installPath . '_' . uniqid('tmp', true);
Expand Down
45 changes: 45 additions & 0 deletions test/PackageVersionsTest/E2EInstallerTest.php
Expand Up @@ -14,13 +14,15 @@
use function array_map;
use function array_walk;
use function chdir;
use function chmod;
use function escapeshellarg;
use function exec;
use function file_get_contents;
use function file_put_contents;
use function getcwd;
use function in_array;
use function is_dir;
use function is_writable;
use function iterator_to_array;
use function json_decode;
use function json_encode;
Expand Down Expand Up @@ -184,6 +186,45 @@ public function testRemovingPluginWithNoDevDoesNotAttemptToGenerateVersions() :
);
}

public function testOnReadonlyFilesystemDoesNotGenerateClasses() : void
{
$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('install', $this->tempLocalComposerHome);

$versionsDir = $this->tempLocalComposerHome . '/vendor/ocramius/package-versions/src/PackageVersions';

$versionsFilePath = $versionsDir . '/Versions.php';

file_put_contents($versionsFilePath, 'NOT PHP!');

chmod($versionsFilePath, 0400);
chmod($versionsDir, 0400);

$this->execComposerInDir('update', $this->tempLocalComposerHome);

chmod($versionsDir, 0700);
chmod($versionsFilePath, 0600);

self::assertSame('NOT PHP!', file_get_contents($versionsFilePath));
}

/**
* @group 101
*/
Expand Down Expand Up @@ -325,6 +366,10 @@ private function execComposerInDir(string $command, string $dir) : array

private function rmDir(string $directory) : void
{
if (! is_writable($directory)) {
chmod($directory, 0700);
}

if (! is_dir($directory)) {
unlink($directory);

Expand Down
55 changes: 55 additions & 0 deletions test/PackageVersionsTest/InstallerTest.php
Expand Up @@ -142,6 +142,61 @@ public function testDumpVersionsClassIfExistingFileIsNotWritable() : void
$this->rmDir($vendorDir);
}

public function testDumpVersionsClassIfReadonlyFilesystem() : 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, 0700, true);

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

$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
));

chmod($expectedPath, 0700);
chmod($expectedFileName, 0600);

self::assertSame('NOT 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 4d9276a

Please sign in to comment.