11from __future__ import annotations
22
3- import contextlib
43import os
5- import subprocess
64
75import pytest
86from _pytest .config .argparsing import Parser
9- from filelock import FileLock
107
118from reactpy .config import (
129 REACTPY_ASYNC_RENDERING ,
1613 BackendFixture ,
1714 DisplayFixture ,
1815 capture_reactpy_logs ,
19- clear_reactpy_web_modules_dir ,
2016)
2117from reactpy .testing .common import GITHUB_ACTIONS
2218
@@ -33,59 +29,6 @@ def pytest_addoption(parser: Parser) -> None:
3329 )
3430
3531
36- def headless_environ (pytestconfig : pytest .Config ):
37- if (
38- pytestconfig .getoption ("headless" )
39- or os .environ .get ("PLAYWRIGHT_HEADLESS" ) == "1"
40- or GITHUB_ACTIONS
41- ):
42- os .environ ["PLAYWRIGHT_HEADLESS" ] = "1"
43- return True
44- return False
45-
46-
47- def get_lock_dir (tmp_path_factory , worker_id ):
48- if worker_id == "master" :
49- return tmp_path_factory .getbasetemp ()
50- return tmp_path_factory .getbasetemp ().parent
51-
52-
53- @pytest .fixture (autouse = True , scope = "session" )
54- def install_playwright (tmp_path_factory , worker_id ):
55- root_tmp_dir = get_lock_dir (tmp_path_factory , worker_id )
56- fn = root_tmp_dir / "playwright_install.lock"
57- flag = root_tmp_dir / "playwright_install.done"
58-
59- with FileLock (str (fn )):
60- if not flag .exists ():
61- subprocess .run (["playwright" , "install" , "chromium" ], check = True ) # noqa: S607
62- # Try to install system deps, but don't fail if already installed or no root access
63- with contextlib .suppress (subprocess .CalledProcessError ):
64- subprocess .run (["playwright" , "install-deps" ], check = True ) # noqa: S607
65- flag .touch ()
66-
67-
68- @pytest .fixture (autouse = True , scope = "session" )
69- def rebuild (tmp_path_factory , worker_id ):
70- # When running inside `hatch test`, the `HATCH_ENV_ACTIVE` environment variable
71- # is set. If we try to run `hatch build` with this variable set, Hatch will
72- # complain that the current environment is not a builder environment.
73- # To fix this, we remove `HATCH_ENV_ACTIVE` from the environment variables
74- # passed to the subprocess.
75- env = os .environ .copy ()
76- env .pop ("HATCH_ENV_ACTIVE" , None )
77-
78- root_tmp_dir = get_lock_dir (tmp_path_factory , worker_id )
79- fn = root_tmp_dir / "build.lock"
80- flag = root_tmp_dir / "build.done"
81-
82- # Whoever gets the lock first performs the build.
83- with FileLock (str (fn )):
84- if not flag .exists ():
85- subprocess .run (["hatch" , "build" , "-t" , "wheel" ], check = True , env = env ) # noqa: S607
86- flag .touch ()
87-
88-
8932@pytest .fixture (scope = "session" )
9033async def display (server , browser ):
9134 async with DisplayFixture (backend = server , browser = browser ) as display :
@@ -103,12 +46,7 @@ async def browser(pytestconfig: pytest.Config):
10346 from playwright .async_api import async_playwright
10447
10548 async with async_playwright () as pw :
106- yield await pw .chromium .launch (headless = headless_environ (pytestconfig ))
107-
108-
109- @pytest .fixture (autouse = True )
110- def clear_web_modules_dir_after_test ():
111- clear_reactpy_web_modules_dir ()
49+ yield await pw .chromium .launch (headless = _headless_environ (pytestconfig ))
11250
11351
11452@pytest .fixture (autouse = True )
@@ -121,3 +59,14 @@ def assert_no_logged_exceptions():
12159 raise r .exc_info [1 ]
12260 finally :
12361 records .clear ()
62+
63+
64+ def _headless_environ (pytestconfig : pytest .Config ):
65+ if (
66+ pytestconfig .getoption ("headless" )
67+ or os .environ .get ("PLAYWRIGHT_HEADLESS" ) == "1"
68+ or GITHUB_ACTIONS
69+ ):
70+ os .environ ["PLAYWRIGHT_HEADLESS" ] = "1"
71+ return True
72+ return False
0 commit comments