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
|
from __future__ import annotations
from contextlib import nullcontext
from typing import TYPE_CHECKING
import pytest
from streamlink.utils.args import boolean, comma_list, comma_list_filter, filesize, keyvalue, num
if TYPE_CHECKING:
from collections.abc import Sequence
does_not_raise = nullcontext()
@pytest.mark.parametrize(
("value", "expected", "raises"),
[
("1", True, does_not_raise),
("on", True, does_not_raise),
("true", True, does_not_raise),
("yes", True, does_not_raise),
("YES", True, does_not_raise),
("0", False, does_not_raise),
("off", False, does_not_raise),
("false", False, does_not_raise),
("no", False, does_not_raise),
("NO", False, does_not_raise),
("invalid", None, pytest.raises(ValueError, match=r"invalid is not one of .+")),
("2", None, pytest.raises(ValueError, match=r"2 is not one of .+")),
],
)
def test_boolean(value: str, expected: bool, raises: nullcontext):
with raises:
assert boolean(value) is expected
@pytest.mark.parametrize(
("value", "expected"),
[
pytest.param("foo", ["foo"], id="single item"),
pytest.param("foo.bar,example.com", ["foo.bar", "example.com"], id="separator"),
pytest.param("/var/run/foo,/var/run/bar", ["/var/run/foo", "/var/run/bar"], id="paths"),
pytest.param("foo bar,baz", ["foo bar", "baz"], id="whitespace"),
],
)
def test_comma_list(value: str, expected: list[str]):
assert comma_list(value) == expected
@pytest.mark.parametrize(
("options", "value", "expected"),
[
pytest.param(
{"acceptable": ["foo", "bar"]},
"foo,bar,baz,qux",
["foo", "bar"],
id="superset",
),
pytest.param(
{"acceptable": ["foo", "bar", "baz"]},
"foo,bar",
["foo", "bar"],
id="subset",
),
pytest.param(
{"acceptable": ["foo", "bar"], "unique": True},
"foo,bar,baz,foo,bar,baz",
["bar", "foo"],
id="unique",
),
],
)
def test_comma_list_filter(options: dict, value: str, expected: list[str]):
func = comma_list_filter(**options)
assert func(value) == expected
def test_comma_list_filter_hashable():
assert hash(comma_list_filter(["1", "2"])) != hash(comma_list_filter(["1", "2", "3"]))
assert hash(comma_list_filter(["1", "2"], unique=True)) == hash(comma_list_filter(["1", "2"], unique=True))
assert hash(comma_list_filter(["1", "2"], unique=True)) != hash(comma_list_filter(["1", "2"], unique=False))
@pytest.mark.parametrize(
("value", "expected", "raises"),
[
("12345", 12345, does_not_raise),
("123.45", int(123.45), does_not_raise),
("1KB", 1 * 2**10, does_not_raise),
("123kB", 123 * 2**10, does_not_raise),
("123.45kB", int(123.45 * 2**10), does_not_raise),
("1mb", 1 * 2**20, does_not_raise),
("123k", 123 * 2**10, does_not_raise),
("123M", 123 * 2**20, does_not_raise),
("123.45M", int(123.45 * 2**20), does_not_raise),
(" 123.45MB ", int(123.45 * 2**20), does_not_raise),
("FOO", None, pytest.raises(ValueError, match=r"^Invalid file size format$")),
("0", None, pytest.raises(ValueError, match=r"^int value must be >=1, but is 0$")),
("0.00000", None, pytest.raises(ValueError, match=r"^int value must be >=1, but is 0$")),
],
)
def test_filesize(value: str, expected: int, raises: nullcontext):
with raises:
assert filesize(value) == expected
@pytest.mark.parametrize(
("value", "expected", "raises"),
[
pytest.param(
"X-Forwarded-For=127.0.0.1",
("X-Forwarded-For", "127.0.0.1"),
does_not_raise,
id="separator",
),
pytest.param(
"foo=bar=baz",
("foo", "bar=baz"),
does_not_raise,
id="value with separator",
),
pytest.param(
"foo=",
("foo", ""),
does_not_raise,
id="missing value",
),
pytest.param(
" foo = bar ",
("foo", "bar "),
does_not_raise,
id="whitespace",
),
pytest.param(
"User-Agent=Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0",
("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"),
does_not_raise,
id="user-agent",
),
pytest.param(
"127.0.0.1",
None,
pytest.raises(ValueError, match=r"^Invalid key=value format$"),
id="invalid format",
),
pytest.param(
"=value",
None,
pytest.raises(ValueError, match=r"^Invalid key=value format$"),
id="missing key",
),
],
)
def test_keyvalue(value: str, expected: Sequence[str], raises: nullcontext):
with raises:
assert keyvalue(value) == expected
class TestNum:
@pytest.mark.parametrize(
("numtype", "value", "expected", "raises"),
[
(int, 123, 123, does_not_raise),
(float, 123, 123.0, does_not_raise),
(int, 123.456, 123, does_not_raise),
(int, "123", 123, does_not_raise),
(int, "-123", -123, does_not_raise),
(float, "123.456", 123.456, does_not_raise),
(float, "3.1415e2", 314.15, does_not_raise),
(int, "", None, pytest.raises(ValueError, match=r"^invalid literal for int\(\) with base 10:")),
(int, ".", None, pytest.raises(ValueError, match=r"^invalid literal for int\(\) with base 10:")),
(int, "-", None, pytest.raises(ValueError, match=r"^invalid literal for int\(\) with base 10:")),
(int, "foo", None, pytest.raises(ValueError, match=r"^invalid literal for int\(\) with base 10:")),
(float, "", None, pytest.raises(ValueError, match=r"^could not convert string to float:")),
(float, ".", None, pytest.raises(ValueError, match=r"^could not convert string to float:")),
(float, "-", None, pytest.raises(ValueError, match=r"^could not convert string to float:")),
(float, "foo", None, pytest.raises(ValueError, match=r"^could not convert string to float:")),
],
)
def test_numtype(self, numtype: type[float], value: float, expected: float, raises: nullcontext):
func = num(numtype)
assert func.__name__ == numtype.__name__
with raises:
result = func(value)
assert type(result) is numtype
assert result == expected
@pytest.mark.parametrize(
("operators", "value", "raises"),
[
({"ge": 1}, 1, does_not_raise),
({"ge": 0}, 1, does_not_raise),
({"gt": 0}, 1, does_not_raise),
({"le": 1}, 1, does_not_raise),
({"le": 2}, 1, does_not_raise),
({"lt": 2}, 1, does_not_raise),
({"ge": 1, "gt": 0, "le": 1, "lt": 2}, 1, does_not_raise),
({"ge": 2}, 1, pytest.raises(ValueError, match=r"^int value must be >=2, but is 1$")),
({"gt": 1}, 1, pytest.raises(ValueError, match=r"^int value must be >1, but is 1$")),
({"le": 0}, 1, pytest.raises(ValueError, match=r"^int value must be <=0, but is 1$")),
({"lt": 1}, 1, pytest.raises(ValueError, match=r"^int value must be <1, but is 1$")),
({"ge": 1, "gt": 0, "le": 0, "lt": 2}, 1, pytest.raises(ValueError, match=r"^int value must be <=0, but is 1$")),
],
)
def test_operator(self, operators: dict, value: float, raises: nullcontext):
with raises:
assert num(int, **operators)(value) == value
def test_hashable(self):
assert hash(num(int, ge=1)) == hash(num(int, ge=1))
assert hash(num(float, ge=1.0)) == hash(num(float, ge=1.0))
assert hash(num(int, ge=1)) != hash(num(int, gt=1))
assert hash(num(int, ge=1)) != hash(num(int, le=1))
assert hash(num(int, ge=1)) != hash(num(int, lt=1))
assert hash(num(int, ge=1)) != hash(num(float, ge=1.0))
|