From 833dd83008e0a51e9ca1b603801373d49109a9c1 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 26 May 2022 22:21:16 +0200 Subject: [PATCH] Improved unittests & added tests for cpl new --- unittests/unittests_cli/cli_test_suite.py | 20 +++++------- unittests/unittests_cli/generate_test_case.py | 10 ------ unittests/unittests_cli/new_test_case.py | 32 +++++++++++++++---- unittests/unittests_shared/cli_commands.py | 2 +- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/unittests/unittests_cli/cli_test_suite.py b/unittests/unittests_cli/cli_test_suite.py index e64b3e7f..f1a7d7ea 100644 --- a/unittests/unittests_cli/cli_test_suite.py +++ b/unittests/unittests_cli/cli_test_suite.py @@ -2,20 +2,12 @@ import os import shutil import traceback import unittest +from typing import Optional +from unittest import TestResult -from unittests_cli.add_test_case import AddTestCase -from unittests_cli.build_test_case import BuildTestCase from unittests_cli.constants import PLAYGROUND_PATH from unittests_cli.generate_test_case import GenerateTestCase -from unittests_cli.install_test_case import InstallTestCase from unittests_cli.new_test_case import NewTestCase -from unittests_cli.publish_test_case import PublishTestCase -from unittests_cli.remove_test_case import RemoveTestCase -from unittests_cli.run_test_case import RunTestCase -from unittests_cli.start_test_case import StartTestCase -from unittests_cli.uninstall_test_case import UninstallTestCase -from unittests_cli.update_test_case import UpdateTestCase -from unittests_cli.version_test_case import VersionTestCase class CLITestSuite(unittest.TestSuite): @@ -24,8 +16,9 @@ class CLITestSuite(unittest.TestSuite): unittest.TestSuite.__init__(self) loader = unittest.TestLoader() + self._result: Optional[TestResult] = None # nothing needed - # self.addTests(loader.loadTestsFromTestCase(GenerateTestCase)) + self.addTests(loader.loadTestsFromTestCase(GenerateTestCase)) self.addTests(loader.loadTestsFromTestCase(NewTestCase)) # self.addTests(loader.loadTestsFromTestCase(VersionTestCase)) @@ -54,6 +47,9 @@ class CLITestSuite(unittest.TestSuite): def _cleanup(self): try: + if self._result is not None and (len(self._result.errors) > 0 or len(self._result.failures) > 0): + return + if os.path.exists(PLAYGROUND_PATH): shutil.rmtree(os.path.abspath(os.path.join(PLAYGROUND_PATH))) except Exception as e: @@ -61,5 +57,5 @@ class CLITestSuite(unittest.TestSuite): def run(self, *args): self._setup() - super().run(*args) + self._result = super().run(*args) self._cleanup() diff --git a/unittests/unittests_cli/generate_test_case.py b/unittests/unittests_cli/generate_test_case.py index 5a718416..53c03839 100644 --- a/unittests/unittests_cli/generate_test_case.py +++ b/unittests/unittests_cli/generate_test_case.py @@ -12,40 +12,30 @@ class GenerateTestCase(unittest.TestCase): file_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, f'generated_file{suffix}.py')) file_exists = os.path.exists(file_path) self.assertTrue(file_exists) - 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 1309eb99..53072308 100644 --- a/unittests/unittests_cli/new_test_case.py +++ b/unittests/unittests_cli/new_test_case.py @@ -11,22 +11,42 @@ from unittests_shared.cli_commands import CLICommands class NewTestCase(unittest.TestCase): def setUp(self): - pass + os.chdir(os.path.abspath(PLAYGROUND_PATH)) 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))) + workspace_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, name)) + self.assertTrue(os.path.exists(workspace_path)) + + project_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, name, 'src', String.convert_to_snake_case(name))) self.assertTrue(os.path.exists(project_path)) - shutil.rmtree(project_path) + self.assertTrue(os.path.join(project_path, f'{name}.json')) + + def _test_sub_project(self, project_type: str, name: str, workspace_name: str, *args): + os.chdir(os.path.abspath(os.path.join(os.getcwd(), workspace_name))) + CLICommands.new(project_type, name, *args) + workspace_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, workspace_name)) + self.assertTrue(os.path.exists(workspace_path)) + + project_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, workspace_name, 'src', String.convert_to_snake_case(name))) + self.assertTrue(os.path.exists(project_path)) + self.assertTrue(os.path.join(project_path, f'{name}.json')) + os.chdir(os.path.abspath(os.path.join(os.getcwd(), '../'))) def test_console(self): - print(f'{__name__} new console') self._test_project('console', 'test-console', '--ab', '--s', '--sp') + def test_sub_console(self): + self._test_sub_project('console', 'test-sub-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_sub_library(self): + self._test_sub_project('library', 'test-sub-library', 'test-console', '--ab', '--s', '--sp') + def test_unittest(self): - print(f'{__name__} new unittests') self._test_project('unittest', 'test-unittest', '--ab', '--s', '--sp') + + def test_sub_unittest(self): + self._test_sub_project('unittest', 'test-unittest', 'test-console', '--ab', '--s', '--sp') diff --git a/unittests/unittests_shared/cli_commands.py b/unittests/unittests_shared/cli_commands.py index 9465effd..da9c1ce7 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)