mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-27 04:41:26 -07:00
Improve --diff output when files lack trailing newlines
The behavior now matches GNU diff. Fixes #14094. Example of output before this change: TASK [healthchecks.io : hourly healthchecks.io ping] *************************** changed: [ranka] --- before: /etc/cron.hourly/mg-healthchecks-dot-io +++ after: /tmp/tmpOTvXTw @@ -1,2 +1,2 @@ #!/bin/sh -curl -sS https://hchk.io/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx > /dev/null+curl -sS https://hchk.io/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx > /dev/null after this change: TASK [healthchecks.io : hourly healthchecks.io ping] *************************** changed: [ranka] --- before: /etc/cron.hourly/mg-healthchecks-dot-io +++ after: /tmp/tmpOTvXTw @@ -1,2 +1,2 @@ #!/bin/sh -curl -sS https://hchk.io/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx > /dev/null \ No newline at end of file +curl -sS https://hchk.io/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx > /dev/null The added unit tests contain more examples. This commit also takes care to avoid "no newline at EOF" warnings when no_log is in effect, and also when modules return dicts rather than strings. (It also removes trailing whitespace from using json serialization when diffing dicts, because I hate trailing whitespace in Python source files, even if they're test files.)
This commit is contained in:
parent
a6fff93967
commit
0a7f2c202b
3 changed files with 176 additions and 8 deletions
|
@ -19,6 +19,8 @@
|
|||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import re
|
||||
import textwrap
|
||||
import types
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
|
@ -134,9 +136,15 @@ class TestCallbackDumpResults(unittest.TestCase):
|
|||
# that try except orig appeared in 61d01f549f2143fd9adfa4ffae42f09d24649c26
|
||||
# in 2013 so maybe a < py2.6 issue
|
||||
class TestCallbackDiff(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.cb = CallbackBase()
|
||||
|
||||
def _strip_color(self, s):
|
||||
return re.sub('\033\\[[^m]*m', '', s)
|
||||
|
||||
def test_difflist(self):
|
||||
# TODO: split into smaller tests?
|
||||
cb = CallbackBase()
|
||||
difflist = [{'before': ['preface\nThe Before String\npostscript'],
|
||||
'after': ['preface\nThe After String\npostscript'],
|
||||
'before_header': 'just before',
|
||||
|
@ -153,13 +161,158 @@ class TestCallbackDiff(unittest.TestCase):
|
|||
{'before_header': 'just before'},
|
||||
{'after_header': 'just after'}]
|
||||
|
||||
res = cb._get_diff(difflist)
|
||||
res = self.cb._get_diff(difflist)
|
||||
|
||||
self.assertIn('Before String', res)
|
||||
self.assertIn('After String', res)
|
||||
self.assertIn('just before', res)
|
||||
self.assertIn('just after', res)
|
||||
|
||||
def test_simple_diff(self):
|
||||
self.assertMultiLineEqual(
|
||||
self._strip_color(self.cb._get_diff({
|
||||
'before_header': 'somefile.txt',
|
||||
'after_header': 'generated from template somefile.j2',
|
||||
'before': 'one\ntwo\nthree\n',
|
||||
'after': 'one\nthree\nfour\n',
|
||||
})),
|
||||
textwrap.dedent('''\
|
||||
--- before: somefile.txt
|
||||
+++ after: generated from template somefile.j2
|
||||
@@ -1,3 +1,3 @@
|
||||
one
|
||||
-two
|
||||
three
|
||||
+four
|
||||
|
||||
'''))
|
||||
|
||||
def test_new_file(self):
|
||||
self.assertMultiLineEqual(
|
||||
self._strip_color(self.cb._get_diff({
|
||||
'before_header': 'somefile.txt',
|
||||
'after_header': 'generated from template somefile.j2',
|
||||
'before': '',
|
||||
'after': 'one\ntwo\nthree\n',
|
||||
})),
|
||||
textwrap.dedent('''\
|
||||
--- before: somefile.txt
|
||||
+++ after: generated from template somefile.j2
|
||||
@@ -0,0 +1,3 @@
|
||||
+one
|
||||
+two
|
||||
+three
|
||||
|
||||
'''))
|
||||
|
||||
def test_clear_file(self):
|
||||
self.assertMultiLineEqual(
|
||||
self._strip_color(self.cb._get_diff({
|
||||
'before_header': 'somefile.txt',
|
||||
'after_header': 'generated from template somefile.j2',
|
||||
'before': 'one\ntwo\nthree\n',
|
||||
'after': '',
|
||||
})),
|
||||
textwrap.dedent('''\
|
||||
--- before: somefile.txt
|
||||
+++ after: generated from template somefile.j2
|
||||
@@ -1,3 +0,0 @@
|
||||
-one
|
||||
-two
|
||||
-three
|
||||
|
||||
'''))
|
||||
|
||||
def test_no_trailing_newline_before(self):
|
||||
self.assertMultiLineEqual(
|
||||
self._strip_color(self.cb._get_diff({
|
||||
'before_header': 'somefile.txt',
|
||||
'after_header': 'generated from template somefile.j2',
|
||||
'before': 'one\ntwo\nthree',
|
||||
'after': 'one\ntwo\nthree\n',
|
||||
})),
|
||||
textwrap.dedent('''\
|
||||
--- before: somefile.txt
|
||||
+++ after: generated from template somefile.j2
|
||||
@@ -1,3 +1,3 @@
|
||||
one
|
||||
two
|
||||
-three
|
||||
\\ No newline at end of file
|
||||
+three
|
||||
|
||||
'''))
|
||||
|
||||
def test_no_trailing_newline_after(self):
|
||||
self.assertMultiLineEqual(
|
||||
self._strip_color(self.cb._get_diff({
|
||||
'before_header': 'somefile.txt',
|
||||
'after_header': 'generated from template somefile.j2',
|
||||
'before': 'one\ntwo\nthree\n',
|
||||
'after': 'one\ntwo\nthree',
|
||||
})),
|
||||
textwrap.dedent('''\
|
||||
--- before: somefile.txt
|
||||
+++ after: generated from template somefile.j2
|
||||
@@ -1,3 +1,3 @@
|
||||
one
|
||||
two
|
||||
-three
|
||||
+three
|
||||
\\ No newline at end of file
|
||||
|
||||
'''))
|
||||
|
||||
def test_no_trailing_newline_both(self):
|
||||
self.assertMultiLineEqual(
|
||||
self.cb._get_diff({
|
||||
'before_header': 'somefile.txt',
|
||||
'after_header': 'generated from template somefile.j2',
|
||||
'before': 'one\ntwo\nthree',
|
||||
'after': 'one\ntwo\nthree',
|
||||
}),
|
||||
'')
|
||||
|
||||
def test_no_trailing_newline_both_with_some_changes(self):
|
||||
self.assertMultiLineEqual(
|
||||
self._strip_color(self.cb._get_diff({
|
||||
'before_header': 'somefile.txt',
|
||||
'after_header': 'generated from template somefile.j2',
|
||||
'before': 'one\ntwo\nthree',
|
||||
'after': 'one\nfive\nthree',
|
||||
})),
|
||||
textwrap.dedent('''\
|
||||
--- before: somefile.txt
|
||||
+++ after: generated from template somefile.j2
|
||||
@@ -1,3 +1,3 @@
|
||||
one
|
||||
-two
|
||||
+five
|
||||
three
|
||||
\\ No newline at end of file
|
||||
|
||||
'''))
|
||||
|
||||
def test_diff_dicts(self):
|
||||
self.assertMultiLineEqual(
|
||||
self._strip_color(self.cb._get_diff({
|
||||
'before': dict(one=1, two=2, three=3),
|
||||
'after': dict(one=1, three=3, four=4),
|
||||
})),
|
||||
textwrap.dedent('''\
|
||||
--- before
|
||||
+++ after
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
+ "four": 4,
|
||||
"one": 1,
|
||||
- "three": 3,
|
||||
- "two": 2
|
||||
+ "three": 3
|
||||
}
|
||||
|
||||
'''))
|
||||
|
||||
|
||||
class TestCallbackOnMethods(unittest.TestCase):
|
||||
def _find_on_methods(self, callback):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue