From 033288d4dfdf30ae1b05ad89556844f691c38d87 Mon Sep 17 00:00:00 2001 From: yshepilov Date: Fri, 7 Oct 2022 21:43:08 +0200 Subject: [PATCH] #585 added possibility to specify multiple default values via a script --- src/model/parameter_config.py | 19 ++++++++++++++----- src/tests/parameter_config_test.py | 20 ++++++++++++++++++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/model/parameter_config.py b/src/model/parameter_config.py index 1e62ff85..bd915a8e 100644 --- a/src/model/parameter_config.py +++ b/src/model/parameter_config.py @@ -81,7 +81,13 @@ def _reload(self): 'multiselect_argument_type', default='single_argument', allowed_values=['single_argument', 'argument_per_value', 'repeat_param_value']) - self.default = _resolve_default(config.get('default'), self._username, self._audit_name, self._working_dir) + self.type = self._read_type(config) + self.default = _resolve_default( + config.get('default'), + self._username, + self._audit_name, + self._working_dir, + self.type) self.file_dir = _resolve_file_dir(config, 'file_dir') self._list_files_dir = _resolve_list_files_dir(self.file_dir, self._working_dir) self.file_extensions = _resolve_file_extensions(config, 'file_extensions') @@ -89,8 +95,6 @@ def _reload(self): self.file_recursive = read_bool_from_config('file_recursive', config, default=False) self.excluded_files_matcher = _resolve_excluded_files(config, 'excluded_files', self._list_files_dir) - self.type = self._read_type(config) - self.constant = read_bool_from_config('constant', config, default=False) self._validate_config() @@ -428,7 +432,7 @@ def _build_list_file_path(self, child_path): return os.path.normpath(os.path.join(self._list_files_dir, *child_path)) -def _resolve_default(default, username, audit_name, working_dir): +def _resolve_default(default, username, audit_name, working_dir, type): if not default: return default @@ -449,7 +453,12 @@ def _resolve_default(default, username, audit_name, working_dir): has_variables = string_value != resolved_string_value shell = read_bool_from_config('shell', default, default=not has_variables) output = process_utils.invoke(resolved_string_value, working_dir, shell=shell) - return output.strip() + stripped_output = output.strip() + + if type == PARAM_TYPE_MULTISELECT and '\n' in stripped_output: + return [line.strip() for line in stripped_output.split('\n') if not is_empty(line)] + + return stripped_output return resolved_string_value diff --git a/src/tests/parameter_config_test.py b/src/tests/parameter_config_test.py index 6856c405..bf200108 100644 --- a/src/tests/parameter_config_test.py +++ b/src/tests/parameter_config_test.py @@ -328,9 +328,25 @@ def test_script_value_with_shell_operators(self, grep_pattern, shell, expected_v username='my_user') self.assertEqual(expected_value, default) + @parameterized.expand([ + ('multiselect', '123', '123'), + ('list', '123', '123'), + ('multiselect', '123\n', '123'), + ('multiselect', '123\n456', ['123', '456']), + ('list', '123\n456', '123\n456'), + ('multiselect', '\n123\n456\n', ['123', '456']), + ('multiselect', '\n123 \n \t 456\n', ['123', '456']), + ('multiselect', '123 \n \t 456\n789', ['123', '456', '789']), + ]) + def test_script_value_when_multiselect_and_multiple_values(self, type, output, expected_value): + config = {'script': 'echo "' + output + '"'} + + default = self.resolve_default(config, username='my_user', type=type) + self.assertEqual(expected_value, default) + @staticmethod - def resolve_default(value, *, username=None, audit_name=None, working_dir=None): - return parameter_config._resolve_default(value, username, audit_name, working_dir) + def resolve_default(value, *, username=None, audit_name=None, working_dir=None, type=None): + return parameter_config._resolve_default(value, username, audit_name, working_dir, type) def setUp(self): test_utils.setup()