[ie/soundcloud] Various fixes (#11820)
[yt-dlp.git] / test / conftest.py
bloba8b92f811eb8b0b7b166ee46c84889bb017b6baf
1 import inspect
3 import pytest
5 from yt_dlp.networking import RequestHandler
6 from yt_dlp.networking.common import _REQUEST_HANDLERS
7 from yt_dlp.utils._utils import _YDLLogger as FakeLogger
10 @pytest.fixture
11 def handler(request):
12 RH_KEY = getattr(request, 'param', None)
13 if not RH_KEY:
14 return
15 if inspect.isclass(RH_KEY) and issubclass(RH_KEY, RequestHandler):
16 handler = RH_KEY
17 elif RH_KEY in _REQUEST_HANDLERS:
18 handler = _REQUEST_HANDLERS[RH_KEY]
19 else:
20 pytest.skip(f'{RH_KEY} request handler is not available')
22 class HandlerWrapper(handler):
23 RH_KEY = handler.RH_KEY
25 def __init__(self, **kwargs):
26 super().__init__(logger=FakeLogger, **kwargs)
28 return HandlerWrapper
31 @pytest.fixture(autouse=True)
32 def skip_handler(request, handler):
33 """usage: pytest.mark.skip_handler('my_handler', 'reason')"""
34 for marker in request.node.iter_markers('skip_handler'):
35 if marker.args[0] == handler.RH_KEY:
36 pytest.skip(marker.args[1] if len(marker.args) > 1 else '')
39 @pytest.fixture(autouse=True)
40 def skip_handler_if(request, handler):
41 """usage: pytest.mark.skip_handler_if('my_handler', lambda request: True, 'reason')"""
42 for marker in request.node.iter_markers('skip_handler_if'):
43 if marker.args[0] == handler.RH_KEY and marker.args[1](request):
44 pytest.skip(marker.args[2] if len(marker.args) > 2 else '')
47 @pytest.fixture(autouse=True)
48 def skip_handlers_if(request, handler):
49 """usage: pytest.mark.skip_handlers_if(lambda request, handler: True, 'reason')"""
50 for marker in request.node.iter_markers('skip_handlers_if'):
51 if handler and marker.args[0](request, handler):
52 pytest.skip(marker.args[1] if len(marker.args) > 1 else '')
55 def pytest_configure(config):
56 config.addinivalue_line(
57 'markers', 'skip_handler(handler): skip test for the given handler',
59 config.addinivalue_line(
60 'markers', 'skip_handler_if(handler): skip test for the given handler if condition is true',
62 config.addinivalue_line(
63 'markers', 'skip_handlers_if(handler): skip test for handlers when the condition is true',