display - remove extra new line after warning message (#65199)
Add unit tests for display
This commit is contained in:
parent
85b5f89ed6
commit
8e195adda5
4 changed files with 67 additions and 2 deletions
2
changelogs/fragments/warnings-remove-extra-newline.yaml
Normal file
2
changelogs/fragments/warnings-remove-extra-newline.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
bugfixes:
|
||||
- display - remove extra new line after warnings (https://github.com/ansible/ansible/pull/65199)
|
|
@ -151,8 +151,6 @@ class Display(with_metaclass(Singleton, object)):
|
|||
"""
|
||||
|
||||
nocolor = msg
|
||||
if color:
|
||||
msg = stringc(msg, color)
|
||||
|
||||
if not log_only:
|
||||
if not msg.endswith(u'\n') and newline:
|
||||
|
@ -160,6 +158,9 @@ class Display(with_metaclass(Singleton, object)):
|
|||
else:
|
||||
msg2 = msg
|
||||
|
||||
if color:
|
||||
msg2 = stringc(msg2, color)
|
||||
|
||||
msg2 = to_bytes(msg2, encoding=self._output_encoding(stderr=stderr))
|
||||
if sys.version_info >= (3,):
|
||||
# Convert back to text string on python3
|
||||
|
|
20
test/units/utils/display/test_display.py
Normal file
20
test/units/utils/display/test_display.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2020 Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
from ansible.utils.display import Display
|
||||
|
||||
|
||||
def test_display_basic_message(capsys, mocker):
|
||||
# Disable logging
|
||||
mocker.patch('ansible.utils.display.logger', return_value=None)
|
||||
|
||||
d = Display()
|
||||
d.display(u'Some displayed message')
|
||||
out, err = capsys.readouterr()
|
||||
assert out == 'Some displayed message\n'
|
||||
assert err == ''
|
42
test/units/utils/display/test_warning.py
Normal file
42
test/units/utils/display/test_warning.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2020 Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible.utils.display import Display
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def warning_message():
|
||||
warning_message = 'bad things will happen'
|
||||
expected_warning_message = '[WARNING]: {0}\n'.format(warning_message)
|
||||
return warning_message, expected_warning_message
|
||||
|
||||
|
||||
def test_warning(capsys, mocker, warning_message):
|
||||
warning_message, expected_warning_message = warning_message
|
||||
|
||||
mocker.patch('ansible.utils.color.ANSIBLE_COLOR', True)
|
||||
mocker.patch('ansible.utils.color.parsecolor', return_value=u'1;35') # value for 'bright purple'
|
||||
|
||||
d = Display()
|
||||
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'))
|
||||
|
||||
|
||||
def test_warning_no_color(capsys, mocker, warning_message):
|
||||
warning_message, expected_warning_message = warning_message
|
||||
|
||||
mocker.patch('ansible.utils.color.ANSIBLE_COLOR', False)
|
||||
|
||||
d = Display()
|
||||
d.warning(warning_message)
|
||||
out, err = capsys.readouterr()
|
||||
assert d._warns == {expected_warning_message: 1}
|
||||
assert err == expected_warning_message
|
Loading…
Reference in a new issue