forked from UiPath/uipath-dev-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
83 lines (65 loc) · 2.23 KB
/
Copy pathconftest.py
File metadata and controls
83 lines (65 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""E2E-specific fixtures for Textual TUI and web server tests."""
import socket
import threading
import time
import pytest
from uipath.core.tracing import UiPathTraceManager
from tests.conftest import MockRuntimeFactory
from uipath.dev import UiPathDeveloperConsole
@pytest.fixture()
def app(mock_factory, trace_manager):
"""Create a UiPathDeveloperConsole instance for Textual pilot tests."""
return UiPathDeveloperConsole(
runtime_factory=mock_factory,
trace_manager=trace_manager,
)
def _find_free_port() -> int:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("127.0.0.1", 0))
return s.getsockname()[1]
@pytest.fixture(scope="session")
def live_server_url():
"""Start a real FastAPI server in a background thread and yield its URL.
Session-scoped so the server is started only once for all web tests.
Uses 'live_server_url' (not 'base_url') to avoid conflicting with the
autouse session fixture from pytest-base-url, which would force the
server to start even for non-web tests.
"""
try:
import uvicorn
from uipath.dev.server import UiPathDeveloperServer
except ImportError:
pytest.skip("server dependencies not installed (pip install uipath-dev)")
factory = MockRuntimeFactory()
trace_mgr = UiPathTraceManager()
port = _find_free_port()
server_obj = UiPathDeveloperServer(
runtime_factory=factory,
trace_manager=trace_mgr,
host="127.0.0.1",
port=port,
open_browser=False,
)
fastapi_app = server_obj.create_app()
config = uvicorn.Config(
fastapi_app,
host="127.0.0.1",
port=port,
log_level="warning",
)
uv_server = uvicorn.Server(config)
thread = threading.Thread(target=uv_server.run, daemon=True)
thread.start()
# Wait for server to be ready
url = f"http://127.0.0.1:{port}"
for _ in range(50):
try:
with socket.create_connection(("127.0.0.1", port), timeout=0.2):
break
except OSError:
time.sleep(0.1)
else:
raise RuntimeError("Server did not start in time")
yield url
uv_server.should_exit = True
thread.join(timeout=5)