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

Fix colorization to not extend across newline boundary #68517

Merged
merged 3 commits into from Mar 28, 2020
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
@@ -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'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was originally looking for:
\x1b[1;35m (color on) - {0} (the message) - \x1b[0m (color off) - \n (newline) - \x1b[1;35m (color on) - \x1b[0m (color off)

With this PR it now puts the newline outside the coloration, and does not unnecessarily turn the coloration on and off a second time. So it is now looking for:
\x1b[1;35m (color on) - {0} (the message) - \x1b[0m (color off) - \n (newline)



def test_warning_no_color(capsys, mocker, warning_message):
Expand Down