From ff5f9b833d2afdf12c10d78626b7c06c8ecb73f7 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 27 May 2022 18:07:12 +0200 Subject: [PATCH] Improved unittest for cpl new --- unittests/unittests_cli/new_test_case.py | 31 ++++++++++++++++++++++ unittests/unittests_shared/cli_commands.py | 13 ++++----- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/unittests/unittests_cli/new_test_case.py b/unittests/unittests_cli/new_test_case.py index 6b831117..3013e028 100644 --- a/unittests/unittests_cli/new_test_case.py +++ b/unittests/unittests_cli/new_test_case.py @@ -1,3 +1,4 @@ +import json import os import shutil import unittest @@ -50,6 +51,33 @@ class NewTestCase(unittest.TestCase): self.assertTrue(os.path.join(project_path, f'{name}.json')) os.chdir(os.path.abspath(os.path.join(os.getcwd(), '../'))) + def _test_sub_directory_project(self, project_type: str, directory: str, name: str, workspace_name: str, *args): + os.chdir(os.path.abspath(os.path.join(os.getcwd(), workspace_name))) + CLICommands.new(project_type, f'{directory}/{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, f'src/{directory}', String.convert_to_snake_case(name))) + self.assertTrue(os.path.exists(project_path)) + project_file = os.path.join(project_path, f'{name}.json') + self.assertTrue(project_file) + project_json = {} + with open(project_file, 'r', encoding='utf-8') as cfg: + # load json + project_json = json.load(cfg) + cfg.close() + + project_settings = project_json['ProjectSettings'] + build_settings = project_json['BuildSettings'] + + self.assertEqual(project_settings['Name'], name) + self.assertEqual(build_settings['ProjectType'], 'library') + self.assertEqual(build_settings['OutputPath'], '../../dist') + self.assertEqual(build_settings['Main'], f'{String.convert_to_snake_case(name)}.main') + self.assertEqual(build_settings['EntryPoint'], name) + + os.chdir(os.path.abspath(os.path.join(os.getcwd(), '../'))) + def test_console(self): self._test_project('console', 'test-console', '--ab', '--s') @@ -71,6 +99,9 @@ class NewTestCase(unittest.TestCase): def test_sub_library(self): self._test_sub_project('library', 'test-sub-library', 'test-console', '--ab', '--s', '--sp') + def test_sub_directory_library(self): + self._test_sub_directory_project('library', 'directory', 'test-sub-library', 'test-console', '--ab', '--s', '--sp') + def test_unittest(self): self._test_project('unittest', 'test-unittest', '--ab') diff --git a/unittests/unittests_shared/cli_commands.py b/unittests/unittests_shared/cli_commands.py index da9c1ce7..5c04db12 100644 --- a/unittests/unittests_shared/cli_commands.py +++ b/unittests/unittests_shared/cli_commands.py @@ -7,9 +7,10 @@ from unittests_cli.constants import CLI_PATH class CLICommands: @staticmethod - def _run(cmd: str, *args): + def _run(cmd: str, *args, output=False): env_vars = os.environ - env_vars['CPL_IS_UNITTEST'] = 'YES' + env_vars['CPL_IS_UNITTEST'] = 'NO' if output else 'YES' + command = ['python', CLI_PATH, cmd] for arg in args: command.append(arg) @@ -17,9 +18,9 @@ class CLICommands: subprocess.run(command, env=env_vars) @classmethod - def generate(cls, schematic: str, name: str): - cls._run('generate', schematic, name) + def generate(cls, schematic: str, name: str, output=False): + cls._run('generate', schematic, name, output=output) @classmethod - def new(cls, project_type: str, name: str, *args): - cls._run('new', project_type, name, *args) + def new(cls, project_type: str, name: str, *args, output=False): + cls._run('new', project_type, name, *args, output=output)