Skip to content

Commit

Permalink
Add: Specific Java files, that need updated version on release
Browse files Browse the repository at this point in the history
  • Loading branch information
y0urself authored and bjoernricks committed Jun 22, 2023
1 parent cac7c4f commit c794a3a
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
75 changes: 75 additions & 0 deletions pontos/version/commands/_java.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import re
from pathlib import Path
from typing import Literal, Optional, Union

from lxml import etree
Expand All @@ -31,9 +33,47 @@
__version__ = "{}"\n"""


def find_file(
filename: Path, search_path: str, search_glob: str
) -> Optional[Path]:
"""Find a file somewherre within an directory tree
Arguments:
filename (Path) The file to look up
search_path (str) The path to look for the file
search_glob (str) The glob search pattern
Returns:
The file as Path object, if existing"""
search_path = Path(search_path).resolve()
for file_path in search_path.glob(search_glob):
if file_path.is_file() and file_path.name == filename.name:
return file_path
return None


def replace_string_in_file(
file_path: Path, pattern: str, replacement: str
) -> None:
# Read the content of the file
content = file_path.read_text(encoding="utf-8")

# Search for the pattern in the content
match = re.search(pattern, content)

# Replace the matched group (Group 1) with the replacement
if match:
# Write the updated content back to the file
file_path.write_text(
content.replace(match.group(1), replacement), encoding="utf-8"
)


# This class is used for Java Version command(s)
class JavaVersionCommand(VersionCommand):
project_file_name = "pom.xml"
_properties_file_path = Path(
"src/main/resources/application-docker.properties"
)
_pom_xml: Optional[etree.Element] = None

def _get_version_from_pom_xml(self) -> Version:
Expand Down Expand Up @@ -68,6 +108,39 @@ def _update_pom_version(
self.project_file_path, pretty_print=True, encoding="utf-8"
)

def _update_properties_file(
self,
new_version: Version,
) -> None:
# update the java properties file version
if not self._properties_file_path.exists():
# skip if not existing
return
pattern = r"sentry\.release=([0-9]+\.[0-9]+\.[0-9]+)"
replace_string_in_file(
self._properties_file_path,
pattern=pattern,
replacement=str(new_version),
)

def _update_swagger_config(
self,
new_version: Version,
) -> None:
# update swagger config file version
swagger_config_file = find_file(
filename="SwaggerConfig.java",
search_path="src",
search_glob="**/config/swagger/*",
)
if not swagger_config_file:
# skip if not existing
return
pattern = r'.version\("([0-9]+\.[0-9]+\.[0-9]+)"\)'
replace_string_in_file(
swagger_config_file, pattern=pattern, replacement=str(new_version)
)

@property
def pom_xml(self) -> etree.Element:
if self._pom_xml is not None:
Expand Down Expand Up @@ -113,6 +186,8 @@ def update_version(

changed_files = [self.project_file_path]
self._update_pom_version(new_version=new_version)
self._update_properties_file(new_version=new_version)
self._update_swagger_config(new_version=new_version)

return VersionUpdate(
previous=package_version,
Expand Down
85 changes: 85 additions & 0 deletions tests/version/commands/test_java.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,100 @@
# pylint: disable=line-too-long

import unittest
from pathlib import Path

from lxml import etree

from pontos.testing import temp_directory, temp_file
from pontos.version import VersionError
from pontos.version.commands import JavaVersionCommand
from pontos.version.commands._java import find_file, replace_string_in_file
from pontos.version.schemes import PEP440VersioningScheme


class TestFindFile(unittest.TestCase):
def test_file_found(self):
with temp_directory() as temp_dir:
deep_path = Path(temp_dir / "foo/bat/config/swagger/")
deep_path.mkdir(parents=True)
Path(deep_path / "SwaggerConfig.java").touch()
self.assertTrue(
Path(
temp_dir / "foo/bat/config/swagger/SwaggerConfig.java"
).exists()
)
filename = Path("SwaggerConfig.java")
search_path = temp_dir # Assuming 'config/swagger' is two levels up
search_glob = "**/config/swagger/*"

result = find_file(filename, search_path, search_glob)

self.assertIsNotNone(result)
self.assertEqual(result.name, filename.name)

def test_file_not_found(self):
with temp_directory() as temp_dir:
Path(
temp_dir / "foo/bat/config/swagger/SwaggerConfig.java",
parents=True,
)
filename = Path("NonExistentFile.java")
search_path = temp_dir
search_glob = "**/config/swagger/*"

result = find_file(filename, search_path, search_glob)

self.assertIsNone(result)


class TestReplaceString(unittest.TestCase):
def test_replace_string_in_file(self):
# Define the test parameters
content = """
Foo, bar
baz
.version("1.2.3")
glubb
"""
pattern = r'.version\("([0-9]+\.[0-9]+\.[0-9]+)"\)'
replacement = "1.2.4"

with temp_file(content=content) as tmp:
replace_string_in_file(tmp, pattern, replacement)

updated_content = tmp.read_text(encoding="utf-8")

# Verify the replacement was performed correctly
self.assertNotRegex(
updated_content, "1.2.3"
) # Pattern should not be present
self.assertIn(
replacement, updated_content
) # Replacement should be present

def test_replace_string_in_file_no_match(self):
# Define the test parameters
content = """
Foo, bar
baz
.versio("1.2.3")
glubb
"""
pattern = r'.version\("([0-9]+\.[0-9]+\.[0-9]+)"\)'
replacement = "1.2.4"

with temp_file(content=content) as tmp:
# Call the function under test
replace_string_in_file(tmp, pattern, replacement)

# Read the content of the unmodified file
updated_content = tmp.read_text(encoding="utf-8")

# Verify the content remains unchanged
self.assertNotRegex(updated_content, replacement)
self.assertEqual(updated_content, content)


class GetCurrentJavaVersionCommandTestCase(unittest.TestCase):
def test_get_current_version(self):
content = """<?xml version="1.0" encoding="UTF-8"?>
Expand Down

0 comments on commit c794a3a

Please sign in to comment.