Skip to content

Commit

Permalink
Fix: Golang Version command (#655)
Browse files Browse the repository at this point in the history
* Change: Move version regex into static, fix regex catching version

* Fix: Make sure, existing package information is not overwritten
  • Loading branch information
y0urself authored Mar 2, 2023
1 parent a0cdc07 commit 5a4b682
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
16 changes: 10 additions & 6 deletions pontos/version/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .helper import get_last_release_version
from .version import Version, VersionCommand, VersionUpdate, parse_version

VERSION_MATCH = r'var [Vv]ersion = "([deprv0-9.+]+)"'
TEMPLATE = """package main
// THIS IS AN AUTOGENERATED FILE. DO NOT TOUCH!
Expand All @@ -40,9 +41,14 @@ def _update_version_file(self, new_version: Version) -> None:
"""
Update the version file with the new version
"""
self.version_file_path.write_text(
TEMPLATE.format(str(new_version)), encoding="utf-8"
)
if self.version_file_path.exists():
version = self.get_current_version()
template = self.version_file_path.read_text(
encoding="utf-8"
).replace(str(version), str(new_version))
else:
template = TEMPLATE.format(str(new_version))
self.version_file_path.write_text(template, encoding="utf-8")

def get_current_version(self) -> Version:
"""Get the current version of this project
Expand All @@ -52,9 +58,7 @@ def get_current_version(self) -> Version:
version_file_text = self.version_file_path.read_text(
encoding="utf-8"
)
match = re.search(
r'var version = "([deprv0-9.]+)"', version_file_text
)
match = re.search(VERSION_MATCH, version_file_text)
if match:
return parse_version(match.group(1))
else:
Expand Down
11 changes: 6 additions & 5 deletions tests/version/test_go_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class StdOutput:


VERSION_FILE_PATH = "version.go"
TEMPLATE = """package main
TEMPLATE = """package {}
// THIS IS AN AUTOGENERATED FILE. DO NOT TOUCH!
Expand All @@ -58,7 +58,7 @@ def test_getting_version(self):
version = "0.0.1"
version_file_path = Path(VERSION_FILE_PATH)
version_file_path.write_text(
TEMPLATE.format(version), encoding="utf-8"
TEMPLATE.format("main", version), encoding="utf-8"
)
result_version = GoVersionCommand().get_current_version()

Expand Down Expand Up @@ -154,12 +154,13 @@ def test_update_version(self):
version = Version("22.2.2")
version_file_path = Path(VERSION_FILE_PATH)
version_file_path.write_text(
TEMPLATE.format("0.0.1"), encoding="utf-8"
TEMPLATE.format("foo", "0.0.1"), encoding="utf-8"
)
updated = cmd.update_version(version)

content = version_file_path.read_text(encoding="utf-8")
self.assertIn(str(version), content)
self.assertIn("foo", content)
version_file_path.unlink()

self.assertEqual(updated.new, version)
Expand Down Expand Up @@ -193,7 +194,7 @@ def test_no_update(self):
version = Version("22.2.2")
version_file_path = Path(VERSION_FILE_PATH)
version_file_path.write_text(
TEMPLATE.format("22.2.2"), encoding="utf-8"
TEMPLATE.format("main", "22.2.2"), encoding="utf-8"
)
updated = cmd.update_version(version)

Expand All @@ -210,7 +211,7 @@ def test_forced_update(self):
version = Version("22.2.2")
version_file_path = Path(VERSION_FILE_PATH)
version_file_path.write_text(
TEMPLATE.format("22.2.2"), encoding="utf-8"
TEMPLATE.format("main", "22.2.2"), encoding="utf-8"
)
updated = cmd.update_version(version, force=True)

Expand Down

0 comments on commit 5a4b682

Please sign in to comment.