From a5d2e2c861f1dae4d25f6be0d9e1641cc8daba63 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 5 Feb 2024 14:20:26 +0000 Subject: [PATCH] Handle unparseable tags gracefully --- php/utils.php | 14 +++++++++----- tests/test-utils.php | 13 +++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/php/utils.php b/php/utils.php index 6daaeb171..939b6c1f1 100644 --- a/php/utils.php +++ b/php/utils.php @@ -962,12 +962,16 @@ function get_named_sem_ver( $new_version, $original_version ) { $patch = $bits[2]; } - if ( isset( $minor ) && Semver::satisfies( $new_version, "{$major}.{$minor}.x" ) ) { - return 'patch'; - } + try { + if ( isset( $minor ) && Semver::satisfies( $new_version, "{$major}.{$minor}.x" ) ) { + return 'patch'; + } - if ( Semver::satisfies( $new_version, "{$major}.x.x" ) ) { - return 'minor'; + if ( Semver::satisfies( $new_version, "{$major}.x.x" ) ) { + return 'minor'; + } + } catch ( \UnexpectedValueException $e ) { + return ''; } return 'major'; diff --git a/tests/test-utils.php b/tests/test-utils.php index 3e43256e6..2bf02fbb1 100644 --- a/tests/test-utils.php +++ b/tests/test-utils.php @@ -43,6 +43,12 @@ public function testGetSemVer() { $original_version = '0.19.1'; $this->assertEmpty( Utils\get_named_sem_ver( '0.18.0', $original_version ) ); $this->assertEmpty( Utils\get_named_sem_ver( '0.19.1', $original_version ) ); + $this->assertEmpty( Utils\get_named_sem_ver( 'nonsense', $original_version ) ); + $this->assertEmpty( Utils\get_named_sem_ver( '0.18.1-beta3', $original_version ) ); + $this->assertEmpty( Utils\get_named_sem_ver( '0.19.1-dev1', $original_version ) ); + $this->assertEmpty( Utils\get_named_sem_ver( '0.19.1-beta3', $original_version ) ); + $this->assertEmpty( Utils\get_named_sem_ver( '0.19.2-dev1', $original_version ) ); // -dev suffix not accepted by SemVer. + $this->assertEquals( 'patch', Utils\get_named_sem_ver( '0.19.2-beta3', $original_version ) ); $this->assertEquals( 'patch', Utils\get_named_sem_ver( '0.19.2', $original_version ) ); $this->assertEquals( 'minor', Utils\get_named_sem_ver( '0.20.0', $original_version ) ); $this->assertEquals( 'minor', Utils\get_named_sem_ver( '0.20.3', $original_version ) ); @@ -54,7 +60,14 @@ public function testGetSemVerWP() { $original_version = '3.0'; $this->assertEmpty( Utils\get_named_sem_ver( '2.8', $original_version ) ); $this->assertEmpty( Utils\get_named_sem_ver( '2.9.1', $original_version ) ); + $this->assertEmpty( Utils\get_named_sem_ver( 'nonsense', $original_version ) ); + $this->assertEmpty( Utils\get_named_sem_ver( '2.0-beta3', $original_version ) ); + $this->assertEmpty( Utils\get_named_sem_ver( '3.0-dev1', $original_version ) ); + $this->assertEmpty( Utils\get_named_sem_ver( '3.0-beta3', $original_version ) ); + $this->assertEmpty( Utils\get_named_sem_ver( '3.0.1-dev1', $original_version ) ); // -dev suffix not accepted by SemVer. + $this->assertEquals( 'patch', Utils\get_named_sem_ver( '3.0.1-beta3', $original_version ) ); $this->assertEquals( 'patch', Utils\get_named_sem_ver( '3.0.1', $original_version ) ); + $this->assertEquals( 'minor', Utils\get_named_sem_ver( '3.1-beta3', $original_version ) ); $this->assertEquals( 'minor', Utils\get_named_sem_ver( '3.1', $original_version ) ); $this->assertEquals( 'minor', Utils\get_named_sem_ver( '3.1.1', $original_version ) ); $this->assertEquals( 'major', Utils\get_named_sem_ver( '4.0', $original_version ) );