1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import annotations
import curses
import importlib
import io
import pytest
import sys
import ansible.utils.display # make available for monkeypatch
assert ansible.utils.display # avoid reporting as unused
builtin_import = 'builtins.__import__'
def test_pause_curses_tigetstr_none(mocker, monkeypatch):
monkeypatch.delitem(sys.modules, 'ansible.utils.display')
dunder_import = __import__
def _import(*args, **kwargs):
if args[0] == 'curses':
mock_curses = mocker.Mock()
mock_curses.setupterm = mocker.Mock(return_value=True)
mock_curses.tigetstr = mocker.Mock(return_value=None)
return mock_curses
else:
return dunder_import(*args, **kwargs)
mocker.patch(builtin_import, _import)
mod = importlib.import_module('ansible.utils.display')
assert mod.HAS_CURSES is True
mod.setupterm()
assert mod.HAS_CURSES is True
assert mod.MOVE_TO_BOL == b'\r'
assert mod.CLEAR_TO_EOL == b'\x1b[K'
def test_pause_missing_curses(mocker, monkeypatch):
monkeypatch.delitem(sys.modules, 'ansible.utils.display')
dunder_import = __import__
def _import(*args, **kwargs):
if args[0] == 'curses':
raise ImportError
else:
return dunder_import(*args, **kwargs)
mocker.patch(builtin_import, _import)
mod = importlib.import_module('ansible.utils.display')
assert mod.HAS_CURSES is False
with pytest.raises(AttributeError, match=r"^module 'ansible\.utils\.display' has no attribute"):
mod.curses # pylint: disable=pointless-statement
assert mod.HAS_CURSES is False
assert mod.MOVE_TO_BOL == b'\r'
assert mod.CLEAR_TO_EOL == b'\x1b[K'
@pytest.mark.parametrize('exc', (curses.error, TypeError, io.UnsupportedOperation))
def test_pause_curses_setupterm_error(mocker, monkeypatch, exc):
monkeypatch.delitem(sys.modules, 'ansible.utils.display')
dunder_import = __import__
def _import(*args, **kwargs):
if args[0] == 'curses':
mock_curses = mocker.Mock()
mock_curses.setupterm = mocker.Mock(side_effect=exc)
mock_curses.error = curses.error
return mock_curses
else:
return dunder_import(*args, **kwargs)
mocker.patch(builtin_import, _import)
mod = importlib.import_module('ansible.utils.display')
assert mod.HAS_CURSES is True
mod.setupterm()
assert mod.HAS_CURSES is False
assert mod.MOVE_TO_BOL == b'\r'
assert mod.CLEAR_TO_EOL == b'\x1b[K'
|