-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Expand file tree
/
Copy pathtest_progress.py
More file actions
209 lines (178 loc) · 8.31 KB
/
Copy pathtest_progress.py
File metadata and controls
209 lines (178 loc) · 8.31 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
203
204
205
206
207
208
209
#!/usr/bin/env python
"""
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
See the file 'LICENSE' for copying permission
The textual progress bar (lib/utils/progress.py) used during multi-item
extraction. Pure rendering/clamping logic plus ETA formatting.
"""
import os
import re
import sys
import time
import unittest
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _testutils import bootstrap
bootstrap()
import lib.utils.progress as progress_mod
from lib.utils.progress import ProgressBar
class TestProgressBar(unittest.TestCase):
def test_initial_is_zero_percent(self):
pb = ProgressBar(0, 100, 78)
self.assertTrue(str(pb).startswith("0%"), msg=str(pb))
def test_full_is_hundred_percent(self):
pb = ProgressBar(0, 100, 78)
pb.update(100)
self.assertTrue(str(pb).startswith("100%"), msg=str(pb))
def test_half_is_fifty_percent(self):
pb = ProgressBar(0, 100, 78)
pb.update(50)
self.assertIn("50%", str(pb))
def test_update_clamps_below_min(self):
pb = ProgressBar(10, 20, 78)
pb.update(-5)
self.assertTrue(str(pb).startswith("0%"))
def test_update_clamps_above_max(self):
pb = ProgressBar(0, 10, 78)
pb.update(999)
self.assertTrue(str(pb).startswith("100%"))
def test_convert_seconds(self):
pb = ProgressBar(0, 10, 78)
self.assertEqual(pb._convertSeconds(0), "00:00")
self.assertEqual(pb._convertSeconds(65), "01:05")
self.assertEqual(pb._convertSeconds(600), "10:00")
def test_progress_draws_eta_after_second_call(self):
captured = []
real = progress_mod.dataToStdout
realTty = progress_mod.IS_TTY
progress_mod.dataToStdout = lambda data, *a, **k: captured.append(data)
progress_mod.IS_TTY = True # draw() only animates on a terminal
try:
pb = ProgressBar(0, 10, 78)
pb.progress(0) # first call only seeds the timer (eta None)
time.sleep(0.01) # let some wall-clock elapse so eta is computable
pb.progress(5) # second call computes and draws a real ETA
finally:
progress_mod.dataToStdout = real
progress_mod.IS_TTY = realTty
self.assertTrue(captured, msg="progress() never wrote to stdout")
last = captured[-1]
# the drawn bar must carry an ETA token with an mm:ss timer (not the ??:?? placeholder)
self.assertIn("(ETA ", last, msg="no ETA token drawn: %r" % last)
self.assertNotIn("??:??", last, msg="ETA was not computed on the second call: %r" % last)
self.assertTrue(re.search(r"\(ETA \d{2}:\d{2}\)", last),
msg="ETA token missing an mm:ss timer: %r" % last)
def test_eta_available_from_first_completed_item(self):
captured = []
real = progress_mod.dataToStdout
realTty = progress_mod.IS_TTY
progress_mod.dataToStdout = lambda data, *a, **k: captured.append(data)
progress_mod.IS_TTY = True
try:
pb = ProgressBar(0, 5, 78)
pb._start = pb._lastTime = time.time() - 2.0 # one item took ~2s -> estimate must appear now, at 1/5
pb.progress(1)
finally:
progress_mod.dataToStdout = real
progress_mod.IS_TTY = realTty
last = captured[-1]
self.assertNotIn("??:??", last, msg="no ETA at the first item: %r" % last)
m = re.search(r"\(ETA (\d{2}):(\d{2})\)", last)
secs = int(m.group(1)) * 60 + int(m.group(2))
self.assertTrue(7 <= secs <= 9, msg="ETA not ~8s (2s/item x 4 remaining): %r" % last) # not the 2x-optimistic ~4s
def test_eta_reflects_remaining_item_count(self):
# at a constant 10s/item pace: 1/3 must estimate the 2 items left (~20s), 2/3 the 1 left (~10s) -
# i.e. (max - done) items, not just the current one. Fresh bars so each is a first (unsmoothed) estimate.
captured = []
real = progress_mod.dataToStdout
realTty = progress_mod.IS_TTY
progress_mod.dataToStdout = lambda data, *a, **k: captured.append(data)
progress_mod.IS_TTY = True
def drawnEta():
m = re.search(r"\(ETA (\d{2}):(\d{2})\)", captured[-1])
self.assertTrue(m, msg="no ETA drawn: %r" % captured[-1])
return int(m.group(1)) * 60 + int(m.group(2))
try:
a = ProgressBar(0, 3, 78)
a._start = time.time() - 10.0 # 1 done in 10s -> 10s/item, 2 remaining -> ~20s
a.progress(1)
eta1 = drawnEta()
b = ProgressBar(0, 3, 78)
b._start = time.time() - 20.0 # 2 done in 20s -> 10s/item, 1 remaining -> ~10s
b.progress(2)
eta2 = drawnEta()
finally:
progress_mod.dataToStdout = real
progress_mod.IS_TTY = realTty
self.assertTrue(18 <= eta1 <= 22, msg="1/3 should estimate 2 remaining (~20s): %ds" % eta1)
self.assertTrue(8 <= eta2 <= 12, msg="2/3 should estimate 1 remaining (~10s): %ds" % eta2)
def test_new_estimate_is_eased_not_snapped(self):
# when a fresh estimate is far from the value currently on screen, the drawn ETA must land
# between the two (smoothed), not snap straight to the new target
captured = []
real = progress_mod.dataToStdout
realTty = progress_mod.IS_TTY
progress_mod.dataToStdout = lambda data, *a, **k: captured.append(data)
progress_mod.IS_TTY = True
try:
pb = ProgressBar(0, 10, 78)
pb._eta = 8.0 # currently showing ~8s...
pb._etaAt = time.time()
pb._start = time.time() - 2.0 # ...but the fresh target is (2/1)*(10-1) = 18s
pb.progress(1)
finally:
progress_mod.dataToStdout = real
progress_mod.IS_TTY = realTty
m = re.search(r"\(ETA (\d{2}):(\d{2})\)", captured[-1])
secs = int(m.group(1)) * 60 + int(m.group(2))
self.assertTrue(10 <= secs <= 16, msg="ETA snapped instead of easing between 8 and 18: %ds" % secs)
def test_tick_counts_down_from_stored_eta(self):
captured = []
real = progress_mod.dataToStdout
realTty = progress_mod.IS_TTY
progress_mod.dataToStdout = lambda data, *a, **k: captured.append(data)
progress_mod.IS_TTY = True
try:
pb = ProgressBar(0, 10, 78)
pb.update(5)
pb._eta = 100 # a 100s estimate...
pb._etaAt = time.time() - 30 # ...taken 30s ago -> tick() must show ~70s
pb.tick()
finally:
progress_mod.dataToStdout = real
progress_mod.IS_TTY = realTty
m = re.search(r"\(ETA (\d{2}):(\d{2})\)", captured[-1])
self.assertTrue(m, msg="no mm:ss ETA drawn: %r" % captured[-1])
secs = int(m.group(1)) * 60 + int(m.group(2))
self.assertTrue(66 <= secs <= 71, msg="ETA not decremented to ~70s: %r" % captured[-1])
def test_tick_clamps_at_zero_when_overdue(self):
captured = []
real = progress_mod.dataToStdout
realTty = progress_mod.IS_TTY
progress_mod.dataToStdout = lambda data, *a, **k: captured.append(data)
progress_mod.IS_TTY = True
try:
pb = ProgressBar(0, 10, 78)
pb.update(5)
pb._eta = 5
pb._etaAt = time.time() - 60 # long overdue -> must clamp to 00:00, never negative
pb.tick()
finally:
progress_mod.dataToStdout = real
progress_mod.IS_TTY = realTty
self.assertIn("(ETA 00:00)", captured[-1], msg=captured[-1])
def test_no_draw_when_not_tty(self):
captured = []
real = progress_mod.dataToStdout
realTty = progress_mod.IS_TTY
progress_mod.dataToStdout = lambda data, *a, **k: captured.append(data)
progress_mod.IS_TTY = False # piped/redirected: no animated bar should reach the stream
try:
pb = ProgressBar(0, 10, 78)
for i in range(1, 11):
pb.progress(i)
finally:
progress_mod.dataToStdout = real
progress_mod.IS_TTY = realTty
self.assertEqual(captured, [], msg="progress bar leaked to a non-TTY stream: %r" % captured)
if __name__ == "__main__":
unittest.main()