Skip to content

Commit

Permalink
#585 added possibility to specify multiple default values via a script
Browse files Browse the repository at this point in the history
  • Loading branch information
bugy committed Oct 7, 2022
1 parent f07b621 commit 033288d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
19 changes: 14 additions & 5 deletions src/model/parameter_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,20 @@ 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')
self.file_type = _resolve_parameter_file_type(config, 'file_type', self.file_extensions)
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()
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
20 changes: 18 additions & 2 deletions src/tests/parameter_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 033288d

Please sign in to comment.