forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
115 lines (93 loc) · 3.24 KB
/
conftest.py
File metadata and controls
115 lines (93 loc) · 3.24 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import atexit
import os
import socket
import subprocess
import sys
import time
import pytest
PORT = 8080
HTTP_SCRIPT = f"""
import mimetypes
mimetypes.add_type("application/wasm", ".wasm")
import http.server
http.server.test(HandlerClass=http.server.SimpleHTTPRequestHandler, port={PORT})
"""
demo_dist = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../demo/dist/")
server_proc = None
def pytest_sessionstart(session):
global server_proc
server_proc = subprocess.Popen(
["python3", "-c", HTTP_SCRIPT],
cwd=demo_dist,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
wait_for_port(PORT)
def pytest_sessionfinish(session):
global server_proc
server_proc.terminate()
server_proc = None
atexit.register(lambda: server_proc and server_proc.terminate())
# From https://gist.github.com/butla/2d9a4c0f35ea47b7452156c96a4e7b12
def wait_for_port(port, host="localhost", timeout=5.0):
"""Wait until a port starts accepting TCP connections.
Args:
port (int): Port number.
host (str): Host address on which the port should exist.
timeout (float): In seconds. How long to wait before raising errors.
Raises:
TimeoutError: The port isn't accepting connection after time specified in `timeout`.
"""
start_time = time.perf_counter()
while True:
try:
with socket.create_connection((host, port), timeout=timeout):
break
except OSError as ex:
time.sleep(0.01)
if time.perf_counter() - start_time >= timeout:
raise TimeoutError(
"Waited too long for the port {} on host {} to start accepting "
"connections.".format(port, host)
) from ex
from selenium import webdriver
from selenium.common.exceptions import JavascriptException
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
class Driver(webdriver.Firefox):
def _print_panic(self):
stack = self.execute_script(
"return (window.__RUSTPYTHON_ERROR_MSG || '') + '\\n' + (window.__RUSTPYTHON_ERROR_STACK || '')"
)
if stack.strip():
print(f"RustPython panic stack:", stack, file=sys.stderr, sep="\n")
def execute_script(self, *args, **kwargs):
try:
return super().execute_script(*args, **kwargs)
except JavascriptException:
self._print_panic()
raise
@pytest.fixture
def wdriver(request):
options = Options()
options.add_argument("-headless")
driver = Driver(options=options)
try:
driver.get(f"http://localhost:{PORT}")
WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.ID, "rp_loaded"))
)
except JavascriptException:
driver._print_panic()
driver.quit()
raise
except Exception as e:
print(f"Error waiting for page to load: {e}")
# Check the page source to see what's loaded
print("Page source:", driver.page_source[:500])
driver.quit()
raise
yield driver
driver.quit()