diff --git a/borgmatic/hooks/monitoring/ntfy.py b/borgmatic/hooks/monitoring/ntfy.py index 50b31744..e514d679 100644 --- a/borgmatic/hooks/monitoring/ntfy.py +++ b/borgmatic/hooks/monitoring/ntfy.py @@ -22,10 +22,14 @@ def initialize_monitor( ''' -def _convert_string_to_array(value): - value = str(value or '') - - return [str(item).strip() for item in value.split(',') if str(item).strip()] +def convert_string_to_array(value): + value = '' if value is None else str(value) + items = [] + for item in value.split(','): + stripped = item.strip() + if stripped: + items.append(stripped) + return items PRIORITY_NAME_TO_ID = { @@ -72,7 +76,7 @@ def ping_monitor(hook_config, config, config_filename, state, monitoring_log_lev 'title': state_config.get('title'), 'message': state_config.get('message'), 'priority': PRIORITY_NAME_TO_ID.get(state_config.get('priority'), default_priority), - 'tags': _convert_string_to_array(state_config.get('tags')), + 'tags': convert_string_to_array(state_config.get('tags')), } try: diff --git a/tests/unit/hooks/monitoring/test_ntfy.py b/tests/unit/hooks/monitoring/test_ntfy.py index 6280785a..17414ad4 100644 --- a/tests/unit/hooks/monitoring/test_ntfy.py +++ b/tests/unit/hooks/monitoring/test_ntfy.py @@ -380,3 +380,11 @@ def test_ping_monitor_with_other_error_logs_warning(): monitoring_log_level=1, dry_run=False, ) + + +def test_convert_string_to_array(): + assert module.convert_string_to_array(None) == [] + assert module.convert_string_to_array('') == [] + assert module.convert_string_to_array('foo') == ['foo'] + assert module.convert_string_to_array(' foo , bar ,baz ') == ['foo', 'bar', 'baz'] + assert module.convert_string_to_array('foo,,bar,') == ['foo', 'bar']