diff --git a/src/Composer/Repository/FilesystemRepository.php b/src/Composer/Repository/FilesystemRepository.php index 71a8fa6fe..6b44d97d9 100644 --- a/src/Composer/Repository/FilesystemRepository.php +++ b/src/Composer/Repository/FilesystemRepository.php @@ -159,8 +159,13 @@ class FilesystemRepository extends WritableArrayRepository } else { $lines .= "array(),\n"; } - } elseif ($key === 'install_path') { - $lines .= "__DIR__ . " . var_export('/' . $value, true) . ",\n"; + } elseif ($key === 'install_path' && is_string($value)) { + $fs = new Filesystem(); + if ($fs->isAbsolutePath($value)) { + $lines .= var_export($value, true) . ",\n"; + } else { + $lines .= "__DIR__ . " . var_export('/' . $value, true) . ",\n"; + } } else { $lines .= var_export($value, true) . ",\n"; } diff --git a/tests/Composer/Test/InstalledVersionsTest.php b/tests/Composer/Test/InstalledVersionsTest.php index 80947fc02..d48cdf09a 100644 --- a/tests/Composer/Test/InstalledVersionsTest.php +++ b/tests/Composer/Test/InstalledVersionsTest.php @@ -52,6 +52,7 @@ class InstalledVersionsTest extends TestCase 'foo/impl', 'foo/impl2', 'foo/replaced', + 'meta/package', ); $this->assertSame($names, InstalledVersions::getInstalledPackages()); } @@ -74,6 +75,7 @@ class InstalledVersionsTest extends TestCase array(true, '__root__'), array(true, 'b/replacer'), array(false, 'not/there'), + array(true, 'meta/package'), ); } @@ -246,8 +248,8 @@ class InstalledVersionsTest extends TestCase public function testGetInstallPath() { - $this->assertSame($this->root . '/./', \Composer\InstalledVersions::getInstallPath('__root__')); - $this->assertSame($this->root . '/foo/bar/vendor/woop/woop', \Composer\InstalledVersions::getInstallPath('c/c')); + $this->assertSame(realpath($this->root), realpath(\Composer\InstalledVersions::getInstallPath('__root__'))); + $this->assertSame('/foo/bar/vendor/c/c', \Composer\InstalledVersions::getInstallPath('c/c')); $this->assertNull(\Composer\InstalledVersions::getInstallPath('foo/impl')); } } diff --git a/tests/Composer/Test/Repository/FilesystemRepositoryTest.php b/tests/Composer/Test/Repository/FilesystemRepositoryTest.php index 41d6ff0c1..da754661b 100644 --- a/tests/Composer/Test/Repository/FilesystemRepositoryTest.php +++ b/tests/Composer/Test/Repository/FilesystemRepositoryTest.php @@ -12,6 +12,7 @@ namespace Composer\Test\Repository; +use Composer\Package\RootPackageInterface; use Composer\Repository\FilesystemRepository; use Composer\Test\TestCase; use Composer\Json\JsonFile; @@ -157,12 +158,34 @@ class FilesystemRepositoryTest extends TestCase $pkg = $this->getPackage('c/c', '3.0'); $repository->addPackage($pkg); + $pkg = $this->getPackage('meta/package', '3.0'); + $pkg->setType('metapackage'); + $repository->addPackage($pkg); + $im = $this->getMockBuilder('Composer\Installer\InstallationManager') ->disableOriginalConstructor() ->getMock(); $im->expects($this->any()) ->method('getInstallPath') - ->will($this->returnValue('/foo/bar/vendor/woop/woop')); + ->will($this->returnCallback(function ($package) use ($dir) { + // check for empty paths handling + if ($package->getType() === 'metapackage') { + return ''; + } + + if ($package->getName() === 'c/c') { + // check for absolute paths + return '/foo/bar/vendor/c/c'; + } + + // check for cwd + if ($package instanceof RootPackageInterface) { + return $dir; + } + + // check for relative paths + return 'vendor/'.$package->getName(); + })); $repository->write(true, $im); $this->assertSame(require __DIR__.'/Fixtures/installed.php', require $dir.'/installed.php'); diff --git a/tests/Composer/Test/Repository/Fixtures/installed.php b/tests/Composer/Test/Repository/Fixtures/installed.php index a66c75e50..fb9870def 100644 --- a/tests/Composer/Test/Repository/Fixtures/installed.php +++ b/tests/Composer/Test/Repository/Fixtures/installed.php @@ -42,7 +42,7 @@ return array( 'version' => '1.1.0.0', 'type' => 'library', // @phpstan-ignore-next-line - 'install_path' => $dir . '/foo/bar/vendor/woop/woop', + 'install_path' => $dir . '/vendor/a/provider', 'aliases' => array(), 'reference' => 'distref-as-no-source', 'dev_requirement' => false, @@ -52,7 +52,7 @@ return array( 'version' => '1.2.0.0', 'type' => 'library', // @phpstan-ignore-next-line - 'install_path' => $dir . '/foo/bar/vendor/woop/woop', + 'install_path' => $dir . '/vendor/a/provider2', 'aliases' => array( '1.4', ), @@ -64,7 +64,7 @@ return array( 'version' => '2.2.0.0', 'type' => 'library', // @phpstan-ignore-next-line - 'install_path' => $dir . '/foo/bar/vendor/woop/woop', + 'install_path' => $dir . '/vendor/b/replacer', 'aliases' => array(), 'reference' => null, 'dev_requirement' => false, @@ -73,8 +73,7 @@ return array( 'pretty_version' => '3.0', 'version' => '3.0.0.0', 'type' => 'library', - // @phpstan-ignore-next-line - 'install_path' => $dir . '/foo/bar/vendor/woop/woop', + 'install_path' => '/foo/bar/vendor/c/c', 'aliases' => array(), 'reference' => null, 'dev_requirement' => true, @@ -103,5 +102,14 @@ return array( '^3.0', ), ), + 'meta/package' => array( + 'pretty_version' => '3.0', + 'version' => '3.0.0.0', + 'type' => 'metapackage', + 'install_path' => null, + 'aliases' => array(), + 'reference' => null, + 'dev_requirement' => false, + ) ), );