-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathhelper.py
More file actions
71 lines (58 loc) · 1.79 KB
/
Copy pathhelper.py
File metadata and controls
71 lines (58 loc) · 1.79 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
"""
Test helper functions for E2E tests.
"""
import asyncio
import inspect
import os
import time
from collections.abc import Awaitable, Callable
from _session_test_helpers import get_next_event_of_type as get_next_event_of_type
from _session_test_helpers import wait_for_event as wait_for_event
def write_file(work_dir: str, filename: str, content: str) -> str:
"""
Write content to a file in the work directory.
Args:
work_dir: The working directory
filename: The name of the file
content: The content to write
Returns:
The full path to the created file
"""
filepath = os.path.join(work_dir, filename)
with open(filepath, "w") as f:
f.write(content)
return filepath
def read_file(work_dir: str, filename: str) -> str:
"""
Read content from a file in the work directory.
Args:
work_dir: The working directory
filename: The name of the file
Returns:
The content of the file
"""
filepath = os.path.join(work_dir, filename)
with open(filepath) as f:
return f.read()
async def wait_for_condition(
condition: Callable[[], bool | Awaitable[bool]],
*,
timeout: float = 120.0,
poll_interval: float = 0.1,
timeout_message: str = "Timed out waiting for condition.",
) -> None:
"""Poll until condition returns true, with timeout only as a failsafe."""
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
result = condition()
if inspect.isawaitable(result):
result = await result
if result:
return
await asyncio.sleep(poll_interval)
result = condition()
if inspect.isawaitable(result):
result = await result
if result:
return
raise TimeoutError(timeout_message)