Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix: make Windows CLI tests portable (#8788)
Sardor Taylakov1face0fcf0f6
diff --git a/tests/unit/browser_extension/conftest.py b/tests/unit/browser_extension/conftest.pyindex 7c88d4a19..371e75c2f 100644--- a/tests/unit/browser_extension/conftest.py+++ b/tests/unit/browser_extension/conftest.py@@ -1,3 +1,4 @@+import osimport tempfilefrom collections.abc import Iteratorfrom pathlib import Path@@ -9,5 +10,5 @@ from tests.unit.browser_extension.home_guard import isolate_browser_extension_ho@pytest.fixturedef short_broker_base_dir() -> Iterator[Path]:- with tempfile.TemporaryDirectory(prefix="be-", dir="/tmp") as base_dir:+ with tempfile.TemporaryDirectory(prefix="be-", dir=None if os.name == "nt" else "/tmp") as base_dir:yield Path(base_dir)diff --git a/tests/unit/browser_extension/home_guard.py b/tests/unit/browser_extension/home_guard.pyindex 6edd43b56..f1b4e2f6e 100644--- a/tests/unit/browser_extension/home_guard.py+++ b/tests/unit/browser_extension/home_guard.py@@ -1,35 +1,42 @@import os-import pwdimport tempfilefrom collections.abc import Iteratorfrom pathlib import Pathimport pytest-_REAL_HOME = Path(pwd.getpwuid(os.getuid()).pw_dir).resolve()+if os.name == "nt":+ _REAL_HOME = Path.home().resolve()+else:+ import pwd++ _REAL_HOME = Path(pwd.getpwuid(os.getuid()).pw_dir).resolve()def _test_broker_base_dir() -> Path:return Path(os.environ["SKYVERN_BROWSER_EXTENSION_TEST_BASE_DIR"])-def _assert_outside_real_home(path: Path, source: str) -> Path:+def _assert_isolated_path(path: Path, source: str, test_root: Path) -> Path:resolved = path.resolve()- if resolved == _REAL_HOME or resolved.is_relative_to(_REAL_HOME):+ # Windows commonly puts pytest's temporary directory beneath USERPROFILE.+ if resolved.is_relative_to(_REAL_HOME) and not resolved.is_relative_to(test_root.resolve()):pytest.fail(f"{source} resolved beneath the real home: {resolved}")return path@pytest.fixture(autouse=True)def isolate_browser_extension_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:- """Keep broker state and default home-derived paths out of the operator's home."""+ """Isolate broker state and home-derived paths in temporary test directories."""test_home = tmp_path / "home"monkeypatch.setenv("HOME", str(test_home))+ monkeypatch.setenv("USERPROFILE", str(test_home))def isolated_home(_cls: type[Path]) -> Path:- return _assert_outside_real_home(+ return _assert_isolated_path(Path(os.environ.get("HOME", str(test_home))),"Path.home()",+ tmp_path,)original_expanduser = os.path.expanduser@@ -42,11 +49,13 @@ def isolate_browser_extension_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatelse:expanded = original_expanduser(path)if path.startswith("~"):- _assert_outside_real_home(Path(expanded), "os.path.expanduser()")+ _assert_isolated_path(Path(expanded), "os.path.expanduser()", tmp_path)return expandedmonkeypatch.setattr(Path, "home", classmethod(isolated_home))monkeypatch.setattr(os.path, "expanduser", isolated_expanduser)- with tempfile.TemporaryDirectory(prefix="skyvern-browser-extension-", dir="/tmp") as base_dir:+ with tempfile.TemporaryDirectory(+ prefix="skyvern-browser-extension-", dir=None if os.name == "nt" else "/tmp"+ ) as base_dir:monkeypatch.setenv("SKYVERN_BROWSER_EXTENSION_TEST_BASE_DIR", base_dir)yielddiff --git a/tests/unit/browser_extension/test_home_guard.py b/tests/unit/browser_extension/test_home_guard.pynew file mode 100644index 000000000..18db434c2--- /dev/null+++ b/tests/unit/browser_extension/test_home_guard.py@@ -0,0 +1,26 @@+from pathlib import Path++import pytest++from tests.unit.browser_extension import home_guard++[email protected]("relative_path", ["", ".skyvern", "AppData/Local/Temp/unrelated"])+def test_home_guard_rejects_operator_paths(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, relative_path: str) -> None:+ real_home = tmp_path / "operator"+ monkeypatch.setattr(home_guard, "_REAL_HOME", real_home)+ test_root = real_home / "AppData/Local/Temp/pytest/sandbox"++ with pytest.raises(pytest.fail.Exception, match="resolved beneath the real home"):+ home_guard._assert_isolated_path(real_home / relative_path, "test", test_root)+++def test_home_guard_allows_pytest_sandbox_beneath_operator_home(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ real_home = tmp_path / "operator"+ monkeypatch.setattr(home_guard, "_REAL_HOME", real_home)+ test_root = real_home / "AppData/Local/Temp/pytest/sandbox"+ test_home = test_root / "home"++ assert home_guard._assert_isolated_path(test_home, "test", test_root) == test_homediff --git a/tests/unit/test_init_mcp.py b/tests/unit/test_init_mcp.pyindex 496c4d4c2..f0ebd483c 100644--- a/tests/unit/test_init_mcp.py+++ b/tests/unit/test_init_mcp.py@@ -11,6 +11,7 @@ def test_setup_mcp_local_claude_code_uses_local_stdio(monkeypatch) -> None:answers = iter([True, False, False, False])calls: list[dict] = []+ monkeypatch.setattr("skyvern.cli.setup_commands.sys.stdin.isatty", lambda: False)monkeypatch.setattr("skyvern.cli.mcp.Confirm.ask", lambda *args, **kwargs: next(answers))monkeypatch.setattr("skyvern.cli.mcp.setup_claude", lambda **kwargs: (_ for _ in ()).throw(AssertionError))monkeypatch.setattr("skyvern.cli.mcp.setup_cursor", lambda **kwargs: (_ for _ in ()).throw(AssertionError))diff --git a/tests/unit/test_run_commands_cleanup.py b/tests/unit/test_run_commands_cleanup.pyindex 829d27d54..87b4bfeb1 100644--- a/tests/unit/test_run_commands_cleanup.py+++ b/tests/unit/test_run_commands_cleanup.py@@ -29,6 +29,24 @@ def _reset_cleanup_state() -> None:run_commands._mcp_shutdown_exit_code = None[email protected](params=["poll", "peek"])+def stdin_eof_reader(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch) -> MagicMock:+ """Exercise both stdin EOF detectors without relying on host-specific select APIs."""+ reader = MagicMock()+ if request.param == "poll":+ reader.return_value = [(123, 1)]+ poller = SimpleNamespace(register=MagicMock(), poll=reader)+ select_api = SimpleNamespace(poll=lambda: poller, POLLHUP=1, POLLERR=2, POLLNVAL=4)+ else:+ reader.return_value = b""+ select_api = SimpleNamespace()+ monkeypatch.setattr(run_commands, "select", select_api)+ monkeypatch.setattr(+ run_commands.sys, "stdin", SimpleNamespace(fileno=lambda: 123, buffer=SimpleNamespace(peek=reader))+ )+ return reader++@pytest.mark.asyncioasync def test_cleanup_mcp_resources_closes_auth_db(monkeypatch: pytest.MonkeyPatch) -> None:order: list[str] = []@@ -179,10 +197,7 @@ def test_cleanup_mcp_resources_sync_preserves_explicit_user_data_dir(rmtree.assert_not_called()-def test_stdin_eof_watcher_allows_native_clean_return(monkeypatch: pytest.MonkeyPatch) -> None:- poller = MagicMock()- poller.poll.return_value = [(123, run_commands.select.POLLHUP)]- monkeypatch.setattr(run_commands.select, "poll", lambda: poller)+def test_stdin_eof_watcher_allows_native_clean_return(stdin_eof_reader: MagicMock) -> None:request_shutdown, force_exit = MagicMock(), MagicMock()stop = MagicMock(**{"is_set.side_effect": [False, False, True]})@@ -194,6 +209,7 @@ def test_stdin_eof_watcher_allows_native_clean_return(monkeypatch: pytest.Monkeyforce_exit=force_exit,)+ stdin_eof_reader.assert_called_once()request_shutdown.assert_not_called()force_exit.assert_not_called()@@ -202,16 +218,14 @@ def test_stdin_eof_watcher_allows_native_clean_return(monkeypatch: pytest.Monkeydef test_stdin_eof_watcher_force_exits_after_shared_profile_cleanup(monkeypatch: pytest.MonkeyPatch,deleted: bool,+ stdin_eof_reader: MagicMock,) -> None:events: list[str] = []- poller = MagicMock()- poller.poll.return_value = [(123, run_commands.select.POLLHUP)]profile = MagicMock(name="profile")profile_cleanup = MagicMock(side_effect=lambda _profile: events.append("cleanup") or deleted)terminate = MagicMock()rmtree = MagicMock(side_effect=AssertionError("run_commands must not delete profiles directly"))force_exit = MagicMock(side_effect=lambda _code: events.append("exit"))- monkeypatch.setattr(run_commands.select, "poll", lambda: poller)monkeypatch.setattr(run_commands,"_current_local_browser_identity",@@ -368,12 +382,13 @@ def test_run_mcp_restores_signal_handlers_after_cleanup(monkeypatch: pytest.Monkassert events[-3:] == ["cleanup", "restore", "restore"]-def test_run_mcp_stdin_eof_invokes_original_loop_cleanup(monkeypatch: pytest.MonkeyPatch) -> None:+def test_run_mcp_stdin_eof_invokes_original_loop_cleanup(+ monkeypatch: pytest.MonkeyPatch, stdin_eof_reader: MagicMock+) -> None:cleanup = AsyncMock()request_shutdown, force_exit = MagicMock(), MagicMock()eof_detected = threading.Event()- poller = MagicMock()- poller.poll.side_effect = lambda _timeout: (eof_detected.set(), [(123, run_commands.select.POLLHUP)])[1]+ stdin_eof_reader.side_effect = lambda _size: (eof_detected.set(), stdin_eof_reader.return_value)[1]async def return_on_eof(**_kwargs: object) -> None:assert await asyncio.to_thread(eof_detected.wait, 1)@@ -381,9 +396,6 @@ def test_run_mcp_stdin_eof_invokes_original_loop_cleanup(monkeypatch: pytest.Monmonkeypatch.setattr(run_commands, "_cleanup_mcp_resources", cleanup)monkeypatch.setattr(run_commands._thread, "interrupt_main", request_shutdown)monkeypatch.setattr(run_commands.os, "_exit", force_exit)- monkeypatch.setattr(run_commands.select, "poll", lambda: poller)- # pytest's captured stdin raises on fileno(); the poller is mocked, so any fd stands in.- monkeypatch.setattr(run_commands.sys, "stdin", SimpleNamespace(fileno=lambda: 123))monkeypatch.setattr(run_commands.atexit, "register", MagicMock())monkeypatch.setattr("skyvern.cli.mcp_tools.mcp.run_async", return_on_eof)