1
0
Fork 0

Detect incorrectly configured COMPOSER env when set to a directory, refs #12049

pull/12092/head
Jordi Boggiano 2024-08-22 10:48:56 +02:00
parent 8c5f2dbb97
commit f931887304
No known key found for this signature in database
2 changed files with 39 additions and 1 deletions

View File

@ -223,7 +223,19 @@ class Factory
public static function getComposerFile(): string public static function getComposerFile(): string
{ {
return trim((string) Platform::getEnv('COMPOSER')) ?: './composer.json'; $env = Platform::getEnv('COMPOSER');
if (is_string($env)) {
$env = trim($env);
if ('' !== $env) {
if (is_dir($env)) {
throw new \RuntimeException('The COMPOSER environment variable is set to '.$env.' which is a directory, this variable should point to a composer.json or be left unset.');
}
return $env;
}
}
return './composer.json';
} }
public static function getLockFile(string $composerFile): string public static function getLockFile(string $composerFile): string

View File

@ -13,9 +13,16 @@
namespace Composer\Test; namespace Composer\Test;
use Composer\Factory; use Composer\Factory;
use Composer\Util\Platform;
class FactoryTest extends TestCase class FactoryTest extends TestCase
{ {
public function tearDown(): void
{
parent::tearDown();
Platform::clearEnv('COMPOSER');
}
/** /**
* @group TLS * @group TLS
*/ */
@ -37,4 +44,23 @@ class FactoryTest extends TestCase
Factory::createHttpDownloader($ioMock, $config); Factory::createHttpDownloader($ioMock, $config);
} }
public function testGetComposerJsonPath(): void
{
self::assertSame('./composer.json', Factory::getComposerFile());
}
public function testGetComposerJsonPathFailsIfDir(): void
{
Platform::putEnv('COMPOSER', __DIR__);
self::expectException('RuntimeException');
self::expectExceptionMessage('The COMPOSER environment variable is set to '.__DIR__.' which is a directory, this variable should point to a composer.json or be left unset.');
Factory::getComposerFile();
}
public function testGetComposerJsonPathFromEnv(): void
{
Platform::putEnv('COMPOSER', ' foo.json ');
self::assertSame('foo.json', Factory::getComposerFile());
}
} }