1
0
Fork 0

Fix handling of COMPOSER_ROOT_VERSION to normalize according to expectations, fixes #12101 (#12109)

pull/12110/head
Jordi Boggiano 2024-09-17 08:38:43 +02:00 committed by GitHub
parent 5f2b91aea8
commit 1b5b56f234
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 41 additions and 2 deletions

View File

@ -88,7 +88,7 @@ class RootPackageLoader extends ArrayLoader
// override with env var if available // override with env var if available
if (Platform::getEnv('COMPOSER_ROOT_VERSION')) { if (Platform::getEnv('COMPOSER_ROOT_VERSION')) {
$config['version'] = Platform::getEnv('COMPOSER_ROOT_VERSION'); $config['version'] = $this->versionGuesser->getRootVersionFromEnv();
} else { } else {
$versionData = $this->versionGuesser->guessVersion($config, $cwd ?? Platform::getCwd(true)); $versionData = $this->versionGuesser->guessVersion($config, $cwd ?? Platform::getCwd(true));
if ($versionData) { if ($versionData) {

View File

@ -420,4 +420,17 @@ class VersionGuesser
return null; return null;
} }
public function getRootVersionFromEnv(): string
{
$version = Platform::getEnv('COMPOSER_ROOT_VERSION');
if (!is_string($version) || $version === '') {
throw new \RuntimeException('COMPOSER_ROOT_VERSION not set or empty');
}
if (Preg::isMatch('{^(\d+(?:\.\d+)*)-dev$}i', $version, $match)) {
$version = $match[1].'.x-dev';
}
return $version;
}
} }

View File

@ -198,7 +198,7 @@ class PathRepository extends ArrayRepository implements ConfigurableRepositoryIn
&& 0 === $this->process->execute('git rev-parse HEAD', $ref2) && 0 === $this->process->execute('git rev-parse HEAD', $ref2)
&& $ref1 === $ref2 && $ref1 === $ref2
) { ) {
$package['version'] = $rootVersion; $package['version'] = $this->versionGuesser->getRootVersionFromEnv();
} }
} }

View File

@ -17,6 +17,7 @@ use Composer\Package\Version\VersionGuesser;
use Composer\Semver\VersionParser; use Composer\Semver\VersionParser;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Util\Git as GitUtil; use Composer\Util\Git as GitUtil;
use Composer\Util\Platform;
use Composer\Util\ProcessExecutor; use Composer\Util\ProcessExecutor;
class VersionGuesserTest extends TestCase class VersionGuesserTest extends TestCase
@ -365,4 +366,29 @@ class VersionGuesserTest extends TestCase
self::assertEquals("1.5.x-dev", $versionData['pretty_version']); self::assertEquals("1.5.x-dev", $versionData['pretty_version']);
self::assertEquals("1.5.9999999.9999999-dev", $versionData['version']); self::assertEquals("1.5.9999999.9999999-dev", $versionData['version']);
} }
/**
* @dataProvider rootEnvVersionsProvider
*/
public function testGetRootVersionFromEnv(string $env, string $expectedVersion): void
{
Platform::putEnv('COMPOSER_ROOT_VERSION', $env);
$guesser = new VersionGuesser(new Config, $this->getProcessExecutorMock(), new VersionParser());
self::assertSame($expectedVersion, $guesser->getRootVersionFromEnv());
Platform::clearEnv('COMPOSER_ROOT_VERSION');
}
/**
* @return array<array{string, string}>
*/
public function rootEnvVersionsProvider(): array
{
return [
['1.0-dev', '1.0.x-dev'],
['1.0.x-dev', '1.0.x-dev'],
['1-dev', '1.x-dev'],
['1.x-dev', '1.x-dev'],
['1.0.0', '1.0.0'],
];
}
} }