Add basic tests for version increment

This commit is contained in:
bom 2023-02-09 12:02:31 +01:00
parent 1995a7b200
commit 4a34763fb2
2 changed files with 40 additions and 8 deletions

View file

@ -4,8 +4,7 @@ import re
def init_project(): def init_project():
# validate_values() # validate_values()
version = Version('build.gradle') version = Version.from_file('build.gradle')
version.parse()
version.increment(ReleaseLevel.SNAPSHOT) version.increment(ReleaseLevel.SNAPSHOT)
version.write() version.write()
@ -23,11 +22,17 @@ class ReleaseLevel(Enum):
class Version(): class Version():
def __init__(self, config_file_path): def __init__(self, version, is_snapshot):
self.version = [] self.version = version
self.is_snapshot = False self.is_snapshot = is_snapshot
self.config_file_path = config_file_path
self.config_file_type = config_file_path.split('.')[-1] @classmethod
def from_file(cls, config_file_path):
ret_cls = cls(None, False)
ret_cls.config_file_path = config_file_path
ret_cls.config_file_type = config_file_path.split('.')[-1]
ret_cls.parse()
return ret_cls
def parse(self): def parse(self):
match self.config_file_type: match self.config_file_type:
@ -35,6 +40,8 @@ class Version():
self.__parse_json() self.__parse_json()
case 'gradle': case 'gradle':
self.__parse_gradle() self.__parse_gradle()
case _:
raise Exception(f'The file type "{self.config_file_type}" is not implemented')
def __parse_json(self): def __parse_json(self):
with open(self.config_file_path, 'r') as json_file: with open(self.config_file_path, 'r') as json_file:
@ -51,7 +58,7 @@ class Version():
if version_line is None: if version_line is None:
raise Exception("Version not found in gradle file") raise Exception("Version not found in gradle file")
version_line = version_line.group() version_line = version_line.group()
version_string = re.search('[0-9]*\.[0-9]*\.[0-9]*(-SNAPSHOT)?', version_line) version_string = re.search('[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line)
if version_string is None: if version_string is None:
raise Exception("Version not found in gradle file") raise Exception("Version not found in gradle file")

25
test/test_version.py Normal file
View file

@ -0,0 +1,25 @@
from devops_test import Version, ReleaseLevel
def test_version():
version = Version([1, 2, 3], False)
version.increment(ReleaseLevel.SNAPSHOT)
assert version.get() == "1.2.3-SNAPSHOT"
assert version.version == [1, 2, 3]
assert version.is_snapshot
version.increment(ReleaseLevel.PATCH)
assert version.get() == "1.2.4"
assert version.version == [1, 2, 4]
assert not version.is_snapshot
version.increment(ReleaseLevel.SNAPSHOT)
assert version.get() == "1.2.4-SNAPSHOT"
version.increment(ReleaseLevel.SNAPSHOT)
assert version.get() == "1.2.4-SNAPSHOT"
version.increment(ReleaseLevel.MINOR)
assert version.get() == "1.3.0"
version.increment(ReleaseLevel.MAJOR)
assert version.get() == "2.0.0"