Skip to content

Commit

Permalink
Fix generation of autoload rules in a dir that is missing to ensure i…
Browse files Browse the repository at this point in the history
…t does not break
  • Loading branch information
Seldaek committed Apr 1, 2022
1 parent cbda476 commit ffc6267
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/Composer/Autoload/AutoloadGenerator.php
Expand Up @@ -162,6 +162,7 @@ public function setPlatformRequirementFilter(PlatformRequirementFilterInterface
* @param string $suffix
* @return int
* @throws \Seld\JsonLint\ParsingException
* @throws \RuntimeException
*/
public function dump(Config $config, InstalledRepositoryInterface $localRepo, RootPackageInterface $rootPackage, InstallationManager $installationManager, $targetDir, $scanPsrPackages = false, $suffix = '')
{
Expand Down Expand Up @@ -1289,6 +1290,9 @@ function ($matches) use (&$updir) {
}

$resolvedPath = realpath($installPath . '/' . $updir);
if (false === $resolvedPath) {
continue;
}
$autoloads[] = preg_quote(strtr($resolvedPath, '\\', '/')) . '/' . $path . '($|/)';
continue;
}
Expand Down
105 changes: 92 additions & 13 deletions tests/Composer/Test/Autoload/AutoloadGeneratorTest.php
Expand Up @@ -14,6 +14,7 @@

use Composer\Autoload\AutoloadGenerator;
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory;
use Composer\Package\CompletePackage;
use Composer\Package\Link;
use Composer\Package\Version\VersionParser;
use Composer\Semver\Constraint\Constraint;
Expand Down Expand Up @@ -775,8 +776,8 @@ public function testClassMapAutoloadingEmptyDirAndExactFile()
include $this->vendorDir.'/composer/autoload_classmap.php'
);
$this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap');
$this->assertStringNotContainsString('$loader->setClassMapAuthoritative(true);', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
$this->assertStringNotContainsString('$loader->setApcuPrefix(', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
$this->assertStringNotContainsString('$loader->setClassMapAuthoritative(true);', (string) file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
$this->assertStringNotContainsString('$loader->setApcuPrefix(', (string) file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
}

public function testClassMapAutoloadingAuthoritativeAndApcu()
Expand Down Expand Up @@ -824,8 +825,8 @@ public function testClassMapAutoloadingAuthoritativeAndApcu()
);
$this->assertAutoloadFiles('classmap8', $this->vendorDir.'/composer', 'classmap');

$this->assertStringContainsString('$loader->setClassMapAuthoritative(true);', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
$this->assertStringContainsString('$loader->setApcuPrefix(', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
$this->assertStringContainsString('$loader->setClassMapAuthoritative(true);', (string) file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
$this->assertStringContainsString('$loader->setApcuPrefix(', (string) file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
}

public function testClassMapAutoloadingAuthoritativeAndApcuPrefix()
Expand Down Expand Up @@ -873,8 +874,8 @@ public function testClassMapAutoloadingAuthoritativeAndApcuPrefix()
);
$this->assertAutoloadFiles('classmap8', $this->vendorDir.'/composer', 'classmap');

$this->assertStringContainsString('$loader->setClassMapAuthoritative(true);', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
$this->assertStringContainsString('$loader->setApcuPrefix(\'custom\\\'Prefix\');', file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
$this->assertStringContainsString('$loader->setClassMapAuthoritative(true);', (string) file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
$this->assertStringContainsString('$loader->setApcuPrefix(\'custom\\\'Prefix\');', (string) file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
}

public function testFilesAutoloadGeneration()
Expand Down Expand Up @@ -1442,8 +1443,8 @@ public function testVendorDirExcludedFromWorkingDir()
$this->assertStringEqualsFile($vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
$this->assertStringEqualsFile($vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
$this->assertStringEqualsFile($vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
$this->assertStringContainsString("\$vendorDir . '/b/b/bootstrap.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
$this->assertStringContainsString("\$baseDir . '/test.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
$this->assertStringContainsString("\$vendorDir . '/b/b/bootstrap.php',\n", (string) file_get_contents($vendorDir.'/composer/autoload_files.php'));
$this->assertStringContainsString("\$baseDir . '/test.php',\n", (string) file_get_contents($vendorDir.'/composer/autoload_files.php'));
}

public function testUpLevelRelativePaths()
Expand Down Expand Up @@ -1527,7 +1528,85 @@ public function testUpLevelRelativePaths()
$this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);
$this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);
$this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_classmap.php', $expectedClassmap);
$this->assertStringContainsString("\$baseDir . '/../test.php',\n", file_get_contents($this->vendorDir.'/composer/autoload_files.php'));
$this->assertStringContainsString("\$baseDir . '/../test.php',\n", (string) file_get_contents($this->vendorDir.'/composer/autoload_files.php'));
}

public function testAutoloadRulesInPackageThatDoesNotExistOnDisk()
{
$package = new RootPackage('root/a', '1.0', '1.0');
$package->setRequires(array(
'dep/a' => new Link('root/a', 'dep/a', new MatchAllConstraint(), 'requires'),
));
$dep = new CompletePackage('dep/a', '1.0', '1.0');

$this->repository->expects($this->any())
->method('getCanonicalPackages')
->will($this->returnValue(array($dep)));

$dep->setAutoload(array(
'psr-0' => array('Foo' => './src'),
));
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_19');

$expectedNamespace = <<<'EOF'
<?php
// autoload_namespaces.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Foo' => array($vendorDir . '/dep/a/src'),
);
EOF;
$this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_namespaces.php', $expectedNamespace);

$dep->setAutoload(array(
'psr-4' => array('Acme\Foo\\' => './src-psr4'),
));
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_19');

$expectedPsr4 = <<<'EOF'
<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Acme\\Foo\\' => array($vendorDir . '/dep/a/src-psr4'),
);
EOF;
$this->assertStringEqualsFile($this->vendorDir.'/composer/autoload_psr4.php', $expectedPsr4);

$dep->setAutoload(array(
'classmap' => array('classmap'),
));
try {
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_19');
} catch (\RuntimeException $e) {
$this->assertSame('Could not scan for classes inside "'.$this->vendorDir.'/dep/a/classmap" which does not appear to be a file nor a folder', $e->getMessage());
}

$dep->setAutoload(array(
'files' => array('./test.php'),
));
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_19');
$this->assertStringContainsString("\$vendorDir . '/dep/a/test.php',\n", (string) file_get_contents($this->vendorDir.'/composer/autoload_files.php'));

$package->setAutoload(array(
'exclude-from-classmap' => array('../excludedroot', 'root/excl'),
));
$dep->setAutoload(array(
'exclude-from-classmap' => array('../../excluded', 'foo/bar'),
));
$map = $this->generator->buildPackageMap($this->im, $package, [$dep]);
$parsed = $this->generator->parseAutoloads($map, $package);
$this->assertSame(array(preg_quote(dirname($this->workingDir)).'/excludedroot($|/)', preg_quote($this->workingDir).'/root/excl($|/)'), $parsed['exclude-from-classmap']);
}

public function testEmptyPaths()
Expand Down Expand Up @@ -1745,10 +1824,10 @@ public function testGeneratesPlatformCheck(array $requires, $expectedFixture, ar

if (null === $expectedFixture) {
$this->assertFalse(file_exists($this->vendorDir . '/composer/platform_check.php'));
$this->assertStringNotContainsString("require __DIR__ . '/platform_check.php';", file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
$this->assertStringNotContainsString("require __DIR__ . '/platform_check.php';", (string) file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
} else {
$this->assertFileContentEquals(__DIR__ . '/Fixtures/platform/' . $expectedFixture . '.php', $this->vendorDir . '/composer/platform_check.php');
$this->assertStringContainsString("require __DIR__ . '/platform_check.php';", file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
$this->assertStringContainsString("require __DIR__ . '/platform_check.php';", (string) file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
}
}

Expand Down Expand Up @@ -1883,8 +1962,8 @@ private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
public static function assertFileContentEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false)
{
self::assertEqualsNormalized(
file_get_contents($expected),
file_get_contents($actual),
(string) file_get_contents($expected),
(string) file_get_contents($actual),
$message ?: $expected.' equals '.$actual,
0,
10,
Expand Down

0 comments on commit ffc6267

Please sign in to comment.