Skip to content

Commit 870fda0

Browse files
committed
cli: update progress output after stream has ended
1 parent 7f33bc0 commit 870fda0

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/streamlink_cli/utils/progress.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,10 @@ def write(self, chunk: bytes):
251251
def run(self):
252252
self.started = time()
253253
try:
254-
while not self._wait.wait(self.interval): # pragma: no cover
254+
while not self._wait.wait(self.interval):
255255
self.update()
256256
finally:
257+
self.update()
257258
self.print_end()
258259

259260
def update(self):

tests/cli/utils/test_progress.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99

1010
from streamlink_cli.utils.progress import Progress, ProgressFormatter
11+
from tests.testutils.handshake import Handshake
1112

1213

1314
class TestProgressFormatter:
@@ -294,3 +295,43 @@ def test_download_speed(self):
294295
progress.update()
295296
assert output_write.call_args_list[-1] \
296297
== call("\r[download] Written 21.00 KiB (6s @ 6.00 KiB/s) ")
298+
299+
def test_update(self):
300+
handshake = Handshake()
301+
302+
class _Progress(Progress):
303+
def update(self):
304+
with handshake():
305+
return super().update()
306+
307+
stream = StringIO()
308+
thread = _Progress(stream=stream, path=PurePath())
309+
# override the thread's polling time after initializing the deque of the rolling average download speed:
310+
# the interval constructor keyword is used to set the deque size
311+
thread.interval = 0
312+
thread.start()
313+
314+
# first tick
315+
assert handshake.wait_ready(1)
316+
thread.write(b"123")
317+
assert handshake.step(1)
318+
assert stream.getvalue().split("\r")[-1].startswith("[download] Written 3 bytes")
319+
320+
# second tick
321+
assert handshake.wait_ready(1)
322+
thread.write(b"465")
323+
assert handshake.step(1)
324+
assert stream.getvalue().split("\r")[-1].startswith("[download] Written 6 bytes")
325+
326+
# close progress thread
327+
assert handshake.wait_ready(1)
328+
thread.close()
329+
assert handshake.step(1)
330+
assert stream.getvalue().split("\r")[-1].startswith("[download] Written 6 bytes")
331+
332+
# write data right after closing the thread, but before it has halted
333+
thread.write(b"789")
334+
handshake.go()
335+
thread.join(1)
336+
assert not thread.is_alive()
337+
assert stream.getvalue().split("\r")[-1].startswith("[download] Written 9 bytes")

0 commit comments

Comments
 (0)