Debian Codegraph @

tests/test_utils.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 import io
2
3 import pytest
4
5 import progressbar
6 import progressbar.env
7
8
9 @pytest.mark.parametrize(
10 'value,expected',
11 [
12 (None, None),
13 ('', None),
14 ('1', True),
15 ('y', True),
16 ('t', True),
17 ('yes', True),
18 ('true', True),
19 ('True', True),
20 ('0', False),
21 ('n', False),
22 ('f', False),
23 ('no', False),
24 ('false', False),
25 ('False', False),
26 ],
27 )
28 def test_env_flag(value, expected, monkeypatch) -> None:
29 if value is not None:
30 monkeypatch.setenv('TEST_ENV', value)
31 assert progressbar.env.env_flag('TEST_ENV') == expected
32
33 if value:
34 monkeypatch.setenv('TEST_ENV', value.upper())
35 assert progressbar.env.env_flag('TEST_ENV') == expected
36
37 monkeypatch.undo()
38
39
40 def test_is_terminal(monkeypatch) -> None:
41 fd = io.StringIO()
42
43 monkeypatch.delenv('PROGRESSBAR_IS_TERMINAL', raising=False)
44 monkeypatch.setattr(progressbar.env, 'JUPYTER', False)
45
46 assert progressbar.env.is_terminal(fd) is False
47 assert progressbar.env.is_terminal(fd, True) is True
48 assert progressbar.env.is_terminal(fd, False) is False
49
50 monkeypatch.setattr(progressbar.env, 'JUPYTER', True)
51 assert progressbar.env.is_terminal(fd) is True
52
53 # Sanity check
54 monkeypatch.setattr(progressbar.env, 'JUPYTER', False)
55 assert progressbar.env.is_terminal(fd) is False
56
57 monkeypatch.setenv('PROGRESSBAR_IS_TERMINAL', 'true')
58 assert progressbar.env.is_terminal(fd) is True
59 monkeypatch.setenv('PROGRESSBAR_IS_TERMINAL', 'false')
60 assert progressbar.env.is_terminal(fd) is False
61 monkeypatch.delenv('PROGRESSBAR_IS_TERMINAL')
62
63 # Sanity check
64 assert progressbar.env.is_terminal(fd) is False
65
66
67 def test_is_ansi_terminal(monkeypatch) -> None:
68 fd = io.StringIO()
69
70 monkeypatch.delenv('PROGRESSBAR_IS_TERMINAL', raising=False)
71 monkeypatch.setattr(progressbar.env, 'JUPYTER', False)
72
73 assert not progressbar.env.is_ansi_terminal(fd)
74 assert progressbar.env.is_ansi_terminal(fd, True) is True
75 assert progressbar.env.is_ansi_terminal(fd, False) is False
76
77 monkeypatch.setattr(progressbar.env, 'JUPYTER', True)
78 assert progressbar.env.is_ansi_terminal(fd) is True
79 monkeypatch.setattr(progressbar.env, 'JUPYTER', False)
80
81 # Sanity check
82 assert not progressbar.env.is_ansi_terminal(fd)
83
84 monkeypatch.setenv('PROGRESSBAR_IS_TERMINAL', 'true')
85 assert not progressbar.env.is_ansi_terminal(fd)
86 monkeypatch.setenv('PROGRESSBAR_IS_TERMINAL', 'false')
87 assert not progressbar.env.is_ansi_terminal(fd)
88 monkeypatch.delenv('PROGRESSBAR_IS_TERMINAL')
89
90 # Sanity check
91 assert not progressbar.env.is_ansi_terminal(fd)
92
93 # Fake TTY mode for environment testing
94 fd.isatty = lambda: True
95 monkeypatch.setenv('TERM', 'xterm')
96 assert progressbar.env.is_ansi_terminal(fd) is True
97 monkeypatch.setenv('TERM', 'xterm-256')
98 assert progressbar.env.is_ansi_terminal(fd) is True
99 monkeypatch.setenv('TERM', 'xterm-256color')
100 assert progressbar.env.is_ansi_terminal(fd) is True
101 monkeypatch.setenv('TERM', 'xterm-24bit')
102 assert progressbar.env.is_ansi_terminal(fd) is True
103 monkeypatch.delenv('TERM')
104
105 monkeypatch.setenv('ANSICON', 'true')
106 assert progressbar.env.is_ansi_terminal(fd) is True
107 monkeypatch.delenv('ANSICON')
108 assert not progressbar.env.is_ansi_terminal(fd)
109
110 def raise_error():
111 raise RuntimeError('test')
112
113 fd.isatty = raise_error
114 assert not progressbar.env.is_ansi_terminal(fd)