Skip to content

Commit

Permalink
Fix numeric default-branches with v prefix (e.g. v2.x-dev) being trea…
Browse files Browse the repository at this point in the history
…ted as non-numeric and receiving an alias like e.g. dev-main
  • Loading branch information
Seldaek committed May 23, 2023
1 parent 7397a78 commit e51d755
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Composer/Package/Loader/ArrayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public function getBranchAlias(array $config): ?string
if (
isset($config['default-branch'])
&& $config['default-branch'] === true
&& false === $this->versionParser->parseNumericAliasPrefix($config['version'])
&& false === $this->versionParser->parseNumericAliasPrefix(Preg::replace('{^v}', '', $config['version']))
) {
return VersionParser::DEFAULT_BRANCH_ALIAS;
}
Expand Down
52 changes: 52 additions & 0 deletions tests/Composer/Test/Package/Loader/ArrayLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Composer\Package\Loader\ArrayLoader;
use Composer\Package\Dumper\ArrayDumper;
use Composer\Package\Link;
use Composer\Package\Version\VersionParser;
use Composer\Test\TestCase;

class ArrayLoaderTest extends TestCase
Expand Down Expand Up @@ -249,6 +250,57 @@ public function testPackageWithBranchAlias(): void
$this->assertEquals('4.x-dev', $package->getPrettyVersion());
}

public function testPackageAliasingWithoutBranchAlias(): void
{
// non-numeric gets a default alias
$config = [
'name' => 'A',
'version' => 'dev-main',
'default-branch' => true,
];

$package = $this->loader->load($config);

$this->assertInstanceOf('Composer\Package\AliasPackage', $package);
$this->assertEquals(VersionParser::DEFAULT_BRANCH_ALIAS, $package->getPrettyVersion());

// non-default branch gets no alias even if non-numeric
$config = [
'name' => 'A',
'version' => 'dev-main',
'default-branch' => false,
];

$package = $this->loader->load($config);

$this->assertInstanceOf('Composer\Package\CompletePackage', $package);
$this->assertEquals('dev-main', $package->getPrettyVersion());

// default branch gets no alias if already numeric
$config = [
'name' => 'A',
'version' => '2.x-dev',
'default-branch' => true,
];

$package = $this->loader->load($config);

$this->assertInstanceOf('Composer\Package\CompletePackage', $package);
$this->assertEquals('2.9999999.9999999.9999999-dev', $package->getVersion());

// default branch gets no alias if already numeric, with v prefix
$config = [
'name' => 'A',
'version' => 'v2.x-dev',
'default-branch' => true,
];

$package = $this->loader->load($config);

$this->assertInstanceOf('Composer\Package\CompletePackage', $package);
$this->assertEquals('2.9999999.9999999.9999999-dev', $package->getVersion());
}

public function testAbandoned(): void
{
$config = [
Expand Down

0 comments on commit e51d755

Please sign in to comment.