allow before/after diff to be NoneType (#62582)

when creating or deleting an object (e.g. via an API), before/after can
be `None` (or at least represented as such by the used library). to
avoid modules havig to do

    diff={'before': before or '', 'after': after or ''}

let's just convert `None` to an empty string that can be diffed properly
This commit is contained in:
Evgeni Golov 2019-09-20 21:47:18 +02:00 committed by Sam Doran
parent bb0fa0a622
commit 8d0c193b25
3 changed files with 34 additions and 0 deletions

View file

@ -0,0 +1,4 @@
minor_changes:
- callbacks - Allow modules to return `None` as before/after entries for diff.
This should make it easier for modules to report the "not existing" state of
the entity they touched.

View file

@ -184,6 +184,8 @@ class CallbackBase(AnsiblePlugin):
for x in ['before', 'after']:
if isinstance(diff[x], MutableMapping):
diff[x] = self._serialize_diff(diff[x])
elif diff[x] is None:
diff[x] = ''
if 'before_header' in diff:
before_header = u"before: %s" % diff['before_header']
else:

View file

@ -365,6 +365,34 @@ class TestCallbackDiff(unittest.TestCase):
'''))
def test_diff_before_none(self):
self.assertMultiLineEqual(
self._strip_color(self.cb._get_diff({
'before': None,
'after': 'one line\n',
})),
textwrap.dedent('''\
--- before
+++ after
@@ -0,0 +1 @@
+one line
'''))
def test_diff_after_none(self):
self.assertMultiLineEqual(
self._strip_color(self.cb._get_diff({
'before': 'one line\n',
'after': None,
})),
textwrap.dedent('''\
--- before
+++ after
@@ -1 +0,0 @@
-one line
'''))
class TestCallbackOnMethods(unittest.TestCase):
def _find_on_methods(self, callback):