From 703a2c91b5eb7e8effe2155ed94304344bde7fd5 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 1 Dec 2022 13:55:55 +0100 Subject: [PATCH] Added logic to change base path for cpl n #120 --- src/cpl_cli/command/new_service.py | 10 ++++++++- src/cpl_cli/startup_argument_extension.py | 3 ++- unittests/unittests_cli/new_test_case.py | 25 ++++++++++++++++++++--- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/cpl_cli/command/new_service.py b/src/cpl_cli/command/new_service.py index f7f843dd..9740a324 100644 --- a/src/cpl_cli/command/new_service.py +++ b/src/cpl_cli/command/new_service.py @@ -54,6 +54,7 @@ class NewService(CommandABC): self._use_service_providing: bool = False self._use_async: bool = False self._use_venv: bool = False + self._use_base: bool = False @property def help_message(self) -> str: @@ -159,7 +160,8 @@ class NewService(CommandABC): if self._workspace is None: project_path = os.path.join(self._env.working_directory, self._rel_path, self._project.name) else: - project_path = os.path.join(self._env.working_directory, 'src', self._rel_path, String.convert_to_snake_case(self._project.name)) + base = '' if self._use_base else 'src' + project_path = os.path.join(self._env.working_directory, base, self._rel_path, String.convert_to_snake_case(self._project.name)) if os.path.isdir(project_path) and len(os.listdir(project_path)) > 0: Console.write_line(project_path) @@ -292,6 +294,9 @@ class NewService(CommandABC): if self._env.working_directory.endswith(project): project = '' + if self._workspace is None and self._use_base: + project = f'{self._rel_path}/{project}' + VenvHelper.init_venv( False, self._env, @@ -335,6 +340,9 @@ class NewService(CommandABC): if 'venv' in args: self._use_venv = True args.remove('venv') + if 'base' in args: + self._use_base = True + args.remove('base') console = self._config.get_configuration(ProjectTypeEnum.console.value) library = self._config.get_configuration(ProjectTypeEnum.library.value) diff --git a/src/cpl_cli/startup_argument_extension.py b/src/cpl_cli/startup_argument_extension.py index 765ceafa..de57d9c3 100644 --- a/src/cpl_cli/startup_argument_extension.py +++ b/src/cpl_cli/startup_argument_extension.py @@ -55,7 +55,8 @@ class StartupArgumentExtension(StartupExtensionABC): .add_console_argument(ArgumentTypeEnum.Flag, '--', 'startup', ['s', 'S']) \ .add_console_argument(ArgumentTypeEnum.Flag, '--', 'service-providing', ['sp', 'SP']) \ .add_console_argument(ArgumentTypeEnum.Flag, '--', 'nothing', ['n', 'N']) \ - .add_console_argument(ArgumentTypeEnum.Flag, '--', 'venv', ['v', 'V']) + .add_console_argument(ArgumentTypeEnum.Flag, '--', 'venv', ['v', 'V']) \ + .add_console_argument(ArgumentTypeEnum.Flag, '--', 'base', ['b', 'B']) config.create_console_argument(ArgumentTypeEnum.Executable, '', 'publish', ['p', 'P'], PublishService, True, validators=[ProjectValidator]) config.create_console_argument(ArgumentTypeEnum.Executable, '', 'remove', ['r', 'R'], RemoveService, True, validators=[WorkspaceValidator]) \ .add_console_argument(ArgumentTypeEnum.Flag, '--', 'simulate', ['s', 'S']) diff --git a/unittests/unittests_cli/new_test_case.py b/unittests/unittests_cli/new_test_case.py index b2b24177..5c05334e 100644 --- a/unittests/unittests_cli/new_test_case.py +++ b/unittests/unittests_cli/new_test_case.py @@ -12,7 +12,7 @@ class NewTestCase(unittest.TestCase): def setUp(self): os.chdir(os.path.abspath(PLAYGROUND_PATH)) - def _test_project(self, project_type: str, name: str, *args, test_venv=False): + def _test_project(self, project_type: str, name: str, *args, test_venv=False, without_ws=False): CLICommands.new(project_type, name, *args) workspace_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, name)) self.assertTrue(os.path.exists(workspace_path)) @@ -22,7 +22,15 @@ class NewTestCase(unittest.TestCase): self.assertTrue(os.path.exists(os.path.join(workspace_path, 'venv/bin/python'))) self.assertTrue(os.path.islink(os.path.join(workspace_path, 'venv/bin/python'))) - project_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, name, 'src', String.convert_to_snake_case(name))) + base = 'src' + if '--base' in args and '/' in name: + base = name.split('/')[0] + name = name.replace(f'{name.split("/")[0]}/', '') + + project_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, name, base, String.convert_to_snake_case(name))) + if without_ws: + project_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, base, 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')) self.assertTrue(os.path.join(project_path, f'main.py')) @@ -54,7 +62,12 @@ class NewTestCase(unittest.TestCase): self.assertTrue(os.path.exists(os.path.join(workspace_path, 'venv/bin/python'))) self.assertTrue(os.path.islink(os.path.join(workspace_path, 'venv/bin/python'))) - project_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, workspace_name, 'src', String.convert_to_snake_case(name))) + base = 'src' + if '--base' in args and '/' in name: + base = name.split('/')[0] + name = name.replace(f'{name.split("/")[0]}/', '') + + project_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, workspace_name, base, 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(), '../'))) @@ -88,6 +101,9 @@ class NewTestCase(unittest.TestCase): def test_console(self): self._test_project('console', 'test-console', '--ab', '--s', '--venv', test_venv=True) + def test_console_with_other_base(self): + self._test_project('console', 'tools/test-console', '--ab', '--s', '--venv', '--base', test_venv=True, without_ws=True) + def test_console_without_s(self): self._test_project('console', 'test-console-without-s', '--ab') @@ -100,6 +116,9 @@ class NewTestCase(unittest.TestCase): def test_sub_console(self): self._test_sub_project('console', 'test-sub-console', 'test-console', '--ab', '--s', '--sp', '--venv', test_venv=True) + def test_sub_console_with_other_base(self): + self._test_sub_project('console', 'tools/test-sub-console', 'test-console', '--ab', '--s', '--sp', '--venv', '--base', test_venv=True) + def test_library(self): self._test_project('library', 'test-library', '--ab', '--s', '--sp')