From 32507afae97badaf823e1f74a61eee2eb0e5b42d Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 26 Jun 2022 00:39:16 +0200 Subject: [PATCH] Added tests for start command --- unittests/unittests_cli/cli_test_suite.py | 20 ++-- unittests/unittests_cli/install_test_case.py | 3 - unittests/unittests_cli/new_test_case.py | 2 - unittests/unittests_cli/remove_test_case.py | 2 - unittests/unittests_cli/start_test_case.py | 95 ++++++++++++++++++- unittests/unittests_cli/threads/__init__.py | 1 + .../threads/start_test_thread.py | 12 +++ unittests/unittests_shared/cli_commands.py | 6 +- 8 files changed, 119 insertions(+), 22 deletions(-) create mode 100644 unittests/unittests_cli/threads/__init__.py create mode 100644 unittests/unittests_cli/threads/start_test_thread.py diff --git a/unittests/unittests_cli/cli_test_suite.py b/unittests/unittests_cli/cli_test_suite.py index f0242a88..c615ab5a 100644 --- a/unittests/unittests_cli/cli_test_suite.py +++ b/unittests/unittests_cli/cli_test_suite.py @@ -14,6 +14,7 @@ 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 @@ -25,25 +26,24 @@ class CLITestSuite(unittest.TestSuite): loader = unittest.TestLoader() self._result: Optional[TestResult] = None - self._is_online = True + self._is_online = False active_tests = [ # nothing needed - GenerateTestCase, - NewTestCase, + # GenerateTestCase, + # NewTestCase, # compare console output # VersionTestCase, # project needed - BuildTestCase, - PublishTestCase, - RunTestCase, - # check if application was executed properly and file watcher is working - # StartTestCase, + # BuildTestCase, + # PublishTestCase, + # RunTestCase, + StartTestCase, # check in project settings if package is updated # UpdateTestCase, # workspace needed - AddTestCase, - RemoveTestCase + # AddTestCase, + # RemoveTestCase ] if self._is_online: diff --git a/unittests/unittests_cli/install_test_case.py b/unittests/unittests_cli/install_test_case.py index 4986ba15..ceefbf4f 100644 --- a/unittests/unittests_cli/install_test_case.py +++ b/unittests/unittests_cli/install_test_case.py @@ -5,10 +5,7 @@ import subprocess import sys import unittest -import pkg_resources - from cpl_core.utils import String - from unittests_cli.constants import PLAYGROUND_PATH from unittests_shared.cli_commands import CLICommands diff --git a/unittests/unittests_cli/new_test_case.py b/unittests/unittests_cli/new_test_case.py index 94bb652c..4125993d 100644 --- a/unittests/unittests_cli/new_test_case.py +++ b/unittests/unittests_cli/new_test_case.py @@ -1,10 +1,8 @@ import json 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 diff --git a/unittests/unittests_cli/remove_test_case.py b/unittests/unittests_cli/remove_test_case.py index e53d9f1a..81aa8ee0 100644 --- a/unittests/unittests_cli/remove_test_case.py +++ b/unittests/unittests_cli/remove_test_case.py @@ -1,10 +1,8 @@ import json 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 diff --git a/unittests/unittests_cli/start_test_case.py b/unittests/unittests_cli/start_test_case.py index 8da691da..8782b5ca 100644 --- a/unittests/unittests_cli/start_test_case.py +++ b/unittests/unittests_cli/start_test_case.py @@ -1,10 +1,97 @@ +import json +import os +import shutil import unittest +from cpl_core.utils import String +from unittests_cli.constants import PLAYGROUND_PATH +from unittests_cli.threads.start_test_thread import StartTestThread +from unittests_shared.cli_commands import CLICommands + class StartTestCase(unittest.TestCase): - def setUp(self): - pass + def __init__(self, methodName: str): + unittest.TestCase.__init__(self, methodName) + self._source = 'start-test' + self._project_file = f'src/{String.convert_to_snake_case(self._source)}/{self._source}.json' + self._appsettings = f'src/{String.convert_to_snake_case(self._source)}/appsettings.json' + self._application = f'src/{String.convert_to_snake_case(self._source)}/application.py' + self._test_code = f""" + import json + settings = dict() + with open('appsettings.json', 'r', encoding='utf-8') as cfg: + # load json + settings = json.load(cfg) + cfg.close() + + if settings['RunTest']['WasStarted']: + settings['RunTest']['WasRestarted'] = 'True' + + settings['RunTest']['WasStarted'] = 'True' - def test_equal(self): - pass + with open('appsettings.json', 'w', encoding='utf-8') as project_file: + project_file.write(json.dumps(settings, indent=2)) + project_file.close() + """ + + def _get_appsettings(self): + with open(os.path.join(os.getcwd(), self._appsettings), 'r', encoding='utf-8') as cfg: + # load json + project_json = json.load(cfg) + cfg.close() + + return project_json + + def _save_appsettings(self, settings: dict): + with open(os.path.join(os.getcwd(), self._appsettings), 'w', encoding='utf-8') as project_file: + project_file.write(json.dumps(settings, indent=2)) + project_file.close() + + def setUp(self): + os.chdir(os.path.abspath(PLAYGROUND_PATH)) + # create projects + CLICommands.new('console', self._source, '--ab', '--s') + os.chdir(os.path.join(os.getcwd(), self._source)) + settings = {'RunTest': {'WasStarted': 'False', 'WasRestarted': 'False'}} + self._save_appsettings(settings) + with open(os.path.join(os.getcwd(), self._application), 'a', encoding='utf-8') as file: + file.write(f'\t\t{self._test_code}') + file.close() + + def cleanUp(self): + # remove projects + if not os.path.exists(os.path.abspath(os.path.join(PLAYGROUND_PATH, self._source))): + return + + shutil.rmtree(os.path.abspath(os.path.join(PLAYGROUND_PATH, self._source))) + + def test_start(self): + thread = StartTestThread() + thread.run() + settings = self._get_appsettings() + self.assertNotEqual(settings, {}) + self.assertIn('RunTest', settings) + self.assertIn('WasStarted', settings['RunTest']) + self.assertEqual( + 'True', + settings['RunTest']['WasStarted'] + ) + + with open(os.path.join(os.getcwd(), self._application), 'a', encoding='utf-8') as file: + file.write(f'# trigger restart (comment generated by unittest)') + file.close() + + settings = self._get_appsettings() + self.assertNotEqual(settings, {}) + self.assertIn('RunTest', settings) + self.assertIn('WasStarted', settings['RunTest']) + self.assertIn('WasRestarted', settings['RunTest']) + self.assertEqual( + 'True', + settings['RunTest']['WasStarted'] + ) + self.assertEqual( + 'True', + settings['RunTest']['WasRestarted'] + ) diff --git a/unittests/unittests_cli/threads/__init__.py b/unittests/unittests_cli/threads/__init__.py new file mode 100644 index 00000000..425ab6c1 --- /dev/null +++ b/unittests/unittests_cli/threads/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/unittests/unittests_cli/threads/start_test_thread.py b/unittests/unittests_cli/threads/start_test_thread.py new file mode 100644 index 00000000..1a80b8d1 --- /dev/null +++ b/unittests/unittests_cli/threads/start_test_thread.py @@ -0,0 +1,12 @@ +import threading + +from unittests_shared.cli_commands import CLICommands + + +class StartTestThread(threading.Thread): + + def __init__(self): + threading.Thread.__init__(self, daemon=True) + + def run(self): + CLICommands.start() diff --git a/unittests/unittests_shared/cli_commands.py b/unittests/unittests_shared/cli_commands.py index 4a2d38e2..eeb253e1 100644 --- a/unittests/unittests_shared/cli_commands.py +++ b/unittests/unittests_shared/cli_commands.py @@ -18,7 +18,7 @@ class CLICommands: if output: subprocess.run(command, env=env_vars) else: - subprocess.run(command, env=env_vars, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + subprocess.run(command, env=env_vars, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL) @classmethod def add(cls, source: str, target: str, output=False): @@ -59,6 +59,10 @@ class CLICommands: return cls._run('run', project, output=output) + @classmethod + def start(cls, output=False): + cls._run('start', output=output) + @classmethod def uninstall(cls, package: str, output=False): cls._run('uninstall', package, output=output)