From 6a0ed7deefe15b0392a581f6d2233085c71221d6 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Thu, 11 May 2023 18:37:47 +0200 Subject: [PATCH] get_version now works --- .../python/ddadevops/domain/build_file.py | 39 ++++++--- src/test/python/domain/test_build_file.py | 86 +++++++++++++------ 2 files changed, 85 insertions(+), 40 deletions(-) diff --git a/src/main/python/ddadevops/domain/build_file.py b/src/main/python/ddadevops/domain/build_file.py index 321ab33..d15f038 100644 --- a/src/main/python/ddadevops/domain/build_file.py +++ b/src/main/python/ddadevops/domain/build_file.py @@ -49,16 +49,31 @@ class BuildFile(Validateable): return result def get_version(self) -> Version: - version = None - match self.build_file_type(): - case BuildFileType.JS: - print(json.loads(self.content)) - version = json.loads(self.content)["version"] - print(version) - case BuildFileType.JAVA_GRADLE: - version_line = re.search("\nversion = .*", self.content) - version_line_group = version_line.group() - version_string = re.search( - '[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line_group) - version = version_string.group() + try: + match self.build_file_type(): + case BuildFileType.JS: + print(json.loads(self.content)) + version = json.loads(self.content)["version"] + print(version) + case BuildFileType.JAVA_GRADLE: + version_line = re.search("\nversion = .*", self.content) + version_line_group = version_line.group() + version_string = re.search( + '[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line_group) + version = version_string.group() + case BuildFileType.PYTHON: + version_line = re.search("\nversion = .*\n", self.content) + version_line_group = version_line.group() + version_string = re.search( + '[0-9]*\\.[0-9]*\\.[0-9]*(-dev)?[0-9]*', version_line_group) + version = version_string.group() + case BuildFileType.JAVA_CLOJURE: + version_line = re.search("\\(defproject .*\n", self.content) + version_line_group = version_line.group() + version_string = re.search( + '[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line_group) + version = version_string.group() + except: + raise Exception(f"Version not found in file {self.file_path}") + return Version.from_str(version) diff --git a/src/test/python/domain/test_build_file.py b/src/test/python/domain/test_build_file.py index 33a5f19..4db3076 100644 --- a/src/test/python/domain/test_build_file.py +++ b/src/test/python/domain/test_build_file.py @@ -1,3 +1,4 @@ +import pytest from pathlib import Path from src.main.python.ddadevops.domain import ( BuildFileType, @@ -7,48 +8,32 @@ from src.main.python.ddadevops.domain import ( def test_sould_validate_build_file(): - sut = BuildFile( - Path("./project.clj"), - "content" - ) + sut = BuildFile(Path("./project.clj"), "content") assert sut.is_valid() - sut = BuildFile( - None, - "" - ) + sut = BuildFile(None, "") assert not sut.is_valid() - sut = BuildFile( - Path("./unknown.extension"), - "content" - ) + sut = BuildFile(Path("./unknown.extension"), "content") assert not sut.is_valid() def test_sould_calculate_build_type(): - sut = BuildFile( - Path("./project.clj"), - "content" - ) + sut = BuildFile(Path("./project.clj"), "content") assert sut.build_file_type() == BuildFileType.JAVA_CLOJURE - sut = BuildFile( - Path("./build.gradle"), - "content" - ) + sut = BuildFile(Path("./build.gradle"), "content") assert sut.build_file_type() == BuildFileType.JAVA_GRADLE - sut = BuildFile( - Path("./package.json"), - "content" - ) + sut = BuildFile(Path("./package.json"), "content") assert sut.build_file_type() == BuildFileType.JS -def test_sould_parse_version(): + +def test_sould_parse_js(): sut = BuildFile( Path("./package.json"), - """{ + """ +{ "name":"c4k-jira", "description": "Generate c4k yaml for a jira deployment.", "author": "meissa GmbH", @@ -58,16 +43,61 @@ def test_sould_parse_version(): "c4k-jira": "./c4k-jira.js" } } -""" +""", ) assert sut.get_version() == Version.from_str("1.1.5-SNAPSHOT") + sut = BuildFile( + Path("./package.json"), + """ +{ + "name":"c4k-jira", + "description": "Generate c4k yaml for a jira deployment.", + "author": "meissa GmbH", + "homepage": "https://gitlab.com/domaindrivenarchitecture/c4k-jira#readme", + "bin":{ + "c4k-jira": "./c4k-jira.js" + } +} +""", + ) + with pytest.raises(Exception): + sut.get_version() + + +def test_sould_parse_gradle(): sut = BuildFile( Path("./build.gradle"), """ version = "1.1.5-SNAPSHOT" -""" +""", ) assert sut.get_version() == Version.from_str("1.1.5-SNAPSHOT") + +def test_sould_parse_py(): + sut = BuildFile( + Path("./build.py"), + """ +from pybuilder.core import init, use_plugin, Author +use_plugin("python.core") + +name = "ddadevops" +version = "1.1.5-dev" +""", + ) + assert sut.get_version() == Version.from_str("1.1.5-dev") + + +def test_sould_parse_clj(): + sut = BuildFile( + Path("./project.clj"), + """ +(defproject org.domaindrivenarchitecture/c4k-jira "1.1.5-SNAPSHOT" + :description "jira c4k-installation package" + :url "https://domaindrivenarchitecture.org" + ) +""", + ) + assert sut.get_version() == Version.from_str("1.1.5-SNAPSHOT")