From 94fe58b5dad7e54a5a8b3cdbfb9ad05f22ca7ea5 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Mon, 1 Sep 2025 10:25:08 +0200 Subject: [PATCH] safe_join: raise ValueError on None parameters and update tests Changed safe_join to raise ValueError if base or tail is None instead of returning 'None/path'. Adjusted unit tests accordingly to expect exceptions for None inputs and kept empty-string handling valid. Ref: https://chatgpt.com/share/68b55850-e854-800f-9702-09ea956b8dc4 --- filter_plugins/safe_join.py | 13 +++++++------ tests/unit/filter_plugins/test_safe_join.py | 9 +++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/filter_plugins/safe_join.py b/filter_plugins/safe_join.py index e03812b5..2f4d9d44 100644 --- a/filter_plugins/safe_join.py +++ b/filter_plugins/safe_join.py @@ -1,19 +1,20 @@ """ Ansible filter plugin that joins a base string and a tail path safely. -If the base is falsy (None, empty, etc.), returns an empty string. +Raises ValueError if base or tail is None. """ def safe_join(base, tail): """ Safely join base and tail into a path or URL. - - base: the base string. If falsy, returns ''. - - tail: the string to append. Leading/trailing slashes are handled. - - On any exception, returns ''. + - base: the base string. Must not be None. + - tail: the string to append. Must not be None. + - On ValueError, caller should handle it. """ + if base is None or tail is None: + raise ValueError("safe_join: base and tail must not be None") + try: - if not base: - return '' base_str = str(base).rstrip('/') tail_str = str(tail).lstrip('/') return f"{base_str}/{tail_str}" diff --git a/tests/unit/filter_plugins/test_safe_join.py b/tests/unit/filter_plugins/test_safe_join.py index 5486e486..96bfa289 100644 --- a/tests/unit/filter_plugins/test_safe_join.py +++ b/tests/unit/filter_plugins/test_safe_join.py @@ -24,10 +24,15 @@ class TestSafeJoinFilter(unittest.TestCase): ) def test_base_none(self): - self.assertEqual(safe_join(None, 'path'), '') + with self.assertRaises(ValueError): + safe_join(None, 'path') + + def test_tail_none(self): + with self.assertRaises(ValueError): + safe_join('http://example.com', None) def test_base_empty(self): - self.assertEqual(safe_join('', 'path'), '') + self.assertEqual(safe_join('', 'path'), '/path') def test_tail_empty(self): # joining with empty tail should yield base with trailing slash