| 1 |
import signal |
| 2 |
import sys |
| 3 |
import time |
| 4 |
from datetime import timedelta |
| 5 |
|
| •6 |
import progressbar2 |
| 7 |
from progressbar2 import terminal |
| 8 |
|
| 9 |
|
| 10 |
def test_left_justify() -> None: |
| 11 |
"""Left justify using the terminal width""" |
| •12 |
p = progressbar2.ProgressBar( |
| 13 |
widgets=[progressbar2.BouncingBar(marker=progressbar2.RotatingMarker())], |
| 14 |
max_value=100, |
| 15 |
term_width=20, |
| 16 |
left_justify=True, |
| 17 |
) |
| 18 |
|
| 19 |
assert p.term_width is not None |
| 20 |
for i in range(100): |
| 21 |
p.update(i) |
| 22 |
|
| 23 |
|
| 24 |
def test_right_justify() -> None: |
| 25 |
"""Right justify using the terminal width""" |
| •26 |
p = progressbar2.ProgressBar( |
| 27 |
widgets=[progressbar2.BouncingBar(marker=progressbar2.RotatingMarker())], |
| 28 |
max_value=100, |
| 29 |
term_width=20, |
| 30 |
left_justify=False, |
| 31 |
) |
| 32 |
|
| 33 |
assert p.term_width is not None |
| 34 |
for i in range(100): |
| 35 |
p.update(i) |
| 36 |
|
| 37 |
|
| 38 |
def test_auto_width(monkeypatch) -> None: |
| 39 |
"""Right justify using the terminal width""" |
| 40 |
|
| 41 |
def ioctl(*args): |
| 42 |
return '\xbf\x00\xeb\x00\x00\x00\x00\x00' |
| 43 |
|
| 44 |
def fake_signal(signal, func): |
| 45 |
pass |
| 46 |
|
| 47 |
try: |
| 48 |
import fcntl |
| 49 |
|
| 50 |
monkeypatch.setattr(fcntl, 'ioctl', ioctl) |
| 51 |
monkeypatch.setattr(signal, 'signal', fake_signal) |
| •52 |
p = progressbar2.ProgressBar( |
| 53 |
widgets=[ |
| •54 |
progressbar2.BouncingBar(marker=progressbar2.RotatingMarker()), |
| 55 |
], |
| 56 |
max_value=100, |
| 57 |
left_justify=True, |
| 58 |
term_width=None, |
| 59 |
) |
| 60 |
|
| 61 |
assert p.term_width is not None |
| 62 |
for i in range(100): |
| 63 |
p.update(i) |
| 64 |
except ImportError: |
| 65 |
pass |
| 66 |
|
| 67 |
|
| 68 |
def test_fill_right() -> None: |
| 69 |
"""Right justify using the terminal width""" |
| •70 |
p = progressbar2.ProgressBar( |
| 71 |
widgets=[progressbar2.BouncingBar(fill_left=False)], |
| 72 |
max_value=100, |
| 73 |
term_width=20, |
| 74 |
) |
| 75 |
|
| 76 |
assert p.term_width is not None |
| 77 |
for i in range(100): |
| 78 |
p.update(i) |
| 79 |
|
| 80 |
|
| 81 |
def test_fill_left() -> None: |
| 82 |
"""Right justify using the terminal width""" |
| •83 |
p = progressbar2.ProgressBar( |
| 84 |
widgets=[progressbar2.BouncingBar(fill_left=True)], |
| 85 |
max_value=100, |
| 86 |
term_width=20, |
| 87 |
) |
| 88 |
|
| 89 |
assert p.term_width is not None |
| 90 |
for i in range(100): |
| 91 |
p.update(i) |
| 92 |
|
| 93 |
|
| 94 |
def test_no_fill(monkeypatch) -> None: |
| 95 |
"""Simply bounce within the terminal width""" |
| •96 |
bar = progressbar2.BouncingBar() |
| 97 |
bar.INTERVAL = timedelta(seconds=1) |
| •98 |
p = progressbar2.ProgressBar( |
| 99 |
widgets=[bar], |
| •100 |
max_value=progressbar2.UnknownLength, |
| 101 |
term_width=20, |
| 102 |
) |
| 103 |
|
| 104 |
assert p.term_width is not None |
| 105 |
for i in range(30): |
| 106 |
p.update(i, force=True) |
| 107 |
|
| 108 |
p.start_time = p.start_time - timedelta(seconds=i) |
| 109 |
|
| 110 |
|
| 111 |
def test_stdout_redirection() -> None: |
| •112 |
p = progressbar2.ProgressBar( |
| 113 |
fd=sys.stdout, |
| 114 |
max_value=10, |
| 115 |
redirect_stdout=True, |
| 116 |
) |
| 117 |
|
| 118 |
for i in range(10): |
| 119 |
print('', file=sys.stdout) |
| 120 |
p.update(i) |
| 121 |
|
| 122 |
|
| 123 |
def test_double_stdout_redirection() -> None: |
| •124 |
p = progressbar2.ProgressBar(max_value=10, redirect_stdout=True) |
| 125 |
p2 = progressbar2.ProgressBar(max_value=10, redirect_stdout=True) |
| 126 |
|
| 127 |
for i in range(10): |
| 128 |
print('', file=sys.stdout) |
| 129 |
p.update(i) |
| 130 |
p2.update(i) |
| 131 |
|
| 132 |
|
| 133 |
def test_stderr_redirection() -> None: |
| •134 |
p = progressbar2.ProgressBar(max_value=10, redirect_stderr=True) |
| 135 |
|
| 136 |
for i in range(10): |
| 137 |
print('', file=sys.stderr) |
| 138 |
p.update(i) |
| 139 |
|
| 140 |
|
| 141 |
def test_stdout_stderr_redirection() -> None: |
| •142 |
p = progressbar2.ProgressBar( |
| 143 |
max_value=10, |
| 144 |
redirect_stdout=True, |
| 145 |
redirect_stderr=True, |
| 146 |
) |
| 147 |
p.start() |
| 148 |
|
| 149 |
for i in range(10): |
| 150 |
time.sleep(0.01) |
| 151 |
print('', file=sys.stdout) |
| 152 |
print('', file=sys.stderr) |
| 153 |
p.update(i) |
| 154 |
|
| 155 |
p.finish() |
| 156 |
|
| 157 |
|
| 158 |
def test_resize(monkeypatch) -> None: |
| 159 |
def ioctl(*args): |
| 160 |
return '\xbf\x00\xeb\x00\x00\x00\x00\x00' |
| 161 |
|
| 162 |
def fake_signal(signal, func): |
| 163 |
pass |
| 164 |
|
| 165 |
try: |
| 166 |
import fcntl |
| 167 |
|
| 168 |
monkeypatch.setattr(fcntl, 'ioctl', ioctl) |
| 169 |
monkeypatch.setattr(signal, 'signal', fake_signal) |
| 170 |
|
| •171 |
p = progressbar2.ProgressBar(max_value=10) |
| 172 |
p.start() |
| 173 |
|
| 174 |
for i in range(10): |
| 175 |
p.update(i) |
| 176 |
p._handle_resize() |
| 177 |
|
| 178 |
p.finish() |
| 179 |
except ImportError: |
| 180 |
pass |
| 181 |
|
| 182 |
|
| 183 |
def test_base() -> None: |
| 184 |
assert str(terminal.CUP) |
| 185 |
assert str(terminal.CLEAR_SCREEN_ALL_AND_HISTORY) |
| 186 |
|
| 187 |
terminal.clear_line(0) |
| 188 |
terminal.clear_line(1) |