From ce0ad4013f88819f31eb5b0c487cca83960ebd2c Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 26 May 2022 21:42:43 +0200 Subject: [PATCH] [WIP] Improved unittest --- unittests/unittests_cli/cli_test_suite.py | 26 +++++++++---------- unittests/unittests_cli/constants.py | 2 +- unittests/unittests_cli/generate_test_case.py | 9 +++++++ unittests/unittests_cli/new_test_case.py | 26 +++++++++++++++++-- unittests/unittests_shared/cli_commands.py | 10 +++---- 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/unittests/unittests_cli/cli_test_suite.py b/unittests/unittests_cli/cli_test_suite.py index bb53effd..e64b3e7f 100644 --- a/unittests/unittests_cli/cli_test_suite.py +++ b/unittests/unittests_cli/cli_test_suite.py @@ -25,27 +25,27 @@ class CLITestSuite(unittest.TestSuite): loader = unittest.TestLoader() # nothing needed - self.addTests(loader.loadTestsFromTestCase(GenerateTestCase)) + # self.addTests(loader.loadTestsFromTestCase(GenerateTestCase)) self.addTests(loader.loadTestsFromTestCase(NewTestCase)) - self.addTests(loader.loadTestsFromTestCase(VersionTestCase)) + # self.addTests(loader.loadTestsFromTestCase(VersionTestCase)) # project needed - self.addTests(loader.loadTestsFromTestCase(BuildTestCase)) - self.addTests(loader.loadTestsFromTestCase(InstallTestCase)) - self.addTests(loader.loadTestsFromTestCase(PublishTestCase)) - self.addTests(loader.loadTestsFromTestCase(RunTestCase)) - self.addTests(loader.loadTestsFromTestCase(StartTestCase)) - self.addTests(loader.loadTestsFromTestCase(UninstallTestCase)) - self.addTests(loader.loadTestsFromTestCase(UpdateTestCase)) + # self.addTests(loader.loadTestsFromTestCase(BuildTestCase)) + # self.addTests(loader.loadTestsFromTestCase(InstallTestCase)) + # self.addTests(loader.loadTestsFromTestCase(PublishTestCase)) + # self.addTests(loader.loadTestsFromTestCase(RunTestCase)) + # self.addTests(loader.loadTestsFromTestCase(StartTestCase)) + # self.addTests(loader.loadTestsFromTestCase(UninstallTestCase)) + # self.addTests(loader.loadTestsFromTestCase(UpdateTestCase)) # workspace needed - self.addTests(loader.loadTestsFromTestCase(AddTestCase)) - self.addTests(loader.loadTestsFromTestCase(RemoveTestCase)) + # self.addTests(loader.loadTestsFromTestCase(AddTestCase)) + # self.addTests(loader.loadTestsFromTestCase(RemoveTestCase)) def _setup(self): try: if os.path.exists(PLAYGROUND_PATH): - shutil.rmtree(os.path.abspath(os.path.join(PLAYGROUND_PATH, '../'))) + shutil.rmtree(os.path.abspath(os.path.join(PLAYGROUND_PATH))) os.makedirs(PLAYGROUND_PATH) os.chdir(PLAYGROUND_PATH) @@ -55,7 +55,7 @@ class CLITestSuite(unittest.TestSuite): def _cleanup(self): try: if os.path.exists(PLAYGROUND_PATH): - shutil.rmtree(os.path.abspath(os.path.join(PLAYGROUND_PATH, '../'))) + shutil.rmtree(os.path.abspath(os.path.join(PLAYGROUND_PATH))) except Exception as e: print(f'Cleanup of {__name__} failed: {traceback.format_exc()}') diff --git a/unittests/unittests_cli/constants.py b/unittests/unittests_cli/constants.py index 917dda78..766d72c0 100644 --- a/unittests/unittests_cli/constants.py +++ b/unittests/unittests_cli/constants.py @@ -1,4 +1,4 @@ import os -PLAYGROUND_PATH = os.path.abspath(os.path.join(os.getcwd(), '../generated/test_cli_playground')) +PLAYGROUND_PATH = os.path.abspath(os.path.join(os.getcwd(), '../test_cli_playground')) CLI_PATH = os.path.abspath(os.path.join(os.getcwd(), '../../src/cpl_cli/main.py')) diff --git a/unittests/unittests_cli/generate_test_case.py b/unittests/unittests_cli/generate_test_case.py index ecc11e68..5a718416 100644 --- a/unittests/unittests_cli/generate_test_case.py +++ b/unittests/unittests_cli/generate_test_case.py @@ -15,28 +15,37 @@ class GenerateTestCase(unittest.TestCase): os.remove(file_path) def test_abc(self): + print(f'{__name__} generate abc') self._test_file('abc', '_abc') def test_class(self): + print(f'{__name__} generate class') self._test_file('class', '') def test_enum(self): + print(f'{__name__} generate enum') self._test_file('enum', '_enum') def test_pipe(self): + print(f'{__name__} generate pipe') self._test_file('pipe', '_pipe') def test_service(self): + print(f'{__name__} generate service') self._test_file('service', '_service') def test_settings(self): + print(f'{__name__} generate settings') self._test_file('settings', '_settings') def test_test_case(self): + print(f'{__name__} generate test_case') self._test_file('test_case', '_test_case') def test_thread(self): + print(f'{__name__} generate thread') self._test_file('thread', '_thread') def test_validator(self): + print(f'{__name__} generate validator') self._test_file('validator', '_validator') diff --git a/unittests/unittests_cli/new_test_case.py b/unittests/unittests_cli/new_test_case.py index d902e011..1309eb99 100644 --- a/unittests/unittests_cli/new_test_case.py +++ b/unittests/unittests_cli/new_test_case.py @@ -1,10 +1,32 @@ +import os +import shutil import unittest +from cpl_core.utils import String + +from unittests_cli.constants import PLAYGROUND_PATH +from unittests_shared.cli_commands import CLICommands + class NewTestCase(unittest.TestCase): def setUp(self): pass - def test_equal(self): - pass + def _test_project(self, project_type: str, name: str, *args): + CLICommands.new(project_type, name, *args) + project_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, String.convert_to_snake_case(name))) + self.assertTrue(os.path.exists(project_path)) + shutil.rmtree(project_path) + + def test_console(self): + print(f'{__name__} new console') + self._test_project('console', 'test-console', '--ab', '--s', '--sp') + + def test_library(self): + print(f'{__name__} new library') + self._test_project('library', 'test-library', '--ab', '--s', '--sp') + + def test_unittest(self): + print(f'{__name__} new unittests') + self._test_project('unittest', 'test-unittest', '--ab', '--s', '--sp') diff --git a/unittests/unittests_shared/cli_commands.py b/unittests/unittests_shared/cli_commands.py index 4577e8e0..9465effd 100644 --- a/unittests/unittests_shared/cli_commands.py +++ b/unittests/unittests_shared/cli_commands.py @@ -9,7 +9,7 @@ class CLICommands: @staticmethod def _run(cmd: str, *args): env_vars = os.environ - env_vars['CPL_IS_UNITTEST'] = 'YES' + # env_vars['CPL_IS_UNITTEST'] = 'YES' command = ['python', CLI_PATH, cmd] for arg in args: command.append(arg) @@ -17,9 +17,9 @@ class CLICommands: subprocess.run(command, env=env_vars) @classmethod - def generate(cls, *args): - cls._run('generate', *args) + def generate(cls, schematic: str, name: str): + cls._run('generate', schematic, name) @classmethod - def new(cls, *args): - cls._run('new', *args) + def new(cls, project_type: str, name: str, *args): + cls._run('new', project_type, name, *args)