-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from e2b-dev/change-js-kernel
Add Deno kernel
- Loading branch information
Showing
11 changed files
with
283 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@e2b/code-interpreter-template': patch | ||
--- | ||
|
||
Add [Deno kernel](https://docs.deno.com/runtime/reference/cli/jupyter/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { expect } from 'vitest' | ||
|
||
import { sandboxTest } from '../setup' | ||
|
||
sandboxTest('js simple', async ({ sandbox }) => { | ||
const result = await sandbox.runCode('console.log("Hello, World!")', {language: "deno"}) | ||
|
||
expect(result.logs.stdout.join().trim()).toEqual('Hello, World!') | ||
}) | ||
|
||
sandboxTest('js import', async ({ sandbox }) => { | ||
const result = await sandbox.runCode('import isOdd from "npm:is-odd"\nisOdd(3)', {language: "deno"}) | ||
|
||
expect(result.results[0].text).toEqual('true') | ||
}) | ||
|
||
sandboxTest('js top level await', async ({ sandbox }) => { | ||
const result = await sandbox.runCode(` | ||
async function main() { | ||
return 'Hello, World!' | ||
} | ||
await main() | ||
`, {language: "deno"}) | ||
expect(result.results[0].text).toEqual('Hello, World!') | ||
}) | ||
|
||
sandboxTest('js es6', async ({ sandbox }) => { | ||
const result = await sandbox.runCode(` | ||
const add = (x, y) => x + y; | ||
add(1, 2)`, {language: "deno"}) | ||
expect(result.results[0].text).toEqual('3') | ||
}) | ||
|
||
|
||
sandboxTest('js context', async ({ sandbox }) => { | ||
await sandbox.runCode('const z = 1', {language: "deno"}) | ||
const result = await sandbox.runCode('z', {language: "deno"}) | ||
expect(result.results[0].text).toEqual('1') | ||
}) | ||
|
||
sandboxTest('js cwd', async ({ sandbox }) => { | ||
const result = await sandbox.runCode('process.cwd()', {language: "deno"}) | ||
expect(result.results[0].text).toEqual('/home/user') | ||
|
||
const ctx = await sandbox.createCodeContext( {cwd: '/home', language: "deno"}) | ||
const result2 = await sandbox.runCode('process.cwd()', {context: ctx}) | ||
expect(result2.results[0].text).toEqual('/home') | ||
}) | ||
|
||
sandboxTest('ts simple', async ({ sandbox }) => { | ||
const result = await sandbox.runCode(` | ||
function subtract(x: number, y: number): number { | ||
return x - y; | ||
} | ||
subtract(1, 2) | ||
`, {language: "deno"}) | ||
|
||
expect(result.results[0].text).toEqual('-1') | ||
}) | ||
|
||
sandboxTest('test display', async ({ sandbox }) => { | ||
const result = await sandbox.runCode(` | ||
{ | ||
[Symbol.for("Jupyter.display")]() { | ||
return { | ||
// Plain text content | ||
"text/plain": "Hello world!", | ||
// HTML output | ||
"text/html": "<h1>Hello world!</h1>", | ||
} | ||
} | ||
} | ||
`, {language: "deno"}) | ||
|
||
expect(result.results[0].html).toBe('<h1>Hello world!</h1>') | ||
expect(result.results[0].text).toBe('Hello world!') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from e2b_code_interpreter import AsyncSandbox | ||
|
||
|
||
async def test_javascript(async_sandbox: AsyncSandbox): | ||
code = """ | ||
console.log('Hello, World!') | ||
""" | ||
execution = await async_sandbox.run_code(code, language="deno") | ||
assert execution.logs.stdout == ["Hello, World!\n"] | ||
|
||
|
||
async def test_import(async_sandbox: AsyncSandbox): | ||
code = """ | ||
import isOdd from 'npm:is-odd' | ||
isOdd(3) | ||
""" | ||
execution = await async_sandbox.run_code(code, language="deno") | ||
assert execution.results[0].text == "true" | ||
|
||
|
||
async def test_toplevel_await(async_sandbox: AsyncSandbox): | ||
code = """ | ||
async function main() { | ||
return 'Hello, World!' | ||
} | ||
await main() | ||
""" | ||
execution = await async_sandbox.run_code(code, language="deno") | ||
assert execution.results[0].text == "Hello, World!" | ||
|
||
|
||
async def test_es6(async_sandbox: AsyncSandbox): | ||
code = """ | ||
const add = (x, y) => x + y; | ||
add(1, 2); | ||
""" | ||
execution = await async_sandbox.run_code(code, language="deno") | ||
assert execution.results[0].text == "3" | ||
|
||
|
||
async def test_context(async_sandbox: AsyncSandbox): | ||
await async_sandbox.run_code("const x = 1", language="deno") | ||
execution = await async_sandbox.run_code("x", language="deno") | ||
assert execution.results[0].text == "1" | ||
|
||
|
||
async def test_cwd(async_sandbox: AsyncSandbox): | ||
execution = await async_sandbox.run_code("process.cwd()", language="deno") | ||
assert execution.results[0].text == "/home/user" | ||
|
||
ctx = await async_sandbox.create_code_context("/home", language="deno") | ||
execution = await async_sandbox.run_code("process.cwd()", context=ctx) | ||
assert execution.results[0].text == "/home" | ||
|
||
|
||
async def test_typescript(async_sandbox: AsyncSandbox): | ||
execution = await async_sandbox.run_code( | ||
""" | ||
function subtract(x: number, y: number): number { | ||
return x - y; | ||
} | ||
subtract(1, 2); | ||
""", | ||
language="deno", | ||
) | ||
assert execution.results[0].text == "-1" | ||
|
||
|
||
async def test_display(async_sandbox: AsyncSandbox): | ||
code = """ | ||
{ | ||
[Symbol.for("Jupyter.display")]() { | ||
return { | ||
// Plain text content | ||
"text/plain": "Hello world!", | ||
// HTML output | ||
"text/html": "<h1>Hello world!</h1>", | ||
} | ||
} | ||
} | ||
""" | ||
execution = await async_sandbox.run_code(code, language="deno") | ||
assert execution.results[0].text == "Hello world!" | ||
assert execution.results[0].html == "<h1>Hello world!</h1>" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,20 @@ | ||
import pytest | ||
|
||
from e2b_code_interpreter.code_interpreter_sync import Sandbox | ||
|
||
|
||
def test_js_kernel(sandbox: Sandbox): | ||
execution = sandbox.run_code("console.log('Hello, World!')", language="js") | ||
assert execution.logs.stdout == ["Hello, World!\n"] | ||
|
||
|
||
@pytest.mark.skip_debug() | ||
def test_r_kernel(sandbox: Sandbox): | ||
execution = sandbox.run_code('print("Hello, World!")', language="r") | ||
assert execution.logs.stdout == ['[1] "Hello, World!"\n'] | ||
|
||
|
||
@pytest.mark.skip_debug() | ||
def test_java_kernel(sandbox: Sandbox): | ||
execution = sandbox.run_code('System.out.println("Hello, World!")', language="java") | ||
assert execution.logs.stdout[0] == "Hello, World!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"argv": [ | ||
"/usr/bin/deno", | ||
"jupyter", | ||
"--kernel", | ||
"--conn", | ||
"{connection_file}" | ||
], | ||
"display_name": "Deno", | ||
"env": { | ||
"NO_COLOR": "1" | ||
}, | ||
"language": "typescript" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.