forked from e2b-dev/code-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
95 lines (63 loc) · 2.15 KB
/
conftest.py
File metadata and controls
95 lines (63 loc) · 2.15 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
import asyncio
import os
import pytest
from e2b_code_interpreter import (
AsyncSandbox,
Sandbox,
)
import uuid
@pytest.fixture(scope="session")
def sandbox_test_id():
return f"test_{uuid.uuid4()}"
@pytest.fixture()
def template():
return os.getenv("E2B_TESTS_TEMPLATE") or "code-interpreter-v1"
@pytest.fixture()
def sandbox_factory(request, template, sandbox_test_id):
def factory(*, template_name: str = template, **kwargs):
kwargs.setdefault("secure", False)
kwargs.setdefault("timeout", 60)
metadata = kwargs.setdefault("metadata", dict())
metadata.setdefault("sandbox_test_id", sandbox_test_id)
sandbox = Sandbox.create(template_name, **kwargs)
request.addfinalizer(lambda: sandbox.kill())
return sandbox
return factory
@pytest.fixture()
def sandbox(sandbox_factory):
return sandbox_factory()
# override the event loop so it never closes
# this helps us with the global-scoped async http transport
@pytest.fixture(scope="session")
def event_loop():
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
yield loop
loop.close()
@pytest.fixture
def async_sandbox_factory(request, template, sandbox_test_id, event_loop):
async def factory(*, template_name: str = template, **kwargs):
kwargs.setdefault("timeout", 60)
metadata = kwargs.setdefault("metadata", dict())
metadata.setdefault("sandbox_test_id", sandbox_test_id)
sandbox = await AsyncSandbox.create(template_name, **kwargs)
def kill():
async def _kill():
await sandbox.kill()
event_loop.run_until_complete(_kill())
request.addfinalizer(kill)
return sandbox
return factory
@pytest.fixture
async def async_sandbox(async_sandbox_factory):
return await async_sandbox_factory()
@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")