From 4a34763fb2bc14ef586eb4900b615c18d6304e0a Mon Sep 17 00:00:00 2001 From: bom Date: Thu, 9 Feb 2023 12:02:31 +0100 Subject: [PATCH] Add basic tests for version increment --- devops_test.py | 23 +++++++++++++++-------- test/test_version.py | 25 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 test/test_version.py diff --git a/devops_test.py b/devops_test.py index 0ba4358..8adcb5f 100644 --- a/devops_test.py +++ b/devops_test.py @@ -4,8 +4,7 @@ import re def init_project(): # validate_values() - version = Version('build.gradle') - version.parse() + version = Version.from_file('build.gradle') version.increment(ReleaseLevel.SNAPSHOT) version.write() @@ -23,11 +22,17 @@ class ReleaseLevel(Enum): class Version(): - def __init__(self, config_file_path): - self.version = [] - self.is_snapshot = False - self.config_file_path = config_file_path - self.config_file_type = config_file_path.split('.')[-1] + def __init__(self, version, is_snapshot): + self.version = version + self.is_snapshot = is_snapshot + + @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): match self.config_file_type: @@ -35,6 +40,8 @@ class Version(): self.__parse_json() case 'gradle': self.__parse_gradle() + case _: + raise Exception(f'The file type "{self.config_file_type}" is not implemented') def __parse_json(self): with open(self.config_file_path, 'r') as json_file: @@ -51,7 +58,7 @@ class Version(): if version_line is None: raise Exception("Version not found in gradle file") 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: raise Exception("Version not found in gradle file") diff --git a/test/test_version.py b/test/test_version.py new file mode 100644 index 0000000..d72416b --- /dev/null +++ b/test/test_version.py @@ -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" \ No newline at end of file