Skip to content

Commit

Permalink
Fix: make pontos-version for golang usable (#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
y0urself authored Feb 7, 2023
1 parent 3fa57cf commit cdcda71
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pontos/release/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def prepare(
if not executed:
return PrepareReturnValue.UPDATE_VERSION_ERROR

terminal.ok(f"updated version in {filename} to {release_version}")
terminal.ok(f"updated version in {filename} to {release_version}")

changelog_bool = True
if args.conventional_commits:
Expand Down
70 changes: 49 additions & 21 deletions pontos/version/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,61 @@
# 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 subprocess
from subprocess import CalledProcessError
import re
from pathlib import Path

from .helper import (
VersionError,
check_develop,
is_version_pep440_compliant,
strip_version,
safe_version,
versions_equal,
)
from .version import UpdatedVersion, VersionCommand

TEMPLATE = """package main
// THIS IS AN AUTOGENERATED FILE. DO NOT TOUCH!
var version = "{}"
\n"""


# This class is used for Python Version command(s)
class GoVersionCommand(VersionCommand):
project_file_name = "go.mod"
project_file_name = Path("go.mod")
version_file_path = Path("version.go")

def _update_version_file(self, new_version: str) -> None:
"""
Update the version file with the new version
"""
self.version_file_path.write_text(
TEMPLATE.format(new_version), encoding="utf-8"
)

def get_current_version(self) -> str:
"""Get the current version of this project
In go the version is only defined within the repository
tags, thus we need to check git, what tag is the latest"""
try:
proc = subprocess.run(
"git describe --tags `git rev-list --tags --max-count=1`",
shell=True,
check=True,
errors="ignore",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
if self.version_file_path.exists():
version_file_text = self.version_file_path.read_text(
encoding="utf-8"
)
match = re.search(
r'var version = "([deprv0-9.]+)"', version_file_text
)
version = strip_version(proc.stdout)
return version.strip() if version is not None else ""
except CalledProcessError as e:
if match:
return match.group(1)
else:
raise VersionError(
f"No version found in the {self.version_file_path} file."
)
else:
raise VersionError(
"No version tag found. Maybe this "
"module has not been released at all."
) from e
f"No {self.version_file_path} file found. "
"This file is required for pontos"
)

def verify_version(self, version: str) -> None:
"""Verify the current version of this project"""
Expand All @@ -70,6 +89,15 @@ def update_version(
self, new_version: str, *, develop: bool = False, force: bool = False
) -> UpdatedVersion:
"""Update the current version of this project"""
raise VersionError(
"Updating the version of a go module is not possible."
)
new_version = safe_version(new_version)
if not check_develop(new_version) and develop:
new_version = f"{new_version}.dev1"

try:
current_version = self.get_current_version()
except VersionError as e:
raise e

self._update_version_file(new_version=new_version)

return UpdatedVersion(previous=current_version, new=new_version)
50 changes: 31 additions & 19 deletions tests/version/test_go_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
# 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 subprocess
import unittest
from dataclasses import dataclass
from pathlib import Path
from unittest.mock import MagicMock, patch

from pontos.testing import temp_directory, temp_file
Expand All @@ -31,40 +31,51 @@ class StdOutput:
stdout: bytes


class GetCurrentGoVersionCommandTestCase(unittest.TestCase):
@patch("pontos.version.go.subprocess", autospec=True)
def test_getting_last_git_tag(self, subprocess_mock):
subprocess_mock.run.return_value = StdOutput("21.22")
TEMPLATE = """package main
// THIS IS AN AUTOGENERATED FILE. DO NOT TOUCH!
var version = "0.0.1"
\n"""


class GetCurrentGoVersionCommandTestCase(unittest.TestCase):
def test_getting_last_git_tag(self):
with temp_file(
"",
name="go.mod",
change_into=True,
):
version_file_path = Path("version.go")
version_file_path.write_text(TEMPLATE, encoding="utf-8")
cmd = GoVersionCommand()
version = cmd.get_current_version()

self.assertEqual(
subprocess_mock.run.call_args.args[0],
"git describe --tags `git rev-list --tags --max-count=1`",
)
self.assertEqual(version, "21.22")
self.assertEqual(version, "0.0.1")

version_file_path.unlink()

@patch("pontos.version.go.subprocess", autospec=True)
def test_no_git_tag(self, subprocess_mock):
subprocess_mock.run.side_effect = subprocess.CalledProcessError(
returncode=2,
cmd=["'git describe --tags `git rev-list --tags --max-count=1`'"],
)
def test_no_version_tag(self):
with temp_file(
"",
name="go.mod",
change_into=True,
), self.assertRaisesRegex(
VersionError,
"No version found in the version.go file.",
):
Path("version.go").touch()
cmd = GoVersionCommand()
cmd.get_current_version()

def test_no_version_file(self):
with temp_file(
"",
name="go.mod",
change_into=True,
), self.assertRaisesRegex(
VersionError,
"No version tag found. Maybe this "
"module has not been released at all.",
"No version.go file found. This file is required for pontos",
):
cmd = GoVersionCommand()
cmd.get_current_version()
Expand Down Expand Up @@ -121,7 +132,8 @@ def test_no_update_version(self):
cmd = GoVersionCommand()

with self.assertRaisesRegex(
VersionError, "Updating the version of a go module is not possible."
VersionError,
"No version.go file found. This file is required for pontos",
):
cmd.update_version("22.2.2")

Expand Down

0 comments on commit cdcda71

Please sign in to comment.