diff --git a/src/main/python/ddadevops/__init__.py b/src/main/python/ddadevops/__init__.py index 8f5924d..3df0197 100644 --- a/src/main/python/ddadevops/__init__.py +++ b/src/main/python/ddadevops/__init__.py @@ -19,4 +19,7 @@ from .devops_terraform_build import DevopsTerraformBuild, create_devops_terrafor from .devops_build import DevopsBuild, create_devops_build_config, get_devops_build, get_tag_from_latest_commit from .credential import gopass_password_from_path, gopass_field_from_path +from .domain import Validateable, Build +from .application import BuildService + __version__ = "${version}" diff --git a/src/main/python/ddadevops/application.py b/src/main/python/ddadevops/application.py new file mode 100644 index 0000000..014f871 --- /dev/null +++ b/src/main/python/ddadevops/application.py @@ -0,0 +1,8 @@ +from .domain import Build +from .python_util import execute + + +class BuildService(): + def initialize_build_dir(self, build: Build): + execute('rm -rf ' + build.build_path(), shell=True) + execute('mkdir -p ' + build.build_path(), shell=True) diff --git a/src/main/python/ddadevops/devops_build.py b/src/main/python/ddadevops/devops_build.py index 4c6f403..d48999b 100644 --- a/src/main/python/ddadevops/devops_build.py +++ b/src/main/python/ddadevops/devops_build.py @@ -1,5 +1,5 @@ -from subprocess import run, CalledProcessError -from .python_util import filter_none +from .domain import Build +from .application import BuildService def create_devops_build_config(stage, project_root_path, module, build_dir_name='target'): @@ -11,48 +11,26 @@ def create_devops_build_config(stage, project_root_path, module, def get_devops_build(project): return project.get_property('devops_build') -def get_tag_from_latest_commit(): - try: - value = run('git describe --abbrev=0 --tags --exact-match', shell=True, - capture_output=True, check=True) - return value.stdout.decode('UTF-8').rstrip() - except CalledProcessError: - return None +# def get_tag_from_latest_commit(): +# try: +# value = run('git describe --abbrev=0 --tags --exact-match', shell=True, +# capture_output=True, check=True) +# return value.stdout.decode('UTF-8').rstrip() +# except CalledProcessError: +# return None class DevopsBuild: def __init__(self, project, config): - #deprecate stage - self.stage = config['stage'] - self.project_root_path = config['project_root_path'] - self.module = config['module'] - self.build_dir_name = config['build_dir_name'] - self.stack = {} - self.project = project + self.build = Build(project, config) + self.build_service = BuildService() project.set_property('devops_build', self) def name(self): - return self.project.name + return self.build.name() def build_path(self): - mylist = [self.project_root_path, - self.build_dir_name, - self.name(), - self.module] - return '/'.join(filter_none(mylist)) + return self.build.build_path() def initialize_build_dir(self): - run('rm -rf ' + self.build_path(), shell=True, check=True) - run('mkdir -p ' + self.build_path(), shell=True, check=True) - - def put(self, key, value): - self.stack[key] = value - - def get(self, key): - return self.stack[key] - - def get_keys(self, keys): - result = {} - for key in keys: - result[key] = self.get(key) - return result + self.build_service.initialize_build_dir(self.build) diff --git a/src/main/python/ddadevops/domain.py b/src/main/python/ddadevops/domain.py new file mode 100644 index 0000000..5c6c1b7 --- /dev/null +++ b/src/main/python/ddadevops/domain.py @@ -0,0 +1,50 @@ +from typing import List +from .python_util import filter_none + + +class Validateable(): + def validate(self) -> List[str]: + return [] + + def is_valid(self) -> bool: + return len(self.validate()) < 1 + + def validate_is_not_empty(self, field_name: str) -> List[str]: + value = self.__dict__[field_name] + if value is None or value == '': + return [f"Field '{field_name}' may not be empty."] + else: + return [] + +class Build(Validateable): + + def __init__(self, project, config): + # deprecate stage + self.stage = config['stage'] + self.project_root_path = config['project_root_path'] + self.module = config['module'] + self.build_dir_name = config['build_dir_name'] + self.stack = {} + self.project = project + + def name(self): + return self.project.name + + def build_path(self): + path = [self.project_root_path, + self.build_dir_name, + self.name(), + self.module] + return '/'.join(filter_none(path)) + + def put(self, key, value): + self.stack[key] = value + + def get(self, key): + return self.stack[key] + + def get_keys(self, keys): + result = {} + for key in keys: + result[key] = self.get(key) + return result diff --git a/src/test/test_domain.py b/src/test/test_domain.py new file mode 100644 index 0000000..861f850 --- /dev/null +++ b/src/test/test_domain.py @@ -0,0 +1,41 @@ +from src.main.python.ddadevops.domain import Validateable + + +class TestValidateable(Validateable): + def __init__(self, value): + self.field = value + + def validate(self): + return self.validate_is_not_empty('field') + + +def test_should_validate_non_empty_strings(): + + sut = TestValidateable("content") + assert sut.is_valid() + + sut = TestValidateable(None) + assert not sut.is_valid() + + sut = TestValidateable('') + assert not sut.is_valid() + + +def test_should_validate_non_empty_others(): + + sut = TestValidateable(1) + assert sut.is_valid() + + sut = TestValidateable(1.0) + assert sut.is_valid() + + sut = TestValidateable(True) + assert sut.is_valid() + + sut = TestValidateable(None) + assert not sut.is_valid() + +def test_validate_with_reason(): + + sut = TestValidateable(None) + assert sut.validate()[0] == "Field 'field' may not be empty."