mirror of https://github.com/ansible/ansible.git
Use libc wcwidth to calculate print width in display (#66214)
* Use libc wcwidth to calculate print width in display. Fixes #63105 * Remove errantly added blank lines * Fixes * Move setlocale, adjust tests to work around py2 oddity with characters following null * Don't change cli stub * emojis * Remove to_text call * Special accounting for deletions * Add initialization function, expand tests, ensure fallback to len * get_text_width requires text, ensure banner deals with it * Handle setlocale errors * Move variable decrement * Remove unused importpull/70228/head
parent
26e8c07f32
commit
1fedb95e4b
@ -0,0 +1,3 @@
|
||||
bugfixes:
|
||||
- Display - Use wcswidth to calculate printable width of a text string
|
||||
(https://github.com/ansible/ansible/issues/63105)
|
@ -0,0 +1,65 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# (c) 2020 Matt Martz <matt@sivel.net>
|
||||
# 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 units.compat.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible.module_utils.six import PY3
|
||||
from ansible.utils.display import Display, get_text_width, initialize_locale
|
||||
|
||||
|
||||
def test_get_text_width():
|
||||
initialize_locale()
|
||||
assert get_text_width(u'コンニチハ') == 10
|
||||
assert get_text_width(u'abコcd') == 6
|
||||
assert get_text_width(u'café') == 4
|
||||
assert get_text_width(u'four') == 4
|
||||
assert get_text_width(u'\u001B') == 0
|
||||
assert get_text_width(u'ab\u0000') == 2
|
||||
assert get_text_width(u'abコ\u0000') == 4
|
||||
assert get_text_width(u'🚀🐮') == 4
|
||||
assert get_text_width(u'\x08') == 0
|
||||
assert get_text_width(u'\x08\x08') == 0
|
||||
assert get_text_width(u'ab\x08cd') == 3
|
||||
assert get_text_width(u'ab\x1bcd') == 3
|
||||
assert get_text_width(u'ab\x7fcd') == 3
|
||||
assert get_text_width(u'ab\x94cd') == 3
|
||||
|
||||
pytest.raises(TypeError, get_text_width, 1)
|
||||
pytest.raises(TypeError, get_text_width, b'four')
|
||||
|
||||
|
||||
@pytest.mark.skipif(PY3, reason='Fallback only happens reliably on py2')
|
||||
def test_get_text_width_no_locale():
|
||||
pytest.raises(EnvironmentError, get_text_width, u'🚀🐮')
|
||||
|
||||
|
||||
def test_Display_banner_get_text_width(monkeypatch):
|
||||
initialize_locale()
|
||||
display = Display()
|
||||
display_mock = MagicMock()
|
||||
monkeypatch.setattr(display, 'display', display_mock)
|
||||
|
||||
display.banner(u'🚀🐮', color=False, cows=False)
|
||||
args, kwargs = display_mock.call_args
|
||||
msg = args[0]
|
||||
stars = u' %s' % (75 * u'*')
|
||||
assert msg.endswith(stars)
|
||||
|
||||
|
||||
@pytest.mark.skipif(PY3, reason='Fallback only happens reliably on py2')
|
||||
def test_Display_banner_get_text_width_fallback(monkeypatch):
|
||||
display = Display()
|
||||
display_mock = MagicMock()
|
||||
monkeypatch.setattr(display, 'display', display_mock)
|
||||
|
||||
display.banner(u'🚀🐮', color=False, cows=False)
|
||||
args, kwargs = display_mock.call_args
|
||||
msg = args[0]
|
||||
stars = u' %s' % (77 * u'*')
|
||||
assert msg.endswith(stars)
|
Loading…
Reference in New Issue