chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:47:58 +08:00
commit b16403ea71
789 changed files with 115226 additions and 0 deletions
@@ -0,0 +1,18 @@
import pytest
from e2b import NotFoundException, AsyncSandbox
async def test_connect_to_process(async_sandbox: AsyncSandbox):
cmd = await async_sandbox.commands.run("sleep 10", background=True)
pid = cmd.pid
process_info = await async_sandbox.commands.connect(pid)
assert process_info.pid == pid
async def test_connect_to_non_existing_process(async_sandbox: AsyncSandbox):
non_existing_pid = 999999
with pytest.raises(NotFoundException):
await async_sandbox.commands.connect(non_existing_pid)
@@ -0,0 +1,19 @@
import pytest
from e2b import AsyncSandbox, CommandExitException
async def test_kill_process(async_sandbox: AsyncSandbox):
cmd = await async_sandbox.commands.run("sleep 10", background=True)
pid = cmd.pid
await async_sandbox.commands.kill(pid)
with pytest.raises(CommandExitException):
await async_sandbox.commands.run(f"kill -0 {pid}")
async def test_kill_non_existing_process(async_sandbox: AsyncSandbox):
non_existing_pid = 999999
assert not await async_sandbox.commands.kill(non_existing_pid)
@@ -0,0 +1,13 @@
from e2b import AsyncSandbox
async def test_kill_process(async_sandbox: AsyncSandbox):
c1 = await async_sandbox.commands.run("sleep 10", background=True)
c2 = await async_sandbox.commands.run("sleep 10", background=True)
processes = await async_sandbox.commands.list()
assert len(processes) >= 2
pids = [p.pid for p in processes]
assert c1.pid in pids
assert c2.pid in pids
@@ -0,0 +1,35 @@
import pytest
from e2b import AsyncSandbox
async def test_command_envs(async_sandbox: AsyncSandbox):
cmd = await async_sandbox.commands.run("echo $FOO", envs={"FOO": "bar"})
assert cmd.stdout.strip() == "bar"
@pytest.mark.skip_debug()
async def test_sandbox_envs(async_sandbox_factory):
sbx = await async_sandbox_factory(envs={"FOO": "bar"})
cmd = await sbx.commands.run("echo $FOO")
assert cmd.stdout.strip() == "bar"
async def test_bash_command_scoped_env_vars(async_sandbox: AsyncSandbox):
cmd = await async_sandbox.commands.run("echo $FOO", envs={"FOO": "bar"})
assert cmd.exit_code == 0
assert cmd.stdout.strip() == "bar"
# test that it is secure and not accessible to subsequent commands
cmd2 = await async_sandbox.commands.run('sudo echo "$FOO"')
assert cmd2.exit_code == 0
assert cmd2.stdout.strip() == ""
async def test_python_command_scoped_env_vars(async_sandbox: AsyncSandbox):
cmd = await async_sandbox.commands.run(
"python3 -c \"import os; print(os.environ['FOO'])\"", envs={"FOO": "bar"}
)
assert cmd.exit_code == 0
assert cmd.stdout.strip() == "bar"
@@ -0,0 +1,53 @@
import pytest
from e2b import AsyncSandbox, TimeoutException
async def test_run(async_sandbox: AsyncSandbox):
text = "Hello, World!"
cmd = await async_sandbox.commands.run(f'echo "{text}"')
assert cmd.exit_code == 0
assert cmd.stdout == f"{text}\n"
async def test_run_with_special_characters(async_sandbox: AsyncSandbox):
text = "!@#$%^&*()_+"
cmd = await async_sandbox.commands.run(f'echo "{text}"')
assert cmd.exit_code == 0
# assert cmd.stdout == f"{text}\n"
async def test_run_with_broken_utf8(async_sandbox: AsyncSandbox):
# Create a string with 8191 'a' characters followed by the problematic byte 0xe2
long_str = "a" * 8191 + "\\xe2"
result = await async_sandbox.commands.run(f'printf "{long_str}"')
assert result.exit_code == 0
# The broken UTF-8 bytes should be replaced with the Unicode replacement character
assert result.stdout == ("a" * 8191 + "\ufffd")
async def test_run_with_multiline_string(async_sandbox: AsyncSandbox):
text = "Hello,\nWorld!"
cmd = await async_sandbox.commands.run(f'echo "{text}"')
assert cmd.exit_code == 0
assert cmd.stdout == f"{text}\n"
async def test_run_with_timeout(async_sandbox: AsyncSandbox):
cmd = await async_sandbox.commands.run('echo "Hello, World!"', timeout=10)
assert cmd.exit_code == 0
async def test_run_with_too_short_timeout(async_sandbox: AsyncSandbox):
with pytest.raises(TimeoutException):
await async_sandbox.commands.run("sleep 10", timeout=2)
@@ -0,0 +1,16 @@
import pytest
from e2b import AsyncSandbox, TimeoutException
@pytest.mark.skip_debug()
async def test_kill_sandbox_while_command_is_running(async_sandbox: AsyncSandbox):
cmd = await async_sandbox.commands.run("sleep 60", background=True)
await async_sandbox.kill()
with pytest.raises(TimeoutException) as exc_info:
await cmd.wait()
# The health check confirms the sandbox is gone, so the error states it outright
assert "sandbox was killed or reached its end of life" in str(exc_info.value)
@@ -0,0 +1,94 @@
import asyncio
from e2b import AsyncSandbox
async def test_send_stdin_to_process(async_sandbox: AsyncSandbox):
ev = asyncio.Event()
def handle_event(stdout: str):
ev.set()
cmd = await async_sandbox.commands.run(
"cat", background=True, on_stdout=handle_event, stdin=True
)
await async_sandbox.commands.send_stdin(cmd.pid, "Hello, World!")
await ev.wait()
assert cmd.stdout == "Hello, World!"
async def test_send_bytes_stdin_to_process(async_sandbox: AsyncSandbox):
ev = asyncio.Event()
def handle_event(stdout: str):
ev.set()
cmd = await async_sandbox.commands.run(
"cat", background=True, on_stdout=handle_event, stdin=True
)
await async_sandbox.commands.send_stdin(cmd.pid, b"Hello, World!")
await ev.wait()
assert cmd.stdout == "Hello, World!"
async def test_send_stdin_via_command_handle(async_sandbox: AsyncSandbox):
ev = asyncio.Event()
def handle_event(stdout: str):
ev.set()
cmd = await async_sandbox.commands.run(
"cat", background=True, on_stdout=handle_event, stdin=True
)
await cmd.send_stdin("Hello, World!")
await ev.wait()
assert cmd.stdout == "Hello, World!"
async def test_close_stdin_via_command_handle(async_sandbox: AsyncSandbox):
cmd = await async_sandbox.commands.run("cat", background=True, stdin=True)
await cmd.send_stdin("Hello, World!")
await cmd.close_stdin()
# `cat` exits once stdin is closed (EOF).
result = await cmd.wait()
assert result.exit_code == 0
assert result.stdout == "Hello, World!"
async def test_send_special_characters_to_process(async_sandbox: AsyncSandbox):
ev = asyncio.Event()
def handle_event(stdout: str):
ev.set()
cmd = await async_sandbox.commands.run(
"cat", background=True, on_stdout=handle_event, stdin=True
)
await async_sandbox.commands.send_stdin(cmd.pid, "!@#$%^&*()_+")
await ev.wait()
assert cmd.stdout == "!@#$%^&*()_+"
async def test_send_multiline_string_to_process(async_sandbox: AsyncSandbox):
ev = asyncio.Event()
def handle_event(stdout: str):
ev.set()
cmd = await async_sandbox.commands.run(
"cat", background=True, on_stdout=handle_event, stdin=True
)
await async_sandbox.commands.send_stdin(cmd.pid, "Hello,\nWorld!")
await ev.wait()
assert cmd.stdout == "Hello,\nWorld!"