File: test_setup_config_args.py

package info (click to toggle)
streamlink 8.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,564 kB
  • sloc: python: 51,188; sh: 184; makefile: 152
file content (291 lines) | stat: -rw-r--r-- 7,775 bytes parent folder | download | duplicates (2)
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
from __future__ import annotations

from argparse import Namespace
from pathlib import Path
from typing import TYPE_CHECKING
from unittest.mock import Mock, call

import pytest

import tests.resources
from streamlink.exceptions import NoPluginError
from streamlink_cli.main import setup_config_args


if TYPE_CHECKING:
    from streamlink import Streamlink


CONFIGDIR = Path(tests.resources.__path__[0], "cli", "config")


@pytest.fixture()
def _args(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch):
    monkeypatch.setattr("streamlink_cli.main.args", Namespace(**getattr(request, "param", {})))


@pytest.fixture()
def _config_files(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch):
    monkeypatch.setattr("streamlink_cli.main.CONFIG_FILES", getattr(request, "param", []))


@pytest.fixture()
def setup_args(monkeypatch: pytest.MonkeyPatch):
    mock_setup_args = Mock()
    monkeypatch.setattr("streamlink_cli.main.setup_args", mock_setup_args)

    return mock_setup_args


@pytest.fixture()
def session(monkeypatch: pytest.MonkeyPatch, session: Streamlink):
    def resolve_url(name):
        if name == "noplugin":
            raise NoPluginError()
        return name, Mock(__module__=name), name

    monkeypatch.setattr(session, "resolve_url", resolve_url)

    return session


# noinspection PyTestParametrized
@pytest.mark.usefixtures("_args", "_config_files")
@pytest.mark.parametrize(
    ("_args", "_config_files", "expected"),
    [
        pytest.param(
            {
                "no_config": False,
                "config": None,
                "url": None,
            },
            [
                CONFIGDIR / "primary",
            ],
            [
                CONFIGDIR / "primary",
            ],
            id="No URL, default config",
        ),
        pytest.param(
            {
                "no_config": False,
                "config": [
                    str(CONFIGDIR / "custom"),
                ],
                "url": None,
            },
            [
                CONFIGDIR / "primary",
            ],
            [
                CONFIGDIR / "custom",
            ],
            id="No URL, custom config",
        ),
        pytest.param(
            {
                "no_config": False,
                "config": [],
                "url": None,
            },
            [
                CONFIGDIR / "non-existent",
            ],
            None,
            id="No URL, non-existent default config",
        ),
        pytest.param(
            {
                "no_config": False,
                "config": [
                    str(CONFIGDIR / "non-existent"),
                ],
                "url": None,
            },
            [
                CONFIGDIR / "primary",
            ],
            None,
            id="No URL, non-existent custom config",
        ),
        pytest.param(
            {
                "no_config": False,
                "config": None,
                "url": "noplugin",
            },
            [
                CONFIGDIR / "primary",
            ],
            [
                CONFIGDIR / "primary",
            ],
            id="No plugin, default config",
        ),
        pytest.param(
            {
                "no_config": False,
                "config": [
                    str(CONFIGDIR / "custom"),
                ],
                "url": "noplugin",
            },
            [
                CONFIGDIR / "primary",
            ],
            [
                CONFIGDIR / "custom",
            ],
            id="No plugin, custom config",
        ),
        pytest.param(
            {
                "no_config": False,
                "config": [],
                "url": "noplugin",
            },
            [
                CONFIGDIR / "non-existent",
            ],
            None,
            id="No plugin, non-existent default config",
        ),
        pytest.param(
            {
                "no_config": False,
                "config": [
                    str(CONFIGDIR / "non-existent"),
                ],
                "url": "noplugin",
            },
            [
                CONFIGDIR / "primary",
            ],
            None,
            id="No plugin, non-existent custom config",
        ),
        pytest.param(
            {
                "no_config": False,
                "config": None,
                "url": "testplugin",
            },
            [
                CONFIGDIR / "primary",
            ],
            [
                CONFIGDIR / "primary",
                CONFIGDIR / "primary.testplugin",
            ],
            id="Testplugin, default config",
        ),
        pytest.param(
            {
                "no_config": False,
                "config": None,
                "url": "testplugin",
            },
            [
                CONFIGDIR / "non-existent",
            ],
            None,
            id="Testplugin, non-existent default config",
        ),
        pytest.param(
            {
                "no_config": False,
                "config": [
                    str(CONFIGDIR / "custom"),
                ],
                "url": "testplugin",
            },
            [
                CONFIGDIR / "primary",
            ],
            [
                CONFIGDIR / "custom",
                CONFIGDIR / "primary.testplugin",
            ],
            id="Testplugin, custom config",
        ),
        pytest.param(
            {
                "no_config": False,
                "config": [
                    str(CONFIGDIR / "non-existent"),
                ],
                "url": "testplugin",
            },
            [
                CONFIGDIR / "primary",
            ],
            [
                CONFIGDIR / "primary.testplugin",
            ],
            id="Testplugin, non-existent custom config",
        ),
        pytest.param(
            {
                "no_config": False,
                "config": [
                    str(CONFIGDIR / "non-existent"),
                    str(CONFIGDIR / "primary"),
                    str(CONFIGDIR / "secondary"),
                ],
                "url": "testplugin",
            },
            [
                CONFIGDIR / "primary",
            ],
            [
                CONFIGDIR / "secondary",
                CONFIGDIR / "primary",
                CONFIGDIR / "primary.testplugin",
            ],
            id="Testplugin, multiple custom configs",
        ),
        pytest.param(
            {
                "no_config": True,
                "config": [],
                "url": "testplugin",
            },
            [
                CONFIGDIR / "primary",
            ],
            None,
            id="No config, default config",
        ),
        pytest.param(
            {
                "no_config": True,
                "config": [
                    str(CONFIGDIR / "primary"),
                    str(CONFIGDIR / "secondary"),
                ],
                "url": "testplugin",
            },
            [
                CONFIGDIR / "primary",
            ],
            None,
            id="No config, multiple custom configs",
        ),
    ],
    indirect=["_args", "_config_files"],
)
@pytest.mark.parametrize("ignore_unknown", [True, False])
def test_setup_config_args(
    recwarn: pytest.WarningsRecorder,
    setup_args: Mock,
    expected: list | None,
    ignore_unknown: bool,
):
    parser = Mock()
    setup_config_args(parser, ignore_unknown=ignore_unknown)
    if expected is not None:
        assert setup_args.call_args_list == [call(parser, expected, ignore_unknown=ignore_unknown)]
    else:
        assert setup_args.call_args_list == []