forked from python-eel/Eel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
66 lines (48 loc) · 2.09 KB
/
Copy pathutils.py
File metadata and controls
66 lines (48 loc) · 2.09 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
import contextlib
import os
import subprocess
import tempfile
import time
from pathlib import Path
import psutil
# Path to the test data folder.
TEST_DATA_DIR = Path(__file__).parent / 'data'
def get_process_listening_port(proc):
psutil_proc = psutil.Process(proc.pid)
while not any(conn.status == 'LISTEN' for conn in psutil_proc.connections()):
time.sleep(0.01)
conn = next(filter(lambda conn: conn.status == 'LISTEN', psutil_proc.connections()))
return conn.laddr.port
@contextlib.contextmanager
def get_eel_server(example_py, start_html):
"""Run an Eel example with the mode/port overridden so that no browser is launched and a random port is assigned"""
test = None
try:
with tempfile.NamedTemporaryFile(mode='w', dir=os.path.dirname(example_py), delete=False) as test:
# We want to run the examples unmodified to keep the test as realistic as possible, but all of the examples
# want to launch browsers, which won't be supported in CI. The below script will configure eel to open on
# a random port and not open a browser, before importing the Python example file - which will then
# do the rest of the set up and start the eel server. This is definitely hacky, and means we can't
# test mode/port settings for examples ... but this is OK for now.
test.write(f"""
import eel
eel._start_args['mode'] = None
eel._start_args['port'] = 0
import {os.path.splitext(os.path.basename(example_py))[0]}
""")
proc = subprocess.Popen(['python', test.name], cwd=os.path.dirname(example_py))
eel_port = get_process_listening_port(proc)
yield f"http://localhost:{eel_port}/{start_html}"
proc.terminate()
finally:
if test:
try:
os.unlink(test.name)
except FileNotFoundError:
pass
def get_console_logs(driver, minimum_logs=0):
console_logs = driver.get_log('browser')
while len(console_logs) < minimum_logs:
console_logs += driver.get_log('browser')
time.sleep(0.1)
return console_logs