Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow before/after to be NoneType #62582

Merged
merged 1 commit into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions lib/ansible/plugins/callback/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ def _get_diff(self, difflist):
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:
Expand Down
28 changes: 28 additions & 0 deletions test/units/plugins/callback/test_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,34 @@ def test_diff_dicts(self):

'''))

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):
Expand Down