FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-progressbar/tests/test_progressbar_command.py at develop · wolph/python-progressbar · GitHub
wolph
/
python-progressbar
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
106
Star
879
Code
Issues
1
Pull requests
0
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-progressbar
/
tests
/
test_progressbar_command.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
303 lines (236 loc) · 8.87 KB
Breadcrumbs
python-progressbar
/
tests
/
test_progressbar_command.py
Copy path
File metadata and controls
303 lines (236 loc) · 8.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import
io
import
pathlib
import
pytest
import
progressbar
import
progressbar
.
__main__
as
main
def
test_size_to_bytes
()
->
None
:
assert
main
.
size_to_bytes
(
'1'
)
==
1
assert
main
.
size_to_bytes
(
'1k'
)
==
1024
assert
main
.
size_to_bytes
(
'1m'
)
==
1048576
assert
main
.
size_to_bytes
(
'1g'
)
==
1073741824
assert
main
.
size_to_bytes
(
'1p'
)
==
1125899906842624
assert
main
.
size_to_bytes
(
'1024'
)
==
1024
assert
main
.
size_to_bytes
(
'1024k'
)
==
1048576
assert
main
.
size_to_bytes
(
'1024m'
)
==
1073741824
assert
main
.
size_to_bytes
(
'1024g'
)
==
1099511627776
assert
main
.
size_to_bytes
(
'1024p'
)
==
1152921504606846976
def
test_sleep_for_rate_limit_skips_when_unset
(
monkeypatch
)
->
None
:
calls
=
[]
monkeypatch
.
setattr
(
main
.
time
,
'sleep'
,
calls
.
append
)
main
.
_sleep_for_rate_limit
(
None
,
transferred
=
1024
,
started_at
=
0
,
now
=
1
)
assert
calls
==
[]
def
test_sleep_for_rate_limit_sleeps_when_ahead
(
monkeypatch
)
->
None
:
calls
=
[]
monkeypatch
.
setattr
(
main
.
time
,
'sleep'
,
calls
.
append
)
main
.
_sleep_for_rate_limit
(
1024
,
transferred
=
2048
,
started_at
=
0
,
now
=
1
)
assert
calls
==
[
1
]
def
test_sleep_for_rate_limit_skips_when_on_schedule
(
monkeypatch
)
->
None
:
calls
=
[]
monkeypatch
.
setattr
(
main
.
time
,
'sleep'
,
calls
.
append
)
main
.
_sleep_for_rate_limit
(
1024
,
transferred
=
1024
,
started_at
=
0
,
now
=
1
)
assert
calls
==
[]
def
test_main_passes_rate_limit
(
tmp_path
,
monkeypatch
)
->
None
:
sleeps
=
[]
monkeypatch
.
setattr
(
main
.
time
,
'sleep'
,
sleeps
.
append
)
monkeypatch
.
setattr
(
main
.
time
,
'monotonic'
,
lambda
:
0
)
file
=
tmp_path
/
'data.bin'
file
.
write_bytes
(
b'x'
*
2048
)
main
.
main
(
[
'--rate-limit'
,
'1k'
,
str
(
file
),
'-o'
,
str
(
tmp_path
/
'out.bin'
)],
)
assert
sleeps
def
test_filename_to_bytes
(
tmp_path
)
->
None
:
file
=
tmp_path
/
'test'
file
.
write_text
(
'test'
)
assert
main
.
size_to_bytes
(
f'@
{
file
}
'
)
==
4
with
pytest
.
raises
(
FileNotFoundError
):
main
.
size_to_bytes
(
f'@
{
tmp_path
/
"nonexistent"
}
'
)
def
test_create_argument_parser
()
->
None
:
parser
=
main
.
create_argument_parser
()
args
=
parser
.
parse_args
(
[
'-p'
,
'-t'
,
'-e'
,
'-r'
,
'-a'
,
'-b'
,
'-8'
,
'-T'
,
'-n'
,
'-q'
,
'input'
,
'-o'
,
'output'
,
]
)
assert
args
.
progress
is
True
assert
args
.
timer
is
True
assert
args
.
eta
is
True
assert
args
.
rate
is
True
assert
args
.
average_rate
is
True
assert
args
.
bytes
is
True
assert
args
.
bits
is
True
assert
args
.
buffer_percent
is
True
assert
args
.
last_written
is
None
assert
args
.
format
is
None
assert
args
.
numeric
is
True
assert
args
.
quiet
is
True
assert
args
.
input
==
[
'input'
]
assert
args
.
output
==
'output'
def
test_main_binary
(
capsys
)
->
None
:
# Call the main function with different command line arguments
main
.
main
(
[
'-p'
,
'-t'
,
'-e'
,
'-r'
,
'-a'
,
'-b'
,
'-8'
,
'-T'
,
'-n'
,
'-q'
,
__file__
,
]
)
captured
=
capsys
.
readouterr
()
assert
'test_main(capsys):'
in
captured
.
out
def
test_main_lines
(
capsys
)
->
None
:
# Call the main function with different command line arguments
main
.
main
(
[
'-p'
,
'-t'
,
'-e'
,
'-r'
,
'-a'
,
'-b'
,
'-8'
,
'-T'
,
'-n'
,
'-q'
,
'-l'
,
'-s'
,
f'@
{
__file__
}
'
,
__file__
,
]
)
captured
=
capsys
.
readouterr
()
assert
'test_main(capsys):'
in
captured
.
out
class
Input
(
io
.
StringIO
):
buffer
:
io
.
BytesIO
@
classmethod
def
create
(
cls
,
text
:
str
)
->
'Input'
:
instance
=
cls
(
text
)
instance
.
buffer
=
io
.
BytesIO
(
text
.
encode
())
return
instance
def
test_main_lines_output
(
monkeypatch
,
tmp_path
)
->
None
:
text
=
'my input'
monkeypatch
.
setattr
(
'sys.stdin'
,
Input
.
create
(
text
))
output_filename
=
tmp_path
/
'output'
main
.
main
([
'-l'
,
'-o'
,
str
(
output_filename
)])
assert
output_filename
.
read_text
()
==
text
def
test_main_bytes_output
(
monkeypatch
,
tmp_path
)
->
None
:
text
=
'my input'
monkeypatch
.
setattr
(
'sys.stdin'
,
Input
.
create
(
text
))
output_filename
=
tmp_path
/
'output'
main
.
main
([
'-o'
,
str
(
output_filename
)])
assert
output_filename
.
read_text
()
==
f'
{
text
}
'
def
test_missing_input
(
tmp_path
)
->
None
:
with
pytest
.
raises
(
SystemExit
):
main
.
main
([
str
(
tmp_path
/
'output'
)])
@
pytest
.
fixture
def
recorded_bars
(
monkeypatch
):
created
=
[]
class
RecordingProgressBar
(
progressbar
.
ProgressBar
):
def
__init__
(
self
,
**
kwargs
)
->
None
:
created
.
append
(
self
)
self
.
init_kwargs
=
kwargs
super
().
__init__
(
**
kwargs
)
class
RecordingNullBar
(
progressbar
.
NullBar
):
def
__init__
(
self
,
**
kwargs
)
->
None
:
created
.
append
(
self
)
self
.
init_kwargs
=
kwargs
super
().
__init__
(
**
kwargs
)
monkeypatch
.
setattr
(
main
.
progressbar
,
'ProgressBar'
,
RecordingProgressBar
)
monkeypatch
.
setattr
(
main
.
progressbar
,
'NullBar'
,
RecordingNullBar
)
return
created
def
test_build_widgets_honors_display_flags
()
->
None
:
parser
=
main
.
create_argument_parser
()
args
=
parser
.
parse_args
(
[
'--progress'
,
'--timer'
,
'--eta'
,
'--rate'
,
'--bytes'
]
)
widgets
=
main
.
_build_widgets
(
args
,
filesize_available
=
True
)
widget_types
=
tuple
(
type
(
widget
)
for
widget
in
widgets
if
not
isinstance
(
widget
,
str
)
)
assert
progressbar
.
Percentage
in
widget_types
assert
progressbar
.
Bar
in
widget_types
assert
progressbar
.
Timer
in
widget_types
assert
progressbar
.
AdaptiveETA
in
widget_types
assert
progressbar
.
FileTransferSpeed
in
widget_types
assert
progressbar
.
DataSize
in
widget_types
def
test_build_widgets_quiet_is_empty
()
->
None
:
parser
=
main
.
create_argument_parser
()
args
=
parser
.
parse_args
([
'--quiet'
])
assert
main
.
_build_widgets
(
args
,
filesize_available
=
True
)
==
[]
def
test_main_quiet_uses_null_bar
(
tmp_path
,
recorded_bars
)
->
None
:
file
=
tmp_path
/
'data.bin'
file
.
write_bytes
(
b'x'
*
16
)
main
.
main
([
'--quiet'
,
str
(
file
),
'-o'
,
str
(
tmp_path
/
'out.bin'
)])
assert
isinstance
(
recorded_bars
[
0
],
progressbar
.
NullBar
)
def
test_numeric_output_uses_line_breaks
(
tmp_path
,
recorded_bars
)
->
None
:
file
=
tmp_path
/
'data.bin'
file
.
write_bytes
(
b'x'
*
16
)
main
.
main
([
'--numeric'
,
str
(
file
),
'-o'
,
str
(
tmp_path
/
'out.bin'
)])
assert
recorded_bars
[
0
].
init_kwargs
[
'line_breaks'
]
is
True
assert
any
(
isinstance
(
widget
,
progressbar
.
Percentage
)
for
widget
in
recorded_bars
[
0
].
init_kwargs
[
'widgets'
]
)
def
test_main_passes_widgets
(
tmp_path
,
recorded_bars
)
->
None
:
# Regression: E2 - the configured widgets were built but never passed
# to the progress bar.
file
=
tmp_path
/
'data.bin'
file
.
write_bytes
(
b'x'
*
1024
)
main
.
main
([
str
(
file
),
'-o'
,
str
(
tmp_path
/
'out.bin'
)])
assert
recorded_bars
assert
recorded_bars
[
0
].
init_kwargs
.
get
(
'widgets'
)
def
test_main_line_mode_counts_bytes
(
tmp_path
,
recorded_bars
)
->
None
:
# Regression: E1 - line mode counted characters while the maximum was
# measured in bytes, so multi-byte content never reached 100%.
file
=
tmp_path
/
'data.txt'
file
.
write_text
((
'é'
*
99
+
'
\n
'
)
*
5
,
encoding
=
'utf-8'
)
size
=
file
.
stat
().
st_size
main
.
main
([
'-l'
,
str
(
file
),
'-o'
,
str
(
tmp_path
/
'out.txt'
)])
assert
recorded_bars
[
0
].
value
==
size
def
test_main_broken_pipe
(
tmp_path
,
monkeypatch
)
->
None
:
# Regression: E3 - an early-closing downstream pipe raised an
# unhandled BrokenPipeError.
file
=
tmp_path
/
'data.bin'
file
.
write_bytes
(
b'x'
*
1024
)
class
BrokenPipeIO
(
io
.
BytesIO
):
def
write
(
self
,
data
)
->
int
:
raise
BrokenPipeError
monkeypatch
.
setattr
(
main
,
'_get_output_stream'
,
lambda
*
args
:
BrokenPipeIO
()
)
main
.
main
([
str
(
file
)])
# must not raise
def
test_main_empty_file_has_known_size
(
tmp_path
,
recorded_bars
)
->
None
:
# Regression: E8 - a zero-byte input flipped the bar into
# unknown-length mode although the file size was known.
file
=
tmp_path
/
'empty.bin'
file
.
write_bytes
(
b''
)
main
.
main
([
str
(
file
),
'-o'
,
str
(
tmp_path
/
'out.bin'
)])
assert
recorded_bars
[
0
].
init_kwargs
.
get
(
'max_value'
)
==
0
def
test_console_scripts_include_the_bar_shorthand
()
->
None
:
# Both the canonical `progressbar` command and its `bar` shorthand
# must point at the same entry point: the shorthand exists purely
# for shorter pipelines (`bar data.bin -o copy.bin`).
pyproject
=
(
pathlib
.
Path
(
__file__
).
parents
[
1
]
/
'pyproject.toml'
).
read_text
(
encoding
=
'utf-8'
)
assert
"progressbar = 'progressbar.__main__:main'"
in
pyproject
assert
"bar = 'progressbar.__main__:main'"
in
pyproject
Back
|
FazBrowse Home
|
New Git URL