Skip to content

Commit 6bbbb8e

Browse files
committed
tests: refactor CLI progress tests
1 parent 870fda0 commit 6bbbb8e

File tree

1 file changed

+75
-70
lines changed

1 file changed

+75
-70
lines changed

tests/cli/utils/test_progress.py

Lines changed: 75 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from io import StringIO
33
from pathlib import PurePath, PurePosixPath, PureWindowsPath
44
from time import time
5-
from unittest.mock import Mock, call, patch
5+
from unittest.mock import Mock
66

77
import freezegun
88
import pytest
@@ -21,6 +21,11 @@ def params(self):
2121
path=lambda *_: "PATH",
2222
)
2323

24+
@pytest.fixture(autouse=True)
25+
def term_width(self, request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch): # noqa: PT004
26+
width = getattr(request, "param", 99)
27+
monkeypatch.setattr("streamlink_cli.utils.progress.ProgressFormatter.term_width", lambda: width)
28+
2429
@pytest.mark.parametrize(("term_width", "expected"), [
2530
(99, "[download] Written WRITTEN to PATH (ELAPSED @ SPEED)"),
2631
(63, "[download] Written WRITTEN to PATH (ELAPSED @ SPEED)"),
@@ -32,10 +37,9 @@ def params(self):
3237
(28, "[download] WRITTEN (ELAPSED)"),
3338
(27, "[download] WRITTEN"),
3439
(1, "[download] WRITTEN"),
35-
])
40+
], indirect=["term_width"])
3641
def test_format(self, params, term_width, expected):
37-
with patch("streamlink_cli.utils.progress.ProgressFormatter.term_width", lambda: term_width):
38-
assert ProgressFormatter.format(ProgressFormatter.FORMATS, params) == expected
42+
assert ProgressFormatter.format(ProgressFormatter.FORMATS, params) == expected
3943

4044
@pytest.mark.parametrize(("term_width", "expected"), [
4145
(99, "[download] Written WRITTEN to PATH (ELAPSED)"),
@@ -46,20 +50,17 @@ def test_format(self, params, term_width, expected):
4650
(28, "[download] WRITTEN (ELAPSED)"),
4751
(27, "[download] WRITTEN"),
4852
(1, "[download] WRITTEN"),
49-
])
53+
], indirect=["term_width"])
5054
def test_format_nospeed(self, params, term_width, expected):
51-
with patch("streamlink_cli.utils.progress.ProgressFormatter.term_width", lambda: term_width):
52-
assert ProgressFormatter.format(ProgressFormatter.FORMATS_NOSPEED, params) == expected
55+
assert ProgressFormatter.format(ProgressFormatter.FORMATS_NOSPEED, params) == expected
5356

5457
def test_format_missing(self, params):
55-
with patch("streamlink_cli.utils.progress.ProgressFormatter.term_width", lambda: 99):
56-
assert ProgressFormatter.format(ProgressFormatter.FORMATS, {"written": "0"}) == "[download] 0"
58+
assert ProgressFormatter.format(ProgressFormatter.FORMATS, {"written": "0"}) == "[download] 0"
5759

5860
def test_format_error(self, params):
59-
with patch("streamlink_cli.utils.progress.ProgressFormatter.term_width", lambda: 99):
60-
params = dict(**params)
61-
params["path"] = Mock(side_effect=ValueError("fail"))
62-
assert ProgressFormatter.format(ProgressFormatter.FORMATS, params) == "[download] Written WRITTEN (ELAPSED @ SPEED)"
61+
params = dict(**params)
62+
params["path"] = Mock(side_effect=ValueError("fail"))
63+
assert ProgressFormatter.format(ProgressFormatter.FORMATS, params) == "[download] Written WRITTEN (ELAPSED @ SPEED)"
6364

