From d3b870bb693b39a147af4d51e2731f7c657e1401 Mon Sep 17 00:00:00 2001 From: Stefan Tolksdorf Date: Fri, 6 Oct 2023 15:00:02 +0200 Subject: [PATCH 1/3] Add tests for java version parsing --- pontos/version/commands/_java.py | 24 ++++++++++-------- tests/version/commands/test_java.py | 38 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/pontos/version/commands/_java.py b/pontos/version/commands/_java.py index 379c7e07..7cdc63bf 100644 --- a/pontos/version/commands/_java.py +++ b/pontos/version/commands/_java.py @@ -24,16 +24,17 @@ from ..errors import VersionError from ..version import Version, VersionUpdate -VERSION_PATTERN = ( - r"^(?P
.*[^\d])"
-    r"(?P\d+\.\d+\.\d+"
-    r"(-?([ab]|rc|alpha|beta)\d+(.dev\d+)?)?)"
-    r"(?P.*$)"
-)
-
-
 # This class is used for Java Version command(s)
 class JavaVersionCommand(VersionCommand):
+    VERSION_PATTERN = (
+        r"^(?P
.*[^\d])?"
+        r"("
+        r"?P\d+\.\d+\.\d+"
+        r"([-\.]+(dev|rc|beta|a|alpha|b)\d+)*"
+        r")"
+        r"(?P.*$)"
+    )
+
     project_file_name = "upgradeVersion.json"
 
     def get_current_version(self) -> Version:
@@ -79,6 +80,9 @@ def update_version(
             changed_files=changed_files,
         )
 
+    def parse_line(self, version_line: str):
+        return re.match(self.VERSION_PATTERN, version_line, re.DOTALL)
+
     def _update_version_files(self, new_version) -> List[Path]:
         config = self._load_config()
 
@@ -90,7 +94,7 @@ def _update_version_files(self, new_version) -> List[Path]:
                 line_number = file_config["line"]
                 version_line = lines[line_number - 1]
 
-                matches = re.match(VERSION_PATTERN, version_line, re.DOTALL)
+                matches = self.parse_line(version_line)
                 if matches is None:
                     raise VersionError(
                         f"Line has no version, "
@@ -143,7 +147,7 @@ def _read_versions_from_files(self) -> Dict[str, str]:
                         f"file:'{file_path}'"
                     )
                 version_line = readlines[line_number - 1]
-                matches = re.match(VERSION_PATTERN, version_line, re.DOTALL)
+                matches = self.parse_line(version_line)
                 if matches is None:
                     raise VersionError(
                         f"Line has no version, "
diff --git a/tests/version/commands/test_java.py b/tests/version/commands/test_java.py
index 50f2a8ec..d032166e 100644
--- a/tests/version/commands/test_java.py
+++ b/tests/version/commands/test_java.py
@@ -14,6 +14,7 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see .
+import re
 import unittest
 from pathlib import Path
 from string import Template
@@ -71,6 +72,43 @@
 server.port=8080
 """
 
+class VerifyJavaVersionParsingTestCase(unittest.TestCase):
+    def test_version_parsing(self):
+        versions = {
+            "2023.12.10",
+            "2023.1.1",
+            "2023.10.1",
+            "2023.1.99",
+            "0.0.1",
+            "1.2.3-a1",
+            "1.2.3-alpha1",
+            "1.2.3-alpha1-dev1",
+            "1.2.3-b1",
+            "1.2.3-beta1",
+            "1.2.3-beta1-dev1",
+            "1.2.3-rc1",
+            "1.2.3-rc1-dev1",
+            "1.2.3-dev1",
+            "22.4.1",
+            "22.4.1-dev1",
+            "0.5.0.dev1",
+            "1.0.0-dev1",
+            "1.0.0-alpha1",
+            "1.0.0-alpha1-dev1",
+            "1.0.0-beta1",
+            "1.0.0-beta1-dev1",
+            "1.0.0-rc1",
+            "1.0.0-rc1-dev1",
+        }
+        for version in versions:
+            with self.subTest(version=version):
+                matches = JavaVersionCommand(
+                    SemanticVersioningScheme
+                ).parse_line(f"pre{version}post")
+
+                self.assertEqual(matches.group("pre"), "pre")
+                self.assertEqual(matches.group("version"), version)
+                self.assertEqual(matches.group("post"), "post")
 
 class GetCurrentJavaVersionCommandTestCase(unittest.TestCase):
     def test_getting_version(self):

From 7158a6717ae0369984161e8d217bd704d5b45864 Mon Sep 17 00:00:00 2001
From: Stefan Tolksdorf 
Date: Fri, 6 Oct 2023 15:03:06 +0200
Subject: [PATCH 2/3] Fix linting problems

---
 pontos/version/commands/_java.py    | 1 +
 tests/version/commands/test_java.py | 2 ++
 2 files changed, 3 insertions(+)

diff --git a/pontos/version/commands/_java.py b/pontos/version/commands/_java.py
index 7cdc63bf..2eb89bd7 100644
--- a/pontos/version/commands/_java.py
+++ b/pontos/version/commands/_java.py
@@ -24,6 +24,7 @@
 from ..errors import VersionError
 from ..version import Version, VersionUpdate
 
+
 # This class is used for Java Version command(s)
 class JavaVersionCommand(VersionCommand):
     VERSION_PATTERN = (
diff --git a/tests/version/commands/test_java.py b/tests/version/commands/test_java.py
index d032166e..ed1c7493 100644
--- a/tests/version/commands/test_java.py
+++ b/tests/version/commands/test_java.py
@@ -72,6 +72,7 @@
 server.port=8080
 """
 
+
 class VerifyJavaVersionParsingTestCase(unittest.TestCase):
     def test_version_parsing(self):
         versions = {
@@ -110,6 +111,7 @@ def test_version_parsing(self):
                 self.assertEqual(matches.group("version"), version)
                 self.assertEqual(matches.group("post"), "post")
 
+
 class GetCurrentJavaVersionCommandTestCase(unittest.TestCase):
     def test_getting_version(self):
         with temp_directory(change_into=True):

From c1cd5eccd15f7f6af0f2ae584bb663c983c15160 Mon Sep 17 00:00:00 2001
From: Stefan Tolksdorf 
Date: Fri, 6 Oct 2023 15:04:26 +0200
Subject: [PATCH 3/3] Fix linting problems

---
 tests/version/commands/test_java.py | 1 -
 1 file changed, 1 deletion(-)

diff --git a/tests/version/commands/test_java.py b/tests/version/commands/test_java.py
index ed1c7493..5f42160b 100644
--- a/tests/version/commands/test_java.py
+++ b/tests/version/commands/test_java.py
@@ -14,7 +14,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see .
-import re
 import unittest
 from pathlib import Path
 from string import Template