Skip to content

Commit

Permalink
Add tests for java version parsing (#897)
Browse files Browse the repository at this point in the history
* Add tests for java version parsing

* Fix linting problems

* Fix linting problems
  • Loading branch information
stefanTolksdorf authored Oct 9, 2023
1 parent 1ab0119 commit 0dfbfe5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
23 changes: 14 additions & 9 deletions pontos/version/commands/_java.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@
from ..errors import VersionError
from ..version import Version, VersionUpdate

VERSION_PATTERN = (
r"^(?P<pre>.*[^\d])"
r"(?P<version>\d+\.\d+\.\d+"
r"(-?([ab]|rc|alpha|beta)\d+(.dev\d+)?)?)"
r"(?P<post>.*$)"
)


# This class is used for Java Version command(s)
class JavaVersionCommand(VersionCommand):
VERSION_PATTERN = (
r"^(?P<pre>.*[^\d])?"
r"("
r"?P<version>\d+\.\d+\.\d+"
r"([-\.]+(dev|rc|beta|a|alpha|b)\d+)*"
r")"
r"(?P<post>.*$)"
)

project_file_name = "upgradeVersion.json"

def get_current_version(self) -> Version:
Expand Down Expand Up @@ -79,6 +81,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()

Expand All @@ -90,7 +95,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, "
Expand Down Expand Up @@ -143,7 +148,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, "
Expand Down
39 changes: 39 additions & 0 deletions tests/version/commands/test_java.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,45 @@
"""


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):
with temp_directory(change_into=True):
Expand Down

0 comments on commit 0dfbfe5

Please sign in to comment.