From d937c4c0e67841003e67f0f1a23f14d89b0b5ff6 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 26 May 2022 14:47:36 +0200 Subject: [PATCH] Added tests for generate command --- src/tests/unittests/application.py | 2 +- src/tests/unittests/unittests.json | 4 +- src/tests/unittests_cli/cli_test_suite.py | 27 +++++++------ src/tests/unittests_cli/constants.py | 3 ++ src/tests/unittests_cli/generate_test_case.py | 39 ++++++++++++++++++- src/tests/unittests_cli/unittests_cli.json | 4 +- src/tests/unittests_core/unittests_core.json | 4 +- .../unittests_query/unittests_query.json | 4 +- src/tests/unittests_shared/cli_commands.py | 23 +++++++++++ .../unittests_shared/unittests_shared.json | 4 +- 10 files changed, 89 insertions(+), 25 deletions(-) create mode 100644 src/tests/unittests_cli/constants.py create mode 100644 src/tests/unittests_shared/cli_commands.py diff --git a/src/tests/unittests/application.py b/src/tests/unittests/application.py index 105f0a8c..2b7af5c8 100644 --- a/src/tests/unittests/application.py +++ b/src/tests/unittests/application.py @@ -1,5 +1,5 @@ +import time import unittest -from unittest import TestSuite from cpl_core.application import ApplicationABC from cpl_core.configuration import ConfigurationABC diff --git a/src/tests/unittests/unittests.json b/src/tests/unittests/unittests.json index 5b8bd202..3229906b 100644 --- a/src/tests/unittests/unittests.json +++ b/src/tests/unittests/unittests.json @@ -28,8 +28,8 @@ "ProjectType": "unittest", "SourcePath": "", "OutputPath": "../../dist", - "Main": "tests/unittest.main", - "EntryPoint": "tests/unittest", + "Main": "unittest.main", + "EntryPoint": "unittest", "IncludePackageData": false, "Included": [], "Excluded": [ diff --git a/src/tests/unittests_cli/cli_test_suite.py b/src/tests/unittests_cli/cli_test_suite.py index 91d3e05b..eb82349a 100644 --- a/src/tests/unittests_cli/cli_test_suite.py +++ b/src/tests/unittests_cli/cli_test_suite.py @@ -1,9 +1,11 @@ import os +import shutil import traceback import unittest from unittests_cli.add_test_case import AddTestCase from unittests_cli.build_test_case import BuildTestCase +from unittests_cli.constants import PLAYGROUND from unittests_cli.generate_test_case import GenerateTestCase from unittests_cli.install_test_case import InstallTestCase from unittests_cli.new_test_case import NewTestCase @@ -21,8 +23,6 @@ class CLITestSuite(unittest.TestSuite): def __init__(self): unittest.TestSuite.__init__(self) - self._setup() - loader = unittest.TestLoader() # nothing needed self.addTests(loader.loadTestsFromTestCase(GenerateTestCase)) @@ -42,23 +42,26 @@ class CLITestSuite(unittest.TestSuite): self.addTests(loader.loadTestsFromTestCase(AddTestCase)) self.addTests(loader.loadTestsFromTestCase(RemoveTestCase)) - self._cleanup() - def _setup(self): + print(f'Setup {__name__}') try: - playground = os.path.abspath(os.path.join(os.getcwd(), 'test_cli_playground')) - if os.path.exists(playground): - os.rmdir(playground) + if os.path.exists(PLAYGROUND): + shutil.rmtree(PLAYGROUND) - os.mkdir(playground) - os.chdir(playground) + os.mkdir(PLAYGROUND) + os.chdir(PLAYGROUND) except Exception as e: print(f'Setup of {__name__} failed: {traceback.format_exc()}') def _cleanup(self): + print(f'Cleanup {__name__}') try: - playground = os.path.abspath(os.path.join(os.getcwd(), 'test_cli_playground')) - if os.path.exists(playground): - os.rmdir(playground) + if os.path.exists(PLAYGROUND): + shutil.rmtree(PLAYGROUND) except Exception as e: print(f'Cleanup of {__name__} failed: {traceback.format_exc()}') + + def run(self, *args): + self._setup() + super().run(*args) + self._cleanup() diff --git a/src/tests/unittests_cli/constants.py b/src/tests/unittests_cli/constants.py new file mode 100644 index 00000000..272a19e0 --- /dev/null +++ b/src/tests/unittests_cli/constants.py @@ -0,0 +1,3 @@ +import os + +PLAYGROUND = os.path.abspath(os.path.join(os.getcwd(), '../generated/test_cli_playground')) \ No newline at end of file diff --git a/src/tests/unittests_cli/generate_test_case.py b/src/tests/unittests_cli/generate_test_case.py index e525695a..5530ccba 100644 --- a/src/tests/unittests_cli/generate_test_case.py +++ b/src/tests/unittests_cli/generate_test_case.py @@ -1,10 +1,45 @@ +import os.path import unittest +from unittests_cli.constants import PLAYGROUND +from unittests_shared.cli_commands import CLICommands + class GenerateTestCase(unittest.TestCase): def setUp(self): pass - def test_equal(self): - pass + def _test_file(self, schematic: str, suffix: str): + CLICommands.generate(schematic, 'GeneratedFile') + file_path = os.path.abspath(os.path.join(PLAYGROUND, f'generated_file{suffix}.py')) + file_exists = os.path.exists(file_path) + self.assertTrue(file_exists) + os.remove(file_path) + + def test_abc(self): + self._test_file('abc', '_abc') + + def test_class(self): + self._test_file('class', '') + + def test_enum(self): + self._test_file('enum', '_enum') + + def test_pipe(self): + self._test_file('pipe', '_pipe') + + def test_service(self): + self._test_file('service', '_service') + + def test_settings(self): + self._test_file('settings', '_settings') + + def test_test_case(self): + self._test_file('test_case', '_test_case') + + def test_thread(self): + self._test_file('thread', '_thread') + + def test_validator(self): + self._test_file('validator', '_validator') diff --git a/src/tests/unittests_cli/unittests_cli.json b/src/tests/unittests_cli/unittests_cli.json index 2288876a..44b9eb9f 100644 --- a/src/tests/unittests_cli/unittests_cli.json +++ b/src/tests/unittests_cli/unittests_cli.json @@ -28,8 +28,8 @@ "ProjectType": "library", "SourcePath": "", "OutputPath": "../../dist", - "Main": "tests/unittest_cli.main", - "EntryPoint": "tests/unittest_cli", + "Main": "unittest_cli.main", + "EntryPoint": "unittest_cli", "IncludePackageData": false, "Included": [], "Excluded": [ diff --git a/src/tests/unittests_core/unittests_core.json b/src/tests/unittests_core/unittests_core.json index 99fd0717..33e28883 100644 --- a/src/tests/unittests_core/unittests_core.json +++ b/src/tests/unittests_core/unittests_core.json @@ -28,8 +28,8 @@ "ProjectType": "library", "SourcePath": "", "OutputPath": "../../dist", - "Main": "tests/unittest_core.main", - "EntryPoint": "tests/unittest_core", + "Main": "unittest_core.main", + "EntryPoint": "unittest_core", "IncludePackageData": false, "Included": [], "Excluded": [ diff --git a/src/tests/unittests_query/unittests_query.json b/src/tests/unittests_query/unittests_query.json index 655d4a42..ae2b1bd4 100644 --- a/src/tests/unittests_query/unittests_query.json +++ b/src/tests/unittests_query/unittests_query.json @@ -28,8 +28,8 @@ "ProjectType": "library", "SourcePath": "", "OutputPath": "../../dist", - "Main": "tests/unittest_query.main", - "EntryPoint": "tests/unittest_query", + "Main": "unittest_query.main", + "EntryPoint": "unittest_query", "IncludePackageData": false, "Included": [], "Excluded": [ diff --git a/src/tests/unittests_shared/cli_commands.py b/src/tests/unittests_shared/cli_commands.py new file mode 100644 index 00000000..23cb5589 --- /dev/null +++ b/src/tests/unittests_shared/cli_commands.py @@ -0,0 +1,23 @@ +import os +import subprocess + + +class CLICommands: + + @staticmethod + def _run(cmd: str, *args): + env_vars = os.environ + command = ['python', os.path.abspath(os.path.join(os.getcwd(), '../../../cpl_cli/main.py')), cmd] + for arg in args: + command.append(arg) + + print(f'Running {command}') + subprocess.run(command, env=env_vars) + + @classmethod + def generate(cls, *args): + cls._run('generate', *args) + + @classmethod + def new(cls, *args): + cls._run('new', *args) diff --git a/src/tests/unittests_shared/unittests_shared.json b/src/tests/unittests_shared/unittests_shared.json index 907ab2ec..d1914479 100644 --- a/src/tests/unittests_shared/unittests_shared.json +++ b/src/tests/unittests_shared/unittests_shared.json @@ -28,8 +28,8 @@ "ProjectType": "library", "SourcePath": "", "OutputPath": "../../dist", - "Main": "tests/unittest_shared.main", - "EntryPoint": "tests/unittest_shared", + "Main": "unittest_shared.main", + "EntryPoint": "unittest_shared", "IncludePackageData": false, "Included": [], "Excluded": [