conftest.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import inspect
  2. import pytest
  3. from yt_dlp.networking import RequestHandler
  4. from yt_dlp.networking.common import _REQUEST_HANDLERS
  5. from yt_dlp.utils._utils import _YDLLogger as FakeLogger
  6. @pytest.fixture
  7. def handler(request):
  8. RH_KEY = getattr(request, 'param', None)
  9. if not RH_KEY:
  10. return
  11. if inspect.isclass(RH_KEY) and issubclass(RH_KEY, RequestHandler):
  12. handler = RH_KEY
  13. elif RH_KEY in _REQUEST_HANDLERS:
  14. handler = _REQUEST_HANDLERS[RH_KEY]
  15. else:
  16. pytest.skip(f'{RH_KEY} request handler is not available')
  17. class HandlerWrapper(handler):
  18. RH_KEY = handler.RH_KEY
  19. def __init__(self, **kwargs):
  20. super().__init__(logger=FakeLogger, **kwargs)
  21. return HandlerWrapper
  22. @pytest.fixture(autouse=True)
  23. def skip_handler(request, handler):
  24. """usage: pytest.mark.skip_handler('my_handler', 'reason')"""
  25. for marker in request.node.iter_markers('skip_handler'):
  26. if marker.args[0] == handler.RH_KEY:
  27. pytest.skip(marker.args[1] if len(marker.args) > 1 else '')
  28. @pytest.fixture(autouse=True)
  29. def skip_handler_if(request, handler):
  30. """usage: pytest.mark.skip_handler_if('my_handler', lambda request: True, 'reason')"""
  31. for marker in request.node.iter_markers('skip_handler_if'):
  32. if marker.args[0] == handler.RH_KEY and marker.args[1](request):
  33. pytest.skip(marker.args[2] if len(marker.args) > 2 else '')
  34. @pytest.fixture(autouse=True)
  35. def skip_handlers_if(request, handler):
  36. """usage: pytest.mark.skip_handlers_if(lambda request, handler: True, 'reason')"""
  37. for marker in request.node.iter_markers('skip_handlers_if'):
  38. if handler and marker.args[0](request, handler):
  39. pytest.skip(marker.args[1] if len(marker.args) > 1 else '')
  40. def pytest_configure(config):
  41. config.addinivalue_line(
  42. 'markers', 'skip_handler(handler): skip test for the given handler',
  43. )
  44. config.addinivalue_line(
  45. 'markers', 'skip_handler_if(handler): skip test for the given handler if condition is true',
  46. )
  47. config.addinivalue_line(
  48. 'markers', 'skip_handlers_if(handler): skip test for handlers when the condition is true',
  49. )