Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add tests

Vasek Mlejnskyae2c6ef60b7f
diff --git a/packages/js-sdk/src/runCode.ts b/packages/js-sdk/src/runCode.ts
index d78e1371c..c38f67e09 100644
--- a/packages/js-sdk/src/runCode.ts
+++ b/packages/js-sdk/src/runCode.ts
@@ -5,13 +5,8 @@ export enum CodeRuntime {
Python3 = 'Python3',
}
-// Fucntions that sleeps for a given amount of time
-function sleep(ms: number) {
- return new Promise(resolve => setTimeout(resolve, ms))
-}
-
/**
- * Run code in a sandboxed cloud environment.
+ * Run code in a sandboxed cloud playground.
* `runCode` wraps the `Session` class and provides a simple interface for running code in a sandboxed environment
* without any need to manage lifecycle of the session.
* `runCode` automatically loads the E2B API key from the `E2B_API_KEY` environment variable.
@@ -46,7 +41,7 @@ export async function runCode(
break
default:
throw new Error(
- `The "${runtime}" runtime isn't supported. Please contact us know if you need support for this runtime.`,
+ `The "${runtime}" runtime isn't supported. Please contact us ([email protected]) if you need support for this runtime`,
)
}
diff --git a/packages/python-sdk/e2b/session/run_code.py b/packages/python-sdk/e2b/session/run_code.py
index 4f2d30dc2..16c2f51b3 100644
--- a/packages/python-sdk/e2b/session/run_code.py
+++ b/packages/python-sdk/e2b/session/run_code.py
@@ -1,4 +1,4 @@
-from typing import Literal, Union
+from typing import Literal, Union, Optional
from os import getenv
from . import Session
@@ -11,7 +11,19 @@ CodeRuntime = Literal[
async def run_code(
runtime: Union[CodeRuntime, str],
code: str,
+ api_key: Optional[str] = getenv("E2B_API_KEY"),
):
+ """
+ Runs code in a sandboxed cloud playground and return the stdout and stderr
+ `run_ode` wraps the `Session` class and provides a simple interface for running code in a sandboxed environment
+ without any need to manage lifecycle of the session.
+ `run_code` automatically loads the E2B API key from the `E2B_API_KEY` environment variable.
+
+ :param runtime: The runtime to run the code in. One of "Node16" or "Python3".
+ :param code: The code to run
+
+ :return: A string touple of stdout and stderr
+ """
binary = ""
filepath = ""
env_id = ""
@@ -28,7 +40,7 @@ async def run_code(
session = await Session.create(
id=env_id,
- api_key=getenv("E2B_API_KEY"),
+ api_key=api_key,
)
await session.filesystem.write(filepath, code)
diff --git a/packages/python-sdk/tests/test_run_code.py b/packages/python-sdk/tests/test_run_code.py
index 3ea458431..984876520 100644
--- a/packages/python-sdk/tests/test_run_code.py
+++ b/packages/python-sdk/tests/test_run_code.py
@@ -1,3 +1,4 @@
+import pytest
from os import getenv
from e2b import run_code
@@ -10,3 +11,10 @@ async def test_run_code():
assert stdout == "hello"
assert "Error: error" in stderr
+
+
+async def test_unsupported_runtime():
+ code = "console.log('hello'); throw new Error('error')"
+ with pytest.raises(Exception) as e:
+ await run_code("unsupported", code)
+