Skip to content

Commit

Permalink
Fix colorization to not extend across newline boundary (ansible#68517)
Browse files Browse the repository at this point in the history
* Fix colorization to not extend across newline boundary

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

* Add changelog fragment
  • Loading branch information
ghjm committed Mar 28, 2020
1 parent 53a3d1f commit 2068131
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
@@ -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)
9 changes: 7 additions & 2 deletions lib/ansible/utils/display.py
Expand Up @@ -153,14 +153,19 @@ def display(self, msg, color=None, stderr=False, screen_only=False, log_only=Fal
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
Expand Down
2 changes: 1 addition & 1 deletion test/units/utils/display/test_warning.py
Expand Up @@ -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):
Expand Down

0 comments on commit 2068131

Please sign in to comment.