Skip to content

Commit

Permalink
Decorator to adapt tests for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
elacuesta committed Jun 21, 2024
1 parent 8b6414d commit 4674ba8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
26 changes: 16 additions & 10 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import inspect
import logging
import platform
from contextlib import asynccontextmanager
from functools import wraps

import pytest
from scrapy import Request
from scrapy.http.response.html import HtmlResponse
from scrapy.utils.test import get_crawler


logger = logging.getLogger("scrapy-playwright-tests")


if platform.system() == "Windows":
from scrapy_playwright.handler import _WindowsAdapter

def windows_pytest_mark_asyncio(pytest_mark_asyncio):
def wrapper(*args, **kwargs):
if args and inspect.iscoroutinefunction(args[0]):
def allow_windows(test_method):
if not inspect.iscoroutinefunction(test_method):
raise RuntimeError(f"{test_method} must be an async def method")

async def method_proxy(*x):
await _WindowsAdapter.get_result(args[0](*x))
@wraps(test_method)
async def wrapped(self, *args, **kwargs):
logger.debug("Calling _WindowsAdapter.get_result for %r", self)
await _WindowsAdapter.get_result(test_method(self, *args, **kwargs))

return pytest_mark_asyncio(method_proxy)
return windows_pytest_mark_asyncio(pytest_mark_asyncio(*args, **kwargs))
return wrapped

return wrapper
else:

pytest.mark.asyncio = windows_pytest_mark_asyncio(pytest.mark.asyncio)
def allow_windows(test_method):
return test_method


@asynccontextmanager
Expand Down
3 changes: 2 additions & 1 deletion tests/tests_asyncio/test_playwright_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from scrapy_playwright.handler import DEFAULT_CONTEXT_NAME
from scrapy_playwright.page import PageMethod

from tests import make_handler, assert_correct_response
from tests import allow_windows, make_handler, assert_correct_response
from tests.mockserver import MockServer, StaticMockServer


Expand All @@ -41,6 +41,7 @@ def inject_fixtures(self, caplog):
caplog.set_level(logging.DEBUG)
self._caplog = caplog

@allow_windows
async def test_basic_response(self):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with StaticMockServer() as server:
Expand Down

0 comments on commit 4674ba8

Please sign in to comment.