From 19f89069a4dddf65888f6779774b540244e0ffbf Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 11 Oct 2011 09:30:10 +0200 Subject: [PATCH] Add some more version normalization and support for branch names --- .../Package/Version/VersionParser.php | 31 ++++++++++++++++++- .../Package/Version/VersionParserTest.php | 26 ++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index d91bbb5f6..59d6f5382 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -34,6 +34,10 @@ class VersionParser { $version = trim($version); + if (in_array($version, array('master', 'trunk'))) { + return '9999999-dev'; + } + // match classical versioning if (preg_match('{^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?'.$this->modifierRegex.'$}i', $version, $matches)) { $version = $matches[1] @@ -65,6 +69,31 @@ class VersionParser throw new \UnexpectedValueException('Invalid version string '.$version); } + /** + * Normalizes a branch name to be able to perform comparisons on it + * + * @param string $version + * @return array + */ + public function normalizeBranch($name) + { + $name = trim($name); + + if (in_array($name, array('master', 'trunk'))) { + return $this->normalize($name); + } + + if (preg_match('#^v?(\d+)(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?$#i', $name, $matches)) { + $version = ''; + for ($i = 1; $i < 5; $i++) { + $version .= isset($matches[$i]) ? str_replace('*', 'x', $matches[$i]) : '.x'; + } + return str_replace('x', '9999999', $version).'-dev'; + } + + throw new \UnexpectedValueException('Invalid branch name '.$branch); + } + /** * Parses as constraint string into LinkConstraint objects * @@ -93,7 +122,7 @@ class VersionParser private function parseConstraint($constraint) { - if ('*' === $constraint || '*.*' === $constraint || '*.*.*' === $constraint) { + if ('*' === $constraint || '*.*' === $constraint || '*.*.*' === $constraint || '*.*.*.*' === $constraint) { return array(); } diff --git a/tests/Composer/Test/Package/Version/VersionParserTest.php b/tests/Composer/Test/Package/Version/VersionParserTest.php index 09789d5e6..4a6909e23 100644 --- a/tests/Composer/Test/Package/Version/VersionParserTest.php +++ b/tests/Composer/Test/Package/Version/VersionParserTest.php @@ -49,6 +49,8 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase 'parses datetime' => array('20100102-203040', '20100102-203040'), 'parses dt+number' => array('20100102203040-10', '20100102203040-10'), 'parses dt+patch' => array('20100102-203040-p1', '20100102-203040-patch1'), + 'parses master' => array('master', '9999999-dev'), + 'parses trunk' => array('trunk', '9999999-dev'), ); } @@ -72,6 +74,30 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase ); } + /** + * @dataProvider successfulNormalizedBranches + */ + public function testNormalizeBranch($input, $expected) + { + $parser = new VersionParser; + $this->assertSame((string) $expected, (string) $parser->normalizeBranch($input)); + } + + public function successfulNormalizedBranches() + { + return array( + 'parses x' => array('v1.x', '1.9999999.9999999.9999999-dev'), + 'parses *' => array('v1.*', '1.9999999.9999999.9999999-dev'), + 'parses digits' => array('v1.0', '1.0.9999999.9999999-dev'), + 'parses long x' => array('v1.0.x', '1.0.9999999.9999999-dev'), + 'parses long *' => array('v1.0.3.*', '1.0.3.9999999-dev'), + 'parses long digits' => array('v2.4.0', '2.4.0.9999999-dev'), + 'parses long digits/2' => array('2.4.4', '2.4.4.9999999-dev'), + 'parses master' => array('master', '9999999-dev'), + 'parses trunk' => array('trunk', '9999999-dev'), + ); + } + /** * @dataProvider simpleConstraints */