Added logic to run build code to cpl start #124

This commit is contained in:
2022-12-02 19:38:03 +01:00
parent d0877a4ea6
commit 856960d799
4 changed files with 68 additions and 12 deletions

View File

@@ -21,6 +21,7 @@ class StartTestCase(CommandTestCase):
self._application = f'src/{String.convert_to_snake_case(self._source)}/application.py'
self._test_code = f"""
import json
import os
settings = dict()
with open('appsettings.json', 'r', encoding='utf-8') as cfg:
# load json
@@ -31,14 +32,19 @@ class StartTestCase(CommandTestCase):
settings['RunTest']['WasRestarted'] = 'True'
settings['RunTest']['WasStarted'] = 'True'
settings['RunTest']['Path'] = os.path.dirname(os.path.realpath(__file__))
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:
def _get_appsettings(self, is_dev=False):
appsettings = f'dist/{self._source}/build/{String.convert_to_snake_case(self._source)}/appsettings.json'
if is_dev:
appsettings = f'src/{String.convert_to_snake_case(self._source)}/appsettings.json'
with open(os.path.join(os.getcwd(), appsettings), 'r', encoding='utf-8') as cfg:
# load json
project_json = json.load(cfg)
cfg.close()
@@ -46,14 +52,14 @@ class StartTestCase(CommandTestCase):
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:
with open(os.path.join(os.getcwd(), f'src/{String.convert_to_snake_case(self._source)}/appsettings.json'), 'w', encoding='utf-8') as project_file:
project_file.write(json.dumps(settings, indent=2))
project_file.close()
def setUp(self):
if not os.path.exists(PLAYGROUND_PATH):
os.makedirs(PLAYGROUND_PATH)
os.chdir(PLAYGROUND_PATH)
# create projects
CLICommands.new('console', self._source, '--ab', '--s')
@@ -67,6 +73,39 @@ class StartTestCase(CommandTestCase):
def test_start(self):
thread = StartTestThread()
thread.start()
time.sleep(5)
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()
time.sleep(5)
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']
)
def test_start_dev(self):
thread = StartTestThread(is_dev=True)
thread.start()
time.sleep(1)
settings = self._get_appsettings()
self.assertNotEqual(settings, {})
@@ -83,7 +122,7 @@ class StartTestCase(CommandTestCase):
time.sleep(1)
settings = self._get_appsettings()
settings = self._get_appsettings(is_dev=True)
self.assertNotEqual(settings, {})
self.assertIn('RunTest', settings)
self.assertIn('WasStarted', settings['RunTest'])

View File

@@ -5,8 +5,9 @@ from unittests_shared.cli_commands import CLICommands
class StartTestThread(threading.Thread):
def __init__(self):
def __init__(self, is_dev=False):
threading.Thread.__init__(self, daemon=True)
self._is_dev = is_dev
def run(self):
CLICommands.start(True)
CLICommands.start(is_dev=self._is_dev, output=True)

View File

@@ -76,8 +76,12 @@ class CLICommands:
cls._run('run', project, *args, output=output)
@classmethod
def start(cls, output=False):
cls._run('start', output=output)
def start(cls, is_dev=False, output=False):
args = []
if is_dev:
args.append('--dev')
cls._run('start', *args, output=output)
@classmethod
def uninstall(cls, package: str, is_dev=False, output=False):