Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for java version parsing #897

Merged
merged 3 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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