Rework get_version and various improvements

This commit is contained in:
Semantic Versioner
2023-07-02 23:03:34 -04:00
parent d1e2d77a29
commit fa41212e77
11 changed files with 310 additions and 74 deletions

View File

@ -26,6 +26,14 @@ class SCM(ABC):
def tag_version(self, version: str) -> None:
raise NotImplementedError()
@abstractmethod
def get_version_hash(self, version: str) -> str:
raise NotImplementedError()
@abstractmethod
def get_hash(self) -> str:
raise NotImplementedError()
def get_file_version(self, config: dict) -> str:
"""
:param config: The bumpversion config as a dict

View File

@ -106,8 +106,6 @@ class Git(SCM):
branch: str = self.get_branch()
logger.info(f"Main branch is {branch}")
matches = self.git_commit_pattern.search(
message.replace("\\n", "\n").replace("\\", "")
)
@ -140,4 +138,25 @@ class Git(SCM):
)
def tag_version(self, version: str) -> None:
"""
Creates a git tag at HEAD with the given version
:param version: The version to tag
"""
self._run_command(self.git_bin, "tag", version)
def get_version_hash(self, version: str) -> str:
"""
Get the hash of the commit that has the given version
:param version: The version to get the hash for
:return: The hash of the commit that has the given version
"""
proc = self._run_command(self.git_bin, "rev-list", "-n", "1", version)
return proc.stdout.rstrip()
def get_hash(self) -> str:
"""
Get the hash of the current commit
:return: The hash of the current commit
"""
proc = self._run_command(self.git_bin, "rev-parse", "HEAD")
return proc.stdout.rstrip()

View File

@ -18,3 +18,9 @@ class MockSCM(SCM):
def tag_version(self, version: str) -> None:
pass
def get_version_hash(self, version: str) -> str:
return "HASH"
def get_hash(self) -> str:
return "HASH"

View File

@ -98,3 +98,20 @@ class TestMockSCM(unittest.TestCase):
version = "1.0.0"
self.scm.tag_version(version)
@mock.patch("subprocess.run")
def test_get_version_hash(self, mock_subprocess_run: mock.Mock):
mock_subprocess_run.return_value.stdout = "HASH\n"
version = "1.0.0"
expected_hash = "HASH"
version_hash = self.scm.get_version_hash(version)
self.assertEqual(version_hash, expected_hash)
@mock.patch("subprocess.run")
def test_get_hash(self, mock_subprocess_run: mock.Mock):
mock_subprocess_run.return_value.stdout = "HASH\n"
expected_hash = "HASH"
version_hash = self.scm.get_hash()
self.assertEqual(version_hash, expected_hash)

View File

@ -32,3 +32,14 @@ class TestMockSCM(unittest.TestCase):
def test_tag_version(self):
version = "1.0.0"
self.scm.tag_version(version)
def test_get_version_hash(self):
version = "1.0.0"
expected_hash = "HASH"
version_hash = self.scm.get_version_hash(version)
self.assertEqual(version_hash, expected_hash)
def test_get_hash(self):
expected_hash = "HASH"
version_hash = self.scm.get_hash()
self.assertEqual(version_hash, expected_hash)