Improved unittests & added tests for cpl new

This commit is contained in:
Sven Heidemann 2022-05-26 22:21:16 +02:00
parent ce0ad4013f
commit 833dd83008
4 changed files with 35 additions and 29 deletions

View File

@ -2,20 +2,12 @@ import os
import shutil import shutil
import traceback import traceback
import unittest 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.constants import PLAYGROUND_PATH
from unittests_cli.generate_test_case import GenerateTestCase 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.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): class CLITestSuite(unittest.TestSuite):
@ -24,8 +16,9 @@ class CLITestSuite(unittest.TestSuite):
unittest.TestSuite.__init__(self) unittest.TestSuite.__init__(self)
loader = unittest.TestLoader() loader = unittest.TestLoader()
self._result: Optional[TestResult] = None
# nothing needed # nothing needed
# self.addTests(loader.loadTestsFromTestCase(GenerateTestCase)) self.addTests(loader.loadTestsFromTestCase(GenerateTestCase))
self.addTests(loader.loadTestsFromTestCase(NewTestCase)) self.addTests(loader.loadTestsFromTestCase(NewTestCase))
# self.addTests(loader.loadTestsFromTestCase(VersionTestCase)) # self.addTests(loader.loadTestsFromTestCase(VersionTestCase))
@ -54,6 +47,9 @@ class CLITestSuite(unittest.TestSuite):
def _cleanup(self): def _cleanup(self):
try: 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): 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: except Exception as e:
@ -61,5 +57,5 @@ class CLITestSuite(unittest.TestSuite):
def run(self, *args): def run(self, *args):
self._setup() self._setup()
super().run(*args) self._result = super().run(*args)
self._cleanup() self._cleanup()

View File

@ -12,40 +12,30 @@ class GenerateTestCase(unittest.TestCase):
file_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, f'generated_file{suffix}.py')) file_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, f'generated_file{suffix}.py'))
file_exists = os.path.exists(file_path) file_exists = os.path.exists(file_path)
self.assertTrue(file_exists) self.assertTrue(file_exists)
os.remove(file_path)
def test_abc(self): def test_abc(self):
print(f'{__name__} generate abc')
self._test_file('abc', '_abc') self._test_file('abc', '_abc')
def test_class(self): def test_class(self):
print(f'{__name__} generate class')
self._test_file('class', '') self._test_file('class', '')
def test_enum(self): def test_enum(self):
print(f'{__name__} generate enum')
self._test_file('enum', '_enum') self._test_file('enum', '_enum')
def test_pipe(self): def test_pipe(self):
print(f'{__name__} generate pipe')
self._test_file('pipe', '_pipe') self._test_file('pipe', '_pipe')
def test_service(self): def test_service(self):
print(f'{__name__} generate service')
self._test_file('service', '_service') self._test_file('service', '_service')
def test_settings(self): def test_settings(self):
print(f'{__name__} generate settings')
self._test_file('settings', '_settings') self._test_file('settings', '_settings')
def test_test_case(self): def test_test_case(self):
print(f'{__name__} generate test_case')
self._test_file('test_case', '_test_case') self._test_file('test_case', '_test_case')
def test_thread(self): def test_thread(self):
print(f'{__name__} generate thread')
self._test_file('thread', '_thread') self._test_file('thread', '_thread')
def test_validator(self): def test_validator(self):
print(f'{__name__} generate validator')
self._test_file('validator', '_validator') self._test_file('validator', '_validator')

View File

@ -11,22 +11,42 @@ from unittests_shared.cli_commands import CLICommands
class NewTestCase(unittest.TestCase): class NewTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
pass os.chdir(os.path.abspath(PLAYGROUND_PATH))
def _test_project(self, project_type: str, name: str, *args): def _test_project(self, project_type: str, name: str, *args):
CLICommands.new(project_type, name, *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)) 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): def test_console(self):
print(f'{__name__} new console')
self._test_project('console', 'test-console', '--ab', '--s', '--sp') 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): def test_library(self):
print(f'{__name__} new library')
self._test_project('library', 'test-library', '--ab', '--s', '--sp') 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): def test_unittest(self):
print(f'{__name__} new unittests')
self._test_project('unittest', 'test-unittest', '--ab', '--s', '--sp') 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')

View File

@ -9,7 +9,7 @@ class CLICommands:
@staticmethod @staticmethod
def _run(cmd: str, *args): def _run(cmd: str, *args):
env_vars = os.environ env_vars = os.environ
# env_vars['CPL_IS_UNITTEST'] = 'YES' env_vars['CPL_IS_UNITTEST'] = 'YES'
command = ['python', CLI_PATH, cmd] command = ['python', CLI_PATH, cmd]
for arg in args: for arg in args:
command.append(arg) command.append(arg)