Fix colorization to not extend across newline boundary (#68517)

* Fix colorization to not extend across newline boundary

* Fix unit test to look for the newline outside the coloration

* Add changelog fragment
pull/68531/head
Graham Mainwaring 5 years ago committed by GitHub
parent 53a3d1ffdb
commit 2068131589
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- display - Improve method of removing extra new line after warnings so it does not break Tower/Runner (https://github.com/ansible/ansible/pull/68517)

@ -153,14 +153,19 @@ class Display(with_metaclass(Singleton, object)):
nocolor = msg
if not log_only:
if not msg.endswith(u'\n') and newline:
msg2 = msg + u'\n'
has_newline = msg.endswith(u'\n')
if has_newline:
msg2 = msg[:-1]
else:
msg2 = msg
if color:
msg2 = stringc(msg2, color)
if has_newline or newline:
msg2 = msg2 + u'\n'
msg2 = to_bytes(msg2, encoding=self._output_encoding(stderr=stderr))
if sys.version_info >= (3,):
# Convert back to text string on python3

@ -27,7 +27,7 @@ def test_warning(capsys, mocker, warning_message):
d.warning(warning_message)
out, err = capsys.readouterr()
assert d._warns == {expected_warning_message: 1}
assert err == '\x1b[1;35m{0}\x1b[0m\n\x1b[1;35m\x1b[0m'.format(expected_warning_message.rstrip('\n'))
assert err == '\x1b[1;35m{0}\x1b[0m\n'.format(expected_warning_message.rstrip('\n'))
def test_warning_no_color(capsys, mocker, warning_message):

Loading…
Cancel
Save