forked from wolph/python-progressbar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_monitor_progress.py
More file actions
241 lines (208 loc) · 9.53 KB
/
test_monitor_progress.py
File metadata and controls
241 lines (208 loc) · 9.53 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import os
import pprint
import progressbar
pytest_plugins = 'pytester'
SCRIPT = '''
import sys
sys.path.append({progressbar_path!r})
import time
import timeit
import freezegun
import progressbar
with freezegun.freeze_time() as fake_time:
timeit.default_timer = time.time
with progressbar.ProgressBar(widgets={widgets}, **{kwargs!r}) as bar:
bar._MINIMUM_UPDATE_INTERVAL = 1e-9
for i in bar({items}):
{loop_code}
'''
def _create_script(widgets=None, items=list(range(9)),
loop_code='fake_time.tick(1)', term_width=60,
**kwargs):
kwargs['term_width'] = term_width
# Reindent the loop code
indent = '\n '
loop_code = loop_code.strip('\n').split('\n')
dedent = len(loop_code[0]) - len(loop_code[0].lstrip())
for i, line in enumerate(loop_code):
loop_code[i] = line[dedent:]
script = SCRIPT.format(
items=items,
widgets=widgets,
kwargs=kwargs,
loop_code=indent.join(loop_code),
progressbar_path=os.path.dirname(os.path.dirname(
progressbar.__file__)),
)
print('# Script:')
print('#' * 78)
print(script)
print('#' * 78)
return script
def test_list_example(testdir):
''' Run the simple example code in a python subprocess and then compare its
stderr to what we expect to see from it. We run it in a subprocess to
best capture its stderr. We expect to see match_lines in order in the
output. This test is just a sanity check to ensure that the progress
bar progresses from 1 to 10, it does not make sure that the '''
result = testdir.runpython(testdir.makepyfile(_create_script(
term_width=65,
)))
result.stderr.lines = [l.rstrip() for l in result.stderr.lines
if l.strip()]
pprint.pprint(result.stderr.lines, width=70)
result.stderr.fnmatch_lines([
' 0% (0 of 9) | | Elapsed Time: ?:00:00 ETA: --:--:--',
' 11% (1 of 9) |# | Elapsed Time: ?:00:01 ETA: ?:00:08',
' 22% (2 of 9) |## | Elapsed Time: ?:00:02 ETA: ?:00:07',
' 33% (3 of 9) |#### | Elapsed Time: ?:00:03 ETA: ?:00:06',
' 44% (4 of 9) |##### | Elapsed Time: ?:00:04 ETA: ?:00:05',
' 55% (5 of 9) |###### | Elapsed Time: ?:00:05 ETA: ?:00:04',
' 66% (6 of 9) |######## | Elapsed Time: ?:00:06 ETA: ?:00:03',
' 77% (7 of 9) |######### | Elapsed Time: ?:00:07 ETA: ?:00:02',
' 88% (8 of 9) |########## | Elapsed Time: ?:00:08 ETA: ?:00:01',
'100% (9 of 9) |############| Elapsed Time: ?:00:09 Time: ?:00:09',
])
def test_generator_example(testdir):
''' Run the simple example code in a python subprocess and then compare its
stderr to what we expect to see from it. We run it in a subprocess to
best capture its stderr. We expect to see match_lines in order in the
output. This test is just a sanity check to ensure that the progress
bar progresses from 1 to 10, it does not make sure that the '''
result = testdir.runpython(testdir.makepyfile(_create_script(
items='iter(range(9))',
)))
result.stderr.lines = [l for l in result.stderr.lines if l.strip()]
pprint.pprint(result.stderr.lines, width=70)
lines = [
r'[/\\|\-]\s+\|\s*#\s*\| %(i)d Elapsed Time: \d:00:%(i)02d' % dict(i=i)
for i in range(9)
]
result.stderr.re_match_lines(lines)
def test_rapid_updates(testdir):
''' Run some example code that updates 10 times, then sleeps .1 seconds,
this is meant to test that the progressbar progresses normally with
this sample code, since there were issues with it in the past '''
result = testdir.runpython(testdir.makepyfile(_create_script(
term_width=60,
items=list(range(10)),
loop_code='''
if i < 5:
fake_time.tick(1)
else:
fake_time.tick(2)
'''
)))
result.stderr.lines = [l for l in result.stderr.lines if l.strip()]
pprint.pprint(result.stderr.lines, width=70)
result.stderr.fnmatch_lines([
' 0% (0 of 10) | | Elapsed Time: ?:00:00 ETA: --:--:--',
' 10% (1 of 10) | | Elapsed Time: ?:00:01 ETA: ?:00:09',
' 20% (2 of 10) |# | Elapsed Time: ?:00:02 ETA: ?:00:08',
' 30% (3 of 10) |# | Elapsed Time: ?:00:03 ETA: ?:00:07',
' 40% (4 of 10) |## | Elapsed Time: ?:00:04 ETA: ?:00:06',
' 50% (5 of 10) |### | Elapsed Time: ?:00:05 ETA: ?:00:05',
' 60% (6 of 10) |### | Elapsed Time: ?:00:07 ETA: ?:00:06',
' 70% (7 of 10) |#### | Elapsed Time: ?:00:09 ETA: ?:00:06',
' 80% (8 of 10) |#### | Elapsed Time: ?:00:11 ETA: ?:00:04',
' 90% (9 of 10) |##### | Elapsed Time: ?:00:13 ETA: ?:00:02',
'100% (10 of 10) |#####| Elapsed Time: ?:00:15 Time: ?:00:15'
])
def test_non_timed(testdir):
result = testdir.runpython(testdir.makepyfile(_create_script(
widgets='[progressbar.Percentage(), progressbar.Bar()]',
items=list(range(5)),
)))
result.stderr.lines = [l for l in result.stderr.lines if l.strip()]
pprint.pprint(result.stderr.lines, width=70)
result.stderr.fnmatch_lines([
' 0%| |',
' 20%|########## |',
' 40%|##################### |',
' 60%|################################ |',
' 80%|########################################### |',
'100%|######################################################|',
])
def test_line_breaks(testdir):
result = testdir.runpython(testdir.makepyfile(_create_script(
widgets='[progressbar.Percentage(), progressbar.Bar()]',
line_breaks=True,
items=list(range(5)),
)))
pprint.pprint(result.stderr.str(), width=70)
assert result.stderr.str() == u'\n'.join((
u' 0%| |',
u' 20%|########## |',
u' 40%|##################### |',
u' 60%|################################ |',
u' 80%|########################################### |',
u'100%|######################################################|',
u'100%|######################################################|',
))
def test_no_line_breaks(testdir):
result = testdir.runpython(testdir.makepyfile(_create_script(
widgets='[progressbar.Percentage(), progressbar.Bar()]',
line_breaks=False,
items=list(range(5)),
)))
pprint.pprint(result.stderr.lines, width=70)
assert result.stderr.lines == [
u'',
u' 0%| |',
u' 20%|########## |',
u' 40%|##################### |',
u' 60%|################################ |',
u' 80%|########################################### |',
u'100%|######################################################|',
u'',
u'100%|######################################################|'
]
def test_percentage_label_bar(testdir):
result = testdir.runpython(testdir.makepyfile(_create_script(
widgets='[progressbar.PercentageLabelBar()]',
line_breaks=False,
items=list(range(5)),
)))
pprint.pprint(result.stderr.lines, width=70)
assert result.stderr.lines == [
u'',
u'| 0% |',
u'|########### 20% |',
u'|####################### 40% |',
u'|###########################60%#### |',
u'|###########################80%################ |',
u'|###########################100%###########################|',
u'',
u'|###########################100%###########################|'
]
def test_granular_bar(testdir):
result = testdir.runpython(testdir.makepyfile(_create_script(
widgets='[progressbar.GranularBar(markers=" .oO")]',
line_breaks=False,
items=list(range(5)),
)))
pprint.pprint(result.stderr.lines, width=70)
assert result.stderr.lines == [
u'',
u'| |',
u'|OOOOOOOOOOO. |',
u'|OOOOOOOOOOOOOOOOOOOOOOO |',
u'|OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOo |',
u'|OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO. |',
u'|OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO|',
u'',
u'|OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO|'
]
def test_colors(testdir):
kwargs = dict(
items=range(1),
widgets=['\033[92mgreen\033[0m'],
)
result = testdir.runpython(testdir.makepyfile(_create_script(
enable_colors=True, **kwargs)))
pprint.pprint(result.stderr.lines, width=70)
assert result.stderr.lines == [u'\x1b[92mgreen\x1b[0m'] * 3
result = testdir.runpython(testdir.makepyfile(_create_script(
enable_colors=False, **kwargs)))
pprint.pprint(result.stderr.lines, width=70)
assert result.stderr.lines == [u'green'] * 3