Debian Codegraph @

tests/test_monitor_progress.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 from __future__ import annotations
2
3 # fmt: off
4 import os
5 import pprint
6
•7 import progressbar2
8
9 pytest_plugins = 'pytester'
10
11 SCRIPT = """
12 import sys
13 sys.path.append({progressbar_path!r})
14 import time
15 import timeit
16 import freezegun
•17 import progressbar2
18
19
20 with freezegun.freeze_time() as fake_time:
21 timeit.default_timer = time.time
•22 with progressbar2.ProgressBar(widgets={widgets}, **{kwargs!r}) as bar:
23 bar._MINIMUM_UPDATE_INTERVAL = 1e-9
24 for i in bar({items}):
25 {loop_code}
26 """
27
28
29 def _non_empty_lines(lines):
30 return [line for line in lines if line.strip()]
31
32
33 def _create_script(
34 widgets=None,
35 items: list[int] | None=None,
36 loop_code: str='fake_time.tick(1)',
37 term_width: int=60,
38 **kwargs,
39 ) -> str:
40 if items is None:
41 items = list(range(9))
42 kwargs['term_width'] = term_width
43
44 # Reindent the loop code
45 indent = '\n '
46 loop_code = loop_code.strip('\n').split('\n')
47 dedent = len(loop_code[0]) - len(loop_code[0].lstrip())
48 for i, line in enumerate(loop_code):
49 loop_code[i] = line[dedent:]
50
51 script = SCRIPT.format(
52 items=items,
53 widgets=widgets,
54 kwargs=kwargs,
55 loop_code=indent.join(loop_code),
56 progressbar_path=os.path.dirname(
•57 os.path.dirname(progressbar2.__file__),
58 ),
59 )
60 print('# Script:')
61 print('#' * 78)
62 print(script)
63 print('#' * 78)
64
65 return script
66
67
68 def test_list_example(testdir) -> None:
69 """Run the simple example code in a python subprocess and then compare its
70 stderr to what we expect to see from it. We run it in a subprocess to
71 best capture its stderr. We expect to see match_lines in order in the
72 output. This test is just a sanity check to ensure that the progress
73 bar progresses from 1 to 10, it does not make sure that the"""
74
75 result = testdir.runpython(
76 testdir.makepyfile(
77 _create_script(
78 term_width=65,
79 ),
80 ),
81 )
82 result.stderr.lines = [
83 line.rstrip() for line in _non_empty_lines(result.stderr.lines)
84 ]
85 pprint.pprint(result.stderr.lines, width=70)
86 result.stderr.fnmatch_lines([
87 ' 0% (0 of 9) | | Elapsed Time: ?:00:00 ETA: --:--:--',
88 ' 11% (1 of 9) |# | Elapsed Time: ?:00:01 ETA: ?:00:08',
89 ' 22% (2 of 9) |## | Elapsed Time: ?:00:02 ETA: ?:00:07',
90 ' 33% (3 of 9) |#### | Elapsed Time: ?:00:03 ETA: ?:00:06',
91 ' 44% (4 of 9) |##### | Elapsed Time: ?:00:04 ETA: ?:00:05',
92 ' 55% (5 of 9) |###### | Elapsed Time: ?:00:05 ETA: ?:00:04',
93 ' 66% (6 of 9) |######## | Elapsed Time: ?:00:06 ETA: ?:00:03',
94 ' 77% (7 of 9) |######### | Elapsed Time: ?:00:07 ETA: ?:00:02',
95 ' 88% (8 of 9) |########## | Elapsed Time: ?:00:08 ETA: ?:00:01',
96 '100% (9 of 9) |############| Elapsed Time: ?:00:09 Time: ?:00:09',
97 ])
98
99
100 def test_generator_example(testdir) -> None:
101 """Run the simple example code in a python subprocess and then compare its
102 stderr to what we expect to see from it. We run it in a subprocess to
103 best capture its stderr. We expect to see match_lines in order in the
104 output. This test is just a sanity check to ensure that the progress
105 bar progresses from 1 to 10, it does not make sure that the"""
106 result = testdir.runpython(
107 testdir.makepyfile(
108 _create_script(
109 items='iter(range(9))',
110 ),
111 ),
112 )
113 result.stderr.lines = _non_empty_lines(result.stderr.lines)
114 pprint.pprint(result.stderr.lines, width=70)
115
116 lines = [
117 r'[/\\|\-]\s+\|\s*#\s*\| %(i)d Elapsed Time: \d:00:%(i)02d' % dict(i=i)
118 for i in range(9)
119 ]
120 result.stderr.re_match_lines(lines)
121
122
123 def test_rapid_updates(testdir) -> None:
124 """Run some example code that updates 10 times, then sleeps .1 seconds,
125 this is meant to test that the progressbar progresses normally with
126 this sample code, since there were issues with it in the past"""
127
128 result = testdir.runpython(
129 testdir.makepyfile(
130 _create_script(
131 term_width=60,
132 items=list(range(10)),
133 loop_code="""
134 if i < 5:
135 fake_time.tick(1)
136 else:
137 fake_time.tick(2)
138 """,
139 ),
140 ),
141 )
142 result.stderr.lines = _non_empty_lines(result.stderr.lines)
143 pprint.pprint(result.stderr.lines, width=70)
144 result.stderr.fnmatch_lines(
145 [
146 ' 0% (0 of 10) | | Elapsed Time: 0:00:00 ETA: --:--:--',
147 ' 10% (1 of 10) | | Elapsed Time: 0:00:01 ETA: 0:00:09',
148 ' 20% (2 of 10) |# | Elapsed Time: 0:00:02 ETA: 0:00:08',
149 ' 30% (3 of 10) |# | Elapsed Time: 0:00:03 ETA: 0:00:07',
150 ' 40% (4 of 10) |## | Elapsed Time: 0:00:04 ETA: 0:00:06',
151 ' 50% (5 of 10) |### | Elapsed Time: 0:00:05 ETA: 0:00:05',
152 ' 60% (6 of 10) |### | Elapsed Time: 0:00:07 ETA: 0:00:04',
153 ' 70% (7 of 10) |#### | Elapsed Time: 0:00:09 ETA: 0:00:03',
154 ' 80% (8 of 10) |#### | Elapsed Time: 0:00:11 ETA: 0:00:02',
155 ' 90% (9 of 10) |##### | Elapsed Time: 0:00:13 ETA: 0:00:01',
156 '100% (10 of 10) |#####| Elapsed Time: 0:00:15 Time: 0:00:15',
157 ],
158 )
159
160
161 def test_non_timed(testdir) -> None:
162 result = testdir.runpython(
163 testdir.makepyfile(
164 _create_script(
•165 widgets='[progressbar2.Percentage(), progressbar2.Bar()]',
166 items=list(range(5)),
167 ),
168 ),
169 )
170 result.stderr.lines = _non_empty_lines(result.stderr.lines)
171 pprint.pprint(result.stderr.lines, width=70)
172 result.stderr.fnmatch_lines(
173 [
174 ' 0%| |',
175 ' 20%|########## |',
176 ' 40%|##################### |',
177 ' 60%|################################ |',
178 ' 80%|########################################### |',
179 '100%|######################################################|',
180 ],
181 )
182
183
184 def test_line_breaks(testdir) -> None:
185 result = testdir.runpython(
186 testdir.makepyfile(
187 _create_script(
•188 widgets='[progressbar2.Percentage(), progressbar2.Bar()]',
189 line_breaks=True,
190 items=list(range(5)),
191 ),
192 ),
193 )
194 pprint.pprint(result.stderr.str(), width=70)
195 assert result.stderr.str() == '\n'.join(
196 (
197 ' 0%| |',
198 ' 20%|########## |',
199 ' 40%|##################### |',
200 ' 60%|################################ |',
201 ' 80%|########################################### |',
202 '100%|######################################################|',
203 '100%|######################################################|',
204 ),
205 )
206
207
208 def test_no_line_breaks(testdir) -> None:
209 result = testdir.runpython(
210 testdir.makepyfile(
211 _create_script(
•212 widgets='[progressbar2.Percentage(), progressbar2.Bar()]',
213 line_breaks=False,
214 items=list(range(5)),
215 ),
216 ),
217 )
218 pprint.pprint(result.stderr.lines, width=70)
219 assert result.stderr.lines == [
220 '',
221 ' 0%| |',
222 ' 20%|########## |',
223 ' 40%|##################### |',
224 ' 60%|################################ |',
225 ' 80%|########################################### |',
226 '100%|######################################################|',
227 '',
228 '100%|######################################################|',
229 ]
230
231
232 def test_percentage_label_bar(testdir) -> None:
233 result = testdir.runpython(
234 testdir.makepyfile(
235 _create_script(
•236 widgets='[progressbar2.PercentageLabelBar()]',
237 line_breaks=False,
238 items=list(range(5)),
239 ),
240 ),
241 )
242 pprint.pprint(result.stderr.lines, width=70)
243 assert result.stderr.lines == [
244 '',
245 '| 0% |',
246 '|########### 20% |',
247 '|####################### 40% |',
248 '|###########################60%#### |',
249 '|###########################80%################ |',
250 '|###########################100%###########################|',
251 '',
252 '|###########################100%###########################|',
253 ]
254
255
256 def test_granular_bar(testdir) -> None:
257 result = testdir.runpython(
258 testdir.makepyfile(
259 _create_script(
•260 widgets='[progressbar2.GranularBar(markers=" .oO")]',
261 line_breaks=False,
262 items=list(range(5)),
263 ),
264 ),
265 )
266 pprint.pprint(result.stderr.lines, width=70)
267 assert result.stderr.lines == [
268 '',
269 '| |',
270 '|OOOOOOOOOOO. |',
271 '|OOOOOOOOOOOOOOOOOOOOOOO |',
272 '|OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOo |',
273 '|OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO. |',
274 '|OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO|',
275 '',
276 '|OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO|',
277 ]
278
279
280 def test_colors(testdir) -> None:
281 kwargs = dict(
282 items=range(1),
283 widgets=['\033[92mgreen\033[0m'],
284 )
285
286 result = testdir.runpython(
287 testdir.makepyfile(_create_script(enable_colors=True, **kwargs)),
288 )
289 pprint.pprint(result.stderr.lines, width=70)
290 assert result.stderr.lines == ['\x1b[92mgreen\x1b[0m'] * 3
291
292 result = testdir.runpython(
293 testdir.makepyfile(_create_script(enable_colors=False, **kwargs)),
294 )
295 pprint.pprint(result.stderr.lines, width=70)
296 assert result.stderr.lines == ['green'] * 3