-
Notifications
You must be signed in to change notification settings - Fork 937
Expand file tree
/
Copy pathconftest.py
More file actions
101 lines (77 loc) · 2.23 KB
/
Copy pathconftest.py
File metadata and controls
101 lines (77 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import asyncio
import pytest
import pytest_asyncio
import os
from logging import warning
from e2b import (
Sandbox,
AsyncSandbox,
AsyncCommandHandle,
CommandExitException,
CommandHandle,
)
@pytest.fixture()
def template():
return "base"
@pytest.fixture()
def sandbox(template, debug):
sandbox = Sandbox(template, debug=debug)
try:
yield sandbox
finally:
try:
sandbox.kill()
except (Exception, RuntimeError):
if not debug:
warning(
"Failed to kill sandbox — this is expected if the test runs with local envd."
)
@pytest_asyncio.fixture
async def async_sandbox(template, debug):
sandbox = await AsyncSandbox.create(template, debug=debug)
try:
yield sandbox
finally:
try:
await sandbox.kill()
except (Exception, RuntimeError):
if not debug:
warning(
"Failed to kill sandbox — this is expected if the test runs with local envd."
)
@pytest.fixture
def debug():
return os.getenv("E2B_DEBUG") is not None
@pytest.fixture(autouse=True)
def skip_by_debug(request, debug):
if request.node.get_closest_marker("skip_debug"):
if debug:
pytest.skip("skipped because E2B_DEBUG is set")
class Helpers:
@staticmethod
def catch_cmd_exit_error_in_background(cmd: AsyncCommandHandle):
disabled = False
async def wait_for_exit():
try:
await cmd.wait()
except CommandExitException as e:
if not disabled:
assert (
False
), f"command failed with exit code {e.exit_code}: {e.stderr}"
asyncio.create_task(wait_for_exit())
def disable():
nonlocal disabled
disabled = True
return disable
@staticmethod
def check_cmd_exit_error(cmd: CommandHandle):
try:
cmd.wait()
except CommandExitException as e:
assert False, f"command failed with exit code {e.exit_code}: {e.stderr}"
except Exception as e:
raise e
@pytest.fixture
def helpers():
return Helpers