-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathtest_progressbar.py
More file actions
202 lines (162 loc) · 6.1 KB
/
Copy pathtest_progressbar.py
File metadata and controls
202 lines (162 loc) · 6.1 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import contextlib
import gc
import io
import os
import signal
import sys
import time
from datetime import timedelta
import original_examples # type: ignore
import pytest
import progressbar
from progressbar import utils
# Import hack to allow for parallel Tox
try:
import examples
except ImportError:
import sys
_project_dir: str = os.path.dirname(os.path.dirname(__file__))
sys.path.append(_project_dir)
import examples
sys.path.remove(_project_dir)
def test_examples(monkeypatch) -> None:
for example in examples.examples:
with contextlib.suppress(ValueError):
example()
@pytest.mark.filterwarnings('ignore:.*maxval.*:DeprecationWarning')
@pytest.mark.parametrize('example', original_examples.examples)
def test_original_examples(example, monkeypatch) -> None:
monkeypatch.setattr(progressbar.ProgressBar, '_MINIMUM_UPDATE_INTERVAL', 1)
monkeypatch.setattr(time, 'sleep', lambda t: None)
example()
@pytest.mark.parametrize('example', examples.examples)
def test_examples_nullbar(monkeypatch, example) -> None:
# Patch progressbar to use null bar instead of regular progress bar
monkeypatch.setattr(progressbar, 'ProgressBar', progressbar.NullBar)
assert progressbar.ProgressBar._MINIMUM_UPDATE_INTERVAL < 0.0001
example()
def test_reuse() -> None:
bar = progressbar.ProgressBar()
bar.start()
for i in range(10):
bar.update(i)
bar.finish()
bar.start(init=True)
for i in range(10):
bar.update(i)
bar.finish()
bar.start(init=False)
for i in range(10):
bar.update(i)
bar.finish()
def test_dirty() -> None:
bar = progressbar.ProgressBar()
bar.start()
assert bar.started()
for i in range(10):
bar.update(i)
bar.finish(dirty=True)
assert bar.finished()
assert bar.started()
def test_negative_maximum() -> None:
with (
pytest.raises(ValueError),
progressbar.ProgressBar(max_value=-1) as progress,
):
progress.start()
def test_elapsed_data_spans_days() -> None:
# Regression: A1 - days_elapsed was computed from timedelta.seconds,
# which only contains the sub-day component.
bar = progressbar.ProgressBar(
max_value=10, fd=io.StringIO(), term_width=60
)
bar.start()
bar.start_time -= timedelta(days=2, hours=3, minutes=4)
data = bar.data()
expected_days = 2 + (3 * 3600 + 4 * 60) / 86400
assert data['days_elapsed'] == pytest.approx(expected_days, abs=0.01)
def test_restart_after_finish_writes_final_newline() -> None:
# Regression: A2 - init() did not reset _finished, so a reused bar
# never wrote its final newline (and never flushed) again.
bar = progressbar.ProgressBar(
max_value=5, fd=io.StringIO(), term_width=60, line_breaks=False
)
bar.start()
bar.update(5)
bar.finish()
assert bar.fd.getvalue().endswith('\n')
bar.fd = io.StringIO()
bar.start()
assert not bar._finished
bar.update(5)
bar.finish()
assert bar.fd.getvalue().endswith('\n')
def test_repeated_finish_keeps_capturing_balanced() -> None:
# Regression: A2 - every finish() call decremented the global
# capturing counter, even when the bar was already finished.
baseline = utils.streams.capturing
try:
bar = progressbar.ProgressBar(
max_value=5, fd=io.StringIO(), term_width=60
)
bar.start()
bar.update(5)
bar.finish()
bar.finish()
assert utils.streams.capturing == baseline
finally:
utils.streams.capturing = baseline
def test_del_suppresses_finish_errors(monkeypatch) -> None:
# Regression: A4 - __del__ only suppressed AttributeError; any other
# exception from finish() leaked out of the finalizer (reported via
# sys.unraisablehook during garbage collection).
class ExplodingIO(io.StringIO):
def write(self, value: str) -> int:
raise ValueError('I/O operation on closed file')
unraisable: list[object] = []
monkeypatch.setattr(sys, 'unraisablehook', unraisable.append)
bar = progressbar.ProgressBar(max_value=5, fd=io.StringIO(), term_width=60)
bar.start()
bar.fd = ExplodingIO()
del bar
gc.collect()
assert not unraisable
@pytest.mark.skipif(os.name == 'nt', reason='SIGWINCH is POSIX-only')
def test_sigwinch_restored_with_overlapping_bars() -> None:
# Regression: A5 - with two live bars, finishing them in creation
# order left a dangling handler installed.
from progressbar.bar import _ResizeRegistry
saved_handler = signal.getsignal(signal.SIGWINCH)
# Isolate the global registry so the assertions don't depend on bars
# left registered (and a handler left installed) by other tests
saved_bars = list(_ResizeRegistry.bars)
saved_prev = _ResizeRegistry.previous_handler
_ResizeRegistry.bars.clear()
_ResizeRegistry.previous_handler = None
# Start from a known sentinel handler so we can tell apart "still
# installed" from "restored" without depending on global state
signal.signal(signal.SIGWINCH, signal.SIG_IGN)
try:
bar1 = progressbar.ProgressBar(max_value=5, fd=io.StringIO())
bar1.start()
bar2 = progressbar.ProgressBar(max_value=5, fd=io.StringIO())
bar2.start()
# The first bar installs the shared handler
assert signal.getsignal(signal.SIGWINCH) is not signal.SIG_IGN
# A resize signal is dispatched to all live bars
signal.raise_signal(signal.SIGWINCH)
assert isinstance(bar1.term_width, int)
assert isinstance(bar2.term_width, int)
bar1.update(5)
bar1.finish()
# The handler must stay installed while bar2 is still live
assert signal.getsignal(signal.SIGWINCH) is not signal.SIG_IGN
bar2.update(5)
bar2.finish()
# The last bar to finish restores the previous handler
assert signal.getsignal(signal.SIGWINCH) is signal.SIG_IGN
finally:
for restored_bar in saved_bars:
_ResizeRegistry.bars.add(restored_bar)
_ResizeRegistry.previous_handler = saved_prev
signal.signal(signal.SIGWINCH, saved_handler)