1
0
Fork 0

Merge pull request #142 from Seldaek/autoload_test

Autoload test
pull/143/head
Nils Adermann 2011-12-04 13:44:59 -08:00
commit a697488948
7 changed files with 216 additions and 43 deletions

View File

@ -17,6 +17,7 @@ use Composer\Json\JsonFile;
use Composer\Package\Loader\JsonLoader; use Composer\Package\Loader\JsonLoader;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Repository\RepositoryInterface; use Composer\Repository\RepositoryInterface;
use Composer\Downloader\Util\Filesystem;
/** /**
* @author Igor Wiedler <igor@wiedler.ch> * @author Igor Wiedler <igor@wiedler.ch>
@ -27,7 +28,6 @@ class AutoloadGenerator
public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir) public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir)
{ {
$autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php'); $autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php');
$autoloadFile .= <<<'EOF' $autoloadFile .= <<<'EOF'
// autoload.php generated by Composer // autoload.php generated by Composer
@ -49,12 +49,17 @@ function init() {
return init(); return init();
EOF; EOF;
$namespacesFile = <<<'EOF' $filesystem = new Filesystem();
$vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
$relVendorPath = ltrim(substr($vendorPath, strlen(getcwd())), '/');
$vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
$namespacesFile = <<<EOF
<?php <?php
// autoload_namespace.php generated by Composer // autoload_namespace.php generated by Composer
$baseDir = dirname(__DIR__); \$vendorDir = $vendorDirCode;
return array( return array(
@ -71,26 +76,26 @@ EOF;
// add main package // add main package
$packageMap[] = array($mainPackage, ''); $packageMap[] = array($mainPackage, '');
$autoloads = $this->parseAutoloads($packageMap); $autoloads = $this->parseAutoloads($packageMap);
$vendorPath = $installationManager->getVendorPath();
$realVendorPath = realpath($vendorPath); $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
$vendorPathDepth = substr_count(strtr(substr($realVendorPath, strlen(getcwd())), '\\', '/'), '/'); $appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir);
$appBaseDir = str_repeat('dirname(', $vendorPathDepth).'$baseDir'.str_repeat(')', $vendorPathDepth);
if (isset($autoloads['psr-0'])) { if (isset($autoloads['psr-0'])) {
foreach ($autoloads['psr-0'] as $def) { foreach ($autoloads['psr-0'] as $def) {
if (!$this->isAbsolutePath($def['path'])) { $def['path'] = strtr($def['path'], '\\', '/');
if (strpos($def['path'], $vendorPath) === 0) { $baseDir = '';
$def['path'] = substr($def['path'], strlen($vendorPath)); if (!$filesystem->isAbsolutePath($def['path'])) {
$baseDir = '$baseDir . '; if (strpos($def['path'], $relVendorPath) === 0) {
$def['path'] = substr($def['path'], strlen($relVendorPath));
$baseDir = '$vendorDir . ';
} else { } else {
$def['path'] = '/'.$def['path']; $def['path'] = '/'.$def['path'];
$baseDir = $appBaseDir . ' . '; $baseDir = $appBaseDir . ' . ';
} }
} else { } elseif (strpos($def['path'], $vendorPath) === 0) {
$baseDir = ''; $def['path'] = substr($def['path'], strlen($vendorPath));
$baseDir = '$vendorDir . ';
} }
$exportedPrefix = var_export($def['namespace'], true); $exportedPrefix = var_export($def['namespace'], true);
$exportedPath = var_export($def['path'], true); $exportedPath = var_export($def['path'], true);
@ -158,9 +163,4 @@ EOF;
return $loader; return $loader;
} }
protected function isAbsolutePath($path)
{
return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':';
}
} }

View File

@ -58,22 +58,22 @@ class Filesystem
if (dirname($from) === dirname($to)) { if (dirname($from) === dirname($to)) {
return './'.basename($to); return './'.basename($to);
} }
$from = strtr($from, '\\', '/'); $from = rtrim(strtr($from, '\\', '/'), '/');
$to = strtr($to, '\\', '/'); $to = rtrim(strtr($to, '\\', '/'), '/');
$commonPath = dirname($to); $commonPath = $to;
while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/$}i', $commonPath)) { while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) {
$commonPath = strtr(dirname($commonPath), '\\', '/'); $commonPath = strtr(dirname($commonPath), '\\', '/');
} }
if (0 !== strpos($from, $commonPath) || '/' === $commonPath) { if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) {
return $to; return $to;
} }
$commonPath = rtrim($commonPath, '/') . '/'; $commonPath = rtrim($commonPath, '/') . '/';
$sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/'); $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/');
$commonPathCode = str_repeat('../', $sourcePathDepth); $commonPathCode = str_repeat('../', $sourcePathDepth);
return $commonPathCode . substr($to, strlen($commonPath)); return ($commonPathCode . substr($to, strlen($commonPath))) ?: './';
} }
/** /**
@ -81,33 +81,38 @@ class Filesystem
* *
* @param string $from * @param string $from
* @param string $to * @param string $to
* @param Boolean $directories if true, the source/target are considered to be directories
* @return string * @return string
*/ */
public function findShortestPathCode($from, $to) public function findShortestPathCode($from, $to, $directories = false)
{ {
if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) { if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) {
throw new \InvalidArgumentException('from and to must be absolute paths'); throw new \InvalidArgumentException('from and to must be absolute paths');
} }
if ($from === $to) { if ($from === $to) {
return '__FILE__'; return $directories ? '__DIR__' : '__FILE__';
} }
$from = strtr($from, '\\', '/'); $from = strtr($from, '\\', '/');
$to = strtr($to, '\\', '/'); $to = strtr($to, '\\', '/');
$commonPath = dirname($to); $commonPath = $to;
while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/$}i', $commonPath)) { while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) {
$commonPath = strtr(dirname($commonPath), '\\', '/'); $commonPath = strtr(dirname($commonPath), '\\', '/');
} }
if (0 !== strpos($from, $commonPath) || '/' === $commonPath) { if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) {
return var_export($to, true); return var_export($to, true);
} }
$commonPath = rtrim($commonPath, '/') . '/'; $commonPath = rtrim($commonPath, '/') . '/';
$sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/'); if (strpos($to, $from.'/') === 0) {
return '__DIR__ . '.var_export(substr($to, strlen($from)), true);
}
$sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') + $directories;
$commonPathCode = str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth); $commonPathCode = str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth);
return $commonPathCode . '.' . var_export('/' . substr($to, strlen($commonPath)), true); $relTarget = substr($to, strlen($commonPath));
return $commonPathCode . (strlen($relTarget) ? '.' . var_export('/' . $relTarget, true) : '');
} }
/** /**

View File

@ -0,0 +1,118 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test\Installer;
use Composer\Autoload\AutoloadGenerator;
use Composer\Downloader\Util\Filesystem;
use Composer\Package\MemoryPackage;
class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase
{
public $vendorDir;
private $workingDir;
private $im;
private $repository;
private $generator;
protected function setUp()
{
$fs = new Filesystem;
$that = $this;
$this->workingDir = sys_get_temp_dir();
$this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload';
if (is_dir($this->vendorDir)) {
$fs->removeDirectory($this->vendorDir);
}
mkdir($this->vendorDir);
$this->dir = getcwd();
chdir($this->workingDir);
$this->im = $this->getMockBuilder('Composer\Installer\InstallationManager')
->disableOriginalConstructor()
->getMock();
$this->im->expects($this->any())
->method('getInstallPath')
->will($this->returnCallback(function ($package) use ($that) {
return $that->vendorDir.'/'.$package->getName();
}));
$this->im->expects($this->any())
->method('getVendorPath')
->will($this->returnCallback(function () use ($that) {
return $that->vendorDir;
}));
$this->repo = $this->getMock('Composer\Repository\RepositoryInterface');
$this->generator = new AutoloadGenerator();
}
protected function tearDown()
{
chdir($this->dir);
}
public function testMainPackageAutoloading()
{
$package = new MemoryPackage('a', '1.0', '1.0');
$package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/')));
$this->repo->expects($this->once())
->method('getPackages')
->will($this->returnValue(array()));
mkdir($this->vendorDir.'/.composer');
$this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
$this->assertAutoloadFiles('main', $this->vendorDir.'/.composer');
}
public function testMainPackageAutoloadingAlternativeVendorDir()
{
$package = new MemoryPackage('a', '1.0', '1.0');
$package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/')));
$this->repo->expects($this->once())
->method('getPackages')
->will($this->returnValue(array()));
$this->vendorDir .= '/subdir';
mkdir($this->vendorDir.'/.composer', 0777, true);
$this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
$this->assertAutoloadFiles('main2', $this->vendorDir.'/.composer');
}
public function testVendorsAutoloading()
{
$package = new MemoryPackage('a', '1.0', '1.0');
$packages = array();
$packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
$packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
$a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/')));
$b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/')));
$this->repo->expects($this->once())
->method('getPackages')
->will($this->returnValue($packages));
mkdir($this->vendorDir.'/.composer', 0777, true);
$this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer');
$this->assertAutoloadFiles('vendors', $this->vendorDir.'/.composer');
}
private function assertAutoloadFiles($name, $dir)
{
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_namespaces.php');
}
}

View File

@ -0,0 +1,10 @@
<?php
// autoload_namespace.php generated by Composer
$vendorDir = dirname(__DIR__);
return array(
'Main' => dirname($vendorDir) . '/src/',
'Lala' => dirname($vendorDir) . '/src/',
);

View File

@ -0,0 +1,10 @@
<?php
// autoload_namespace.php generated by Composer
$vendorDir = dirname(__DIR__);
return array(
'Main' => dirname(dirname($vendorDir)) . '/src/',
'Lala' => dirname(dirname($vendorDir)) . '/src/',
);

View File

@ -0,0 +1,11 @@
<?php
// autoload_namespace.php generated by Composer
$vendorDir = dirname(__DIR__);
return array(
'B\\Sub\\Name' => $vendorDir . '/b/b/src/',
'A\\B' => $vendorDir . '/a/a/lib/',
'A' => $vendorDir . '/a/a/src/',
);

View File

@ -20,24 +20,37 @@ class FilesystemTest extends TestCase
/** /**
* @dataProvider providePathCouplesAsCode * @dataProvider providePathCouplesAsCode
*/ */
public function testFindShortestPathCode($a, $b, $expected) public function testFindShortestPathCode($a, $b, $directory, $expected)
{ {
$fs = new Filesystem; $fs = new Filesystem;
$this->assertEquals($expected, $fs->findShortestPathCode($a, $b)); $this->assertEquals($expected, $fs->findShortestPathCode($a, $b, $directory));
} }
public function providePathCouplesAsCode() public function providePathCouplesAsCode()
{ {
return array( return array(
array('/foo/bar', '/foo/bar', "__FILE__"), array('/foo/bar', '/foo/bar', false, "__FILE__"),
array('/foo/bar', '/foo/baz', "__DIR__.'/baz'"), array('/foo/bar', '/foo/baz', false, "__DIR__.'/baz'"),
array('/foo/bin/run', '/foo/vendor/acme/bin/run', "dirname(__DIR__).'/vendor/acme/bin/run'"), array('/foo/bin/run', '/foo/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
array('/foo/bin/run', '/foo/vendor/acme/bin/run', "dirname(__DIR__).'/vendor/acme/bin/run'"), array('/foo/bin/run', '/foo/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
array('/foo/bin/run', '/bar/bin/run', "'/bar/bin/run'"), array('/foo/bin/run', '/bar/bin/run', false, "'/bar/bin/run'"),
array('c:/bin/run', 'c:/vendor/acme/bin/run', "dirname(__DIR__).'/vendor/acme/bin/run'"), array('c:/bin/run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "dirname(__DIR__).'/vendor/acme/bin/run'"), array('c:\\bin\\run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
array('c:/bin/run', 'd:/vendor/acme/bin/run', "'d:/vendor/acme/bin/run'"), array('c:/bin/run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"),
array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "'d:/vendor/acme/bin/run'"), array('c:\\bin\\run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"),
array('/foo/bar', '/foo/bar', true, "__DIR__"),
array('/foo/bar', '/foo/baz', true, "dirname(__DIR__).'/baz'"),
array('/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
array('/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
array('/foo/bin/run', '/bar/bin/run', true, "'/bar/bin/run'"),
array('c:/bin/run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
array('c:\\bin\\run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
array('c:/bin/run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
array('c:\\bin\\run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
array('C:/Temp/test', 'C:\Temp', true, "dirname(__DIR__)"),
array('C:/Temp', 'C:\Temp\test', true, "__DIR__ . '/test'"),
array('/tmp/test', '/tmp', true, "dirname(__DIR__)"),
array('/tmp', '/tmp/test', true, "__DIR__ . '/test'"),
); );
} }
@ -62,6 +75,12 @@ class FilesystemTest extends TestCase
array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"), array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"), array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"), array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
array('C:/Temp/test', 'C:\Temp', "./"),
array('/tmp/test', '/tmp', "./"),
array('C:/Temp/test/sub', 'C:\Temp', "../"),
array('/tmp/test/sub', '/tmp', "../"),
array('/tmp', '/tmp/test', "test"),
array('C:/Temp', 'C:\Temp\test', "test"),
); );
} }
} }