From c8a7954adb29a6a36491bf10714adb7b39d9ab39 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 22 Nov 2020 14:16:08 +0100 Subject: [PATCH] Added Tester to create all tests --- src/tests/publisher.py | 84 ++++++++++++++++++++--------------- src/tests/service_provider.py | 23 ++++++++++ src/tests/test.py | 67 ++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 35 deletions(-) create mode 100644 src/tests/service_provider.py create mode 100644 src/tests/test.py diff --git a/src/tests/publisher.py b/src/tests/publisher.py index b12719fc..43d350c4 100644 --- a/src/tests/publisher.py +++ b/src/tests/publisher.py @@ -1,42 +1,56 @@ +import os + +from sh_edraft.service import ServiceProvider from sh_edraft.source_code.model import Version from sh_edraft.publish import Publisher from sh_edraft.publish.model import Template -if __name__ == '__main__': - templates = [ - Template( - '../../publish_templates/*_template.txt', - '*', - '', - '', - '2020', - 'sh-edraft.de', - 'MIT', - ', see LICENSE for more details.', - '', - 'Sven Heidemann', - Version(2020, 12, 0.1).to_dict() - ), - Template( - '../../publish_templates/*_template.txt', - 'sh_edraft', - 'common python library', - 'Library to share common classes and models used at sh-edraft.de', - '2020', - 'sh-edraft.de', - 'MIT', - ', see LICENSE for more details.', - '', - 'Sven Heidemann', - Version(2020, 12, 0.1).to_dict() - ) - ] - publisher = Publisher('../', '../../dist', templates) +class PublisherTest: - publisher.exclude('../tests/') - publisher.include('../../LICENSE') - publisher.include('../../README.md') + @staticmethod + def start(services: ServiceProvider): + templates = [ + Template( + '../../publish_templates/*_template.txt', + '*', + '', + '', + '2020', + 'sh-edraft.de', + 'MIT', + ', see LICENSE for more details.', + '', + 'Sven Heidemann', + Version(2020, 12, 0.1).to_dict() + ), + Template( + '../../publish_templates/*_template.txt', + 'sh_edraft', + 'common python library', + 'Library to share common classes and models used at sh-edraft.de', + '2020', + 'sh-edraft.de', + 'MIT', + ', see LICENSE for more details.', + '', + 'Sven Heidemann', + Version(2020, 12, 0.1).to_dict() + ) + ] - publisher.create() - publisher.publish() + source = '../' + dist = '../../dist' + + services.add_singleton(Publisher, None, source, dist, templates) + publisher: Publisher = services.get_service(Publisher) + + publisher.exclude('../tests/') + publisher.include('../../LICENSE') + publisher.include('../../README.md') + + publisher.create() + publisher.publish() + + if not os.path.isdir(dist): + raise Exception(f'{__name__}: Dist path was not created') diff --git a/src/tests/service_provider.py b/src/tests/service_provider.py new file mode 100644 index 00000000..06317e85 --- /dev/null +++ b/src/tests/service_provider.py @@ -0,0 +1,23 @@ +from sh_edraft.publish import Publisher +from sh_edraft.service import ServiceProvider + + +class ServiceProviderTest: + + @staticmethod + def start() -> ServiceProvider: + provider = ServiceProvider() + provider.create() + + provider.add_transient(Publisher, None, '../', '../../dist', []) + + publisher: Publisher = provider.get_service(Publisher) + + if publisher.source_path != '../' or publisher.dist_path != '../../dist': + raise Exception(f'{__name__}: Invalid value in {Publisher.__name__}') + + provider.remove_service(Publisher) + if provider.get_service(Publisher) is not None: + raise Exception(f'{__name__}: Service {Publisher.__name__} was not removed') + + return provider diff --git a/src/tests/test.py b/src/tests/test.py new file mode 100644 index 00000000..91b9bb4f --- /dev/null +++ b/src/tests/test.py @@ -0,0 +1,67 @@ +import os +import sys +from typing import Optional + +from termcolor import colored + +from sh_edraft.service import ServiceProvider +from tests.publisher import PublisherTest +from tests.service_provider import ServiceProviderTest + + +class Test: + + def __init__(self): + self._services: Optional[ServiceProvider] = None + + self._tests = [ + ServiceProviderTest, + PublisherTest + ] + + self._error: bool = False + + @staticmethod + def block_print(): + sys.stdout = open(os.devnull, 'w') + + @staticmethod + def enable_print(): + sys.stdout = sys.__stdout__ + + def success(self, message: str): + self.enable_print() + print(colored(message, 'green')) + self.block_print() + + def failed(self, message: str): + self.enable_print() + print(colored(message, 'red')) + self.block_print() + + def create(self): pass + + def start(self): + self.block_print() + + if not self._error: + try: + self._services = ServiceProviderTest.start() + self.success(f'{ServiceProviderTest.__name__} test succeeded.') + except Exception as e: + self._error = True + self.failed(f'{ServiceProviderTest.__name__} test failed!\n{e}') + + if not self._error: + try: + PublisherTest.start(self._services) + self.success(f'{PublisherTest.__name__} test succeeded.') + except Exception as e: + self._error = True + self.failed(f'{PublisherTest.__name__} test failed!\n{e}') + + +if __name__ == '__main__': + test = Test() + test.create() + test.start()