2
2
from io import StringIO
3
3
from pathlib import PurePath , PurePosixPath , PureWindowsPath
4
4
from time import time
5
- from unittest .mock import Mock , call , patch
5
+ from unittest .mock import Mock
6
6
7
7
import freezegun
8
8
import pytest
@@ -21,6 +21,11 @@ def params(self):
21
21
path = lambda * _ : "PATH" ,
22
22
)
23
23
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
+
24
29
@pytest .mark .parametrize (("term_width" , "expected" ), [
25
30
(99 , "[download] Written WRITTEN to PATH (ELAPSED @ SPEED)" ),
26
31
(63 , "[download] Written WRITTEN to PATH (ELAPSED @ SPEED)" ),
@@ -32,10 +37,9 @@ def params(self):
32
37
(28 , "[download] WRITTEN (ELAPSED)" ),
33
38
(27 , "[download] WRITTEN" ),
34
39
(1 , "[download] WRITTEN" ),
35
- ])
40
+ ], indirect = [ "term_width" ] )
36
41
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
39
43
40
44
@pytest .mark .parametrize (("term_width" , "expected" ), [
41
45
(99 , "[download] Written WRITTEN to PATH (ELAPSED)" ),
@@ -46,20 +50,17 @@ def test_format(self, params, term_width, expected):
46
50
(28 , "[download] WRITTEN (ELAPSED)" ),
47
51
(27 , "[download] WRITTEN" ),
48
52
(1 , "[download] WRITTEN" ),
49
- ])
53
+ ], indirect = [ "term_width" ] )
50
54
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
53
56
54
57
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"
57
59
58
60
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)"
63
64
64
65
@pytest .mark .parametrize (("size" , "expected" ), [
65
66
(0 , "0 bytes" ),
@@ -111,9 +112,9 @@ def test_format_time(self, elapsed, expected):
111
112
112
113
class _TestFormatPath :
113
114
# 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
117
118
118
119
119
120
@pytest .mark .parametrize (("path" , "max_width" , "expected" ), [
@@ -205,10 +206,9 @@ def test_cut(self, prefix, max_len, expected):
205
206
206
207
class TestPrint :
207
208
@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 )
212
212
213
213
@pytest .fixture ()
214
214
def stream (self ):
@@ -238,63 +238,68 @@ def test_print_windows(self, progress: Progress, stream: StringIO):
238
238
239
239
240
240
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 ):
242
257
kib = b"\x00 " * 1024
243
- output_write = Mock ()
258
+ stream = StringIO ()
244
259
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" ),
247
262
interval = 1 ,
248
263
history = 3 ,
249
264
threshold = 2 ,
250
265
)
251
266
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) "
298
303
299
304
def test_update (self ):
300
305
handshake = Handshake ()
0 commit comments