Skip to content

Commit

Permalink
Adjust version compares for new preview release syntax (#22218)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl authored May 15, 2024
1 parent ad6fe9c commit c1b86d3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
6 changes: 2 additions & 4 deletions .github/scripts/build-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ echo "Working directory is '$CURRENT_DIR'..."

echo -e "Going to build Matomo $VERSION (Major version: $MAJOR_VERSION)"

if ! echo "$VERSION" | grep -E 'rc|b|a|alpha|beta|dev|build|p[0-9]{14}' -i
if ! echo "$VERSION" | grep -E 'rc|b|a|alpha|beta|dev|build' -i
then
if curl --output /dev/null --silent --head --fail "https://builds.matomo.org/matomo-$VERSION.zip"
then
Expand Down Expand Up @@ -219,9 +219,7 @@ echo "Git tag: $(git describe --exact-match --tags HEAD)"
echo "Git path: $CURRENT_DIR/$LOCAL_REPO"
echo "Matomo version in core/Version.php: $(php -r "include_once 'core/Version.php'; echo \Piwik\Version::VERSION;")"

IS_PREVIEW_VERSION=$(php -r "include_once 'core/Version.php'; \$v = new \Piwik\Version(); echo (int) \$v->isPreviewVersion('$VERSION');")

if [ "$VERSION" != "build" ] && [ "$IS_PREVIEW_VERSION" == "0" ]
if [ "$VERSION" != "build" ]
then
[ "$(grep "'$VERSION'" core/Version.php | wc -l)" = "1" ] || die "version $VERSION does not match core/Version.php";
fi
Expand Down
6 changes: 3 additions & 3 deletions core/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public function isVersionNumber($version): bool

private function isNonStableVersion($version): bool
{
return (bool) preg_match('/^\d+\.\d+\.\d+((-.{1,4}\d+(-p\d{14})?)|(-dev-p\d{14}))$/i', $version);
return (bool) preg_match('/^\d+\.\d+\.\d+((-.{1,4}\d+(\.\d{14})?)|(-alpha\.\d{14}))$/i', $version);
}

public function isPreviewVersion($version): bool
{
if (\preg_match('/^\d+\.\d+\.\d+((-(rc|b|beta)\d+(-p\d{14})?)|(-dev-p\d{14}))?$/i', $version)) {
if (\preg_match('/-p(\d{14})$/', $version, $matches)) {
if (\preg_match('/^\d+\.\d+\.\d+((-(rc|b|beta)\d+(\.\d{14})?)|(-alpha\.\d{14}))?$/i', $version)) {
if (\preg_match('/\.(\d{14})$/', $version, $matches)) {
$dt = DateTime::createFromFormat('YmdHis', $matches[1]);

return false !== $dt && !\array_sum(array_map('intval', (array) $dt::getLastErrors()));
Expand Down
63 changes: 50 additions & 13 deletions tests/PHPUnit/Unit/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Piwik\Tests\Unit;

use Composer\Semver\VersionParser;
use Piwik\Version;

class VersionTest extends \PHPUnit\Framework\TestCase
Expand Down Expand Up @@ -57,28 +58,29 @@ public function testIsVersionNumber()

public function testIsPreviewVersion()
{
$this->assertIsPreviewVersion('3.3.3-dev-p20240509114000');
$this->assertIsPreviewVersion('3.3.3-dev-p33331224183000');
$this->assertIsPreviewVersion('3.3.3-b1-p20240509114000');
$this->assertIsPreviewVersion('100.999.9191-rc4-p20240509114000');
$this->assertIsPreviewVersion('3.3.3-alpha.20240509114000');
$this->assertIsPreviewVersion('3.3.3-alpha.33331224183000');
$this->assertIsPreviewVersion('3.3.3-b1.20240509114000');
$this->assertIsPreviewVersion('100.999.9191-rc4.20240509114000');

$this->assertNotPreviewVersion('3.3');
$this->assertNotPreviewVersion('3.3.');
$this->assertNotPreviewVersion('3-3-3');
$this->assertNotPreviewVersion('a3.3.3');
$this->assertNotPreviewVersion('3.0.0b');
$this->assertNotPreviewVersion('3.3.3-b1');
$this->assertNotPreviewVersion('3.3.3-b1-pp20240509114000');
$this->assertNotPreviewVersion('3.3.3-b1-p20240509114000a');
$this->assertNotPreviewVersion('3.3.3-b1.p20240509114000');
$this->assertNotPreviewVersion('3.3.3-b1.20240509114000a');
$this->assertNotPreviewVersion('3.3.3-rc1');
$this->assertNotPreviewVersion('3.3.3-p20240509114000');
$this->assertNotPreviewVersion('3.3.3-dev.20240509114000');
$this->assertNotPreviewVersion('3.3.3.20240509114000');
$this->assertNotPreviewVersion('p20240509114000');
$this->assertNotPreviewVersion('3.3.3-b1-p202405091140');
$this->assertNotPreviewVersion('3.3.3-b1-p20243309114000');
$this->assertNotPreviewVersion('3.3.3-b1-p20240544114000');
$this->assertNotPreviewVersion('3.3.3-b1-p20240509554000');
$this->assertNotPreviewVersion('3.3.3-b1-p20240509117700');
$this->assertNotPreviewVersion('3.3.3-b1-p20240509114088');
$this->assertNotPreviewVersion('3.3.3-b1.202405091140');
$this->assertNotPreviewVersion('3.3.3-b1.20243309114000');
$this->assertNotPreviewVersion('3.3.3-b1.20240544114000');
$this->assertNotPreviewVersion('3.3.3-b1.20240509554000');
$this->assertNotPreviewVersion('3.3.3-b1.20240509117700');
$this->assertNotPreviewVersion('3.3.3-b1.20240509114088');
}

private function assertIsStableVersion($versionNumber)
Expand Down Expand Up @@ -116,4 +118,39 @@ private function assertNotPreviewVersion($versionNumber)
$isPreviewVersion = $this->version->isPreviewVersion($versionNumber);
$this->assertFalse($isPreviewVersion);
}

/**
* @dataProvider getLowerVersionCompares
*/
public function testVersionContraints($v1, $v2)
{
$v = new VersionParser();
$v1p = $v->parseConstraints($v1);
$v2p = $v->parseConstraints('<' . $v2);

self::assertTrue($v2p->matches($v1p));
}

/**
* @dataProvider getLowerVersionCompares
*/
public function testVersionCompares($v1, $v2)
{
self::assertTrue(version_compare($v1, $v2, '<'));
}

public function getLowerVersionCompares()
{
return [
[ '5.1.0', '6.0.0-b1' ],
[ '5.1.0-alpha.20240517231100', '5.1.0-b1' ],
[ '5.1.0-alpha.20240517231100', '5.1.0-rc1' ],
[ '5.1.0-alpha.20240517231100', '5.1.0-alpha.20240617231100' ],
[ '5.1.0-b1.20240517231100', '5.1.0-b2' ],
[ '5.1.0-b1.20240517231100', '5.1.0-rc1' ],
[ '5.1.0-b1.20240517221100', '5.1.0-b1.20240517231100' ],
[ '5.1.0-rc1.20240517231100', '5.1.0-rc2' ],
[ '5.1.0-rc1.20240517221100', '5.1.0-rc1.20240517231100' ],
];
}
}

0 comments on commit c1b86d3

Please sign in to comment.