6465
@pytest.mark.parametrize(("size", "expected"), [
6566
(0, "0 bytes"),
@@ -111,9 +112,9 @@ def test_format_time(self, elapsed, expected):
111112

112113
class _TestFormatPath:
113114
# noinspection PyMethodMayBeStatic
114-
def test_format_path(self, path: PurePath, max_width: int, expected: str):
115-
with patch("os.path.sep", "\\" if type(path) is PureWindowsPath else "/"):
116-
assert ProgressFormatter.format_path(path, max_width) == expected
115+
def test_format_path(self, monkeypatch: pytest.MonkeyPatch, path: PurePath, max_width: int, expected: str):
116+
monkeypatch.setattr("os.path.sep", "\\" if type(path) is PureWindowsPath else "/")
117+
assert ProgressFormatter.format_path(path, max_width) == expected
117118

118119

119120
@pytest.mark.parametrize(("path", "max_width", "expected"), [
@@ -205,10 +206,9 @@ def test_cut(self, prefix, max_len, expected):
205206

206207
class TestPrint:
207208
@pytest.fixture(autouse=True)
208-
def _terminal_size(self):
209-
with patch("streamlink_cli.utils.progress.get_terminal_size") as mock_get_terminal_size:
210-
mock_get_terminal_size.return_value = Mock(columns=10)
211-
yield
209+
def _get_terminal_size(self, monkeypatch: pytest.MonkeyPatch):
210+
mock_get_terminal_size = Mock(return_value=Mock(columns=10))
211+
monkeypatch.setattr("streamlink_cli.utils.progress.get_terminal_size", mock_get_terminal_size)
212212

213213
@pytest.fixture()
214214
def stream(self):
@@ -238,63 +238,68 @@ def test_print_windows(self, progress: Progress, stream: StringIO):
238238

239239

240240
class TestProgress:
241-
def test_download_speed(self):
241+
@pytest.fixture(autouse=True)
242+
def _setup(self, monkeypatch: pytest.MonkeyPatch):
243+
monkeypatch.setattr("os.path.sep", "/")
244+
245+
@pytest.fixture(autouse=True)
246+
def mock_width(self, monkeypatch: pytest.MonkeyPatch):
247+
mock = Mock(return_value=70)
248+
monkeypatch.setattr("streamlink_cli.utils.progress.ProgressFormatter.term_width", mock)
249+
return mock
250+
251+
@pytest.fixture()
252+
def frozen_time(self):
253+
with freezegun.freeze_time("2000-01-01T00:00:00Z") as frozen_time:
254+
yield frozen_time
255+
256+
def test_download_speed(self, mock_width: Mock, frozen_time):
242257
kib = b"\x00" * 1024
243-
output_write = Mock()
258+
stream = StringIO()
244259
progress = Progress(
245-
Mock(write=output_write),
246-
PurePosixPath("../../the/path/where/we/write/to"),
260+
stream=stream,
261+
path=PurePosixPath("../../the/path/where/we/write/to"),
247262
interval=1,
248263
history=3,
249264
threshold=2,
250265
)
251266

252-
with freezegun.freeze_time("2000-01-01T00:00:00Z") as frozen_time, \
253-
patch("os.path.sep", "/"), \
254-
patch("streamlink_cli.utils.progress.ProgressFormatter.term_width", Mock(return_value=70)) as mock_width:
255-
progress.started = time()
256-
assert not output_write.call_args_list
257-
258-
progress.update()
259-
assert output_write.call_args_list[-1] \
260-
== call("\r[download] Written 0 bytes to ../../the/path/where/we/write/to (0s) ")
261-
262-
frozen_time.tick()
263-
progress.write(kib * 1)
264-
progress.update()
265-
assert output_write.call_args_list[-1] \
266-
== call("\r[download] Written 1.00 KiB to …th/where/we/write/to (1s @ 1.00 KiB/s)")
267-
268-
frozen_time.tick()
269-
mock_width.return_value = 65
270-
progress.write(kib * 3)
271-
progress.update()
272-
assert output_write.call_args_list[-1] \
273-
== call("\r[download] Written 4.00 KiB to …ere/we/write/to (2s @ 2.00 KiB/s)")
274-
275-
frozen_time.tick()
276-
mock_width.return_value = 60
277-
progress.write(kib * 5)
278-
progress.update()
279-
assert output_write.call_args_list[-1] \
280-
== call("\r[download] Written 9.00 KiB (3s @ 4.50 KiB/s) ")
281-
282-
frozen_time.tick()
283-
progress.write(kib * 7)
284-
progress.update()
285-
assert output_write.call_args_list[-1] \
286-
== call("\r[download] Written 16.00 KiB (4s @ 7.50 KiB/s) ")
287-
288-
frozen_time.tick()
289-
progress.write(kib * 5)
290-
progress.update()
291-
assert output_write.call_args_list[-1] \
292-
== call("\r[download] Written 21.00 KiB (5s @ 8.50 KiB/s) ")
293-
294-
frozen_time.tick()
295-
progress.update()
296-
assert output_write.call_args_list[-1] \
297-
== call("\r[download] Written 21.00 KiB (6s @ 6.00 KiB/s) ")
267+
progress.started = time()
268+
assert stream.getvalue() == ""
269+
270+
progress.update()
271+
assert stream.getvalue().split("\r")[-1] == "[download] Written 0 bytes to ../../the/path/where/we/write/to (0s) "
272+
273+
frozen_time.tick()
274+
progress.write(kib * 1)
275+
progress.update()
276+
assert stream.getvalue().split("\r")[-1] == "[download] Written 1.00 KiB to …th/where/we/write/to (1s @ 1.00 KiB/s)"
277+
278+
frozen_time.tick()
279+
mock_width.return_value = 65
280+
progress.write(kib * 3)
281+
progress.update()
282+
assert stream.getvalue().split("\r")[-1] == "[download] Written 4.00 KiB to …ere/we/write/to (2s @ 2.00 KiB/s)"
283+
284+
frozen_time.tick()
285+
mock_width.return_value = 60
286+
progress.write(kib * 5)
287+
progress.update()
288+
assert stream.getvalue().split("\r")[-1] == "[download] Written 9.00 KiB (3s @ 4.50 KiB/s) "
289+
290+
frozen_time.tick()
291+
progress.write(kib * 7)
292+
progress.update()
293+
assert stream.getvalue().split("\r")[-1] == "[download] Written 16.00 KiB (4s @ 7.50 KiB/s) "
294+
295+
frozen_time.tick()
296+
progress.write(kib * 5)
297+
progress.update()
298+
assert stream.getvalue().split("\r")[-1] == "[download] Written 21.00 KiB (5s @ 8.50 KiB/s) "
299+
300+
frozen_time.tick()
301+
progress.update()
302+
assert stream.getvalue().split("\r")[-1] == "[download] Written 21.00 KiB (6s @ 6.00 KiB/s) "
298303

299304
def test_update(self):
300305
handshake = Handshake()

0 commit comments

Comments
 (0)