Skip to content

Commit 6be8349

Browse files
committed
added tests for languages other than python
1 parent c6dd986 commit 6be8349

File tree

3 files changed

+280
-93
lines changed

3 files changed

+280
-93
lines changed

js/tests/envVars.test.ts

Lines changed: 92 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { expect } from 'vitest'
33
import { isDebug, sandboxTest } from './setup'
44
import { Sandbox } from '../src'
55

6-
// Skip this test if we are running in debug mode — the pwd and user in the testing docker container are not the same as in the actual sandbox.
7-
sandboxTest.skipIf(isDebug)('env vars', async () => {
6+
// Python tests
7+
sandboxTest.skipIf(isDebug)('env vars (python)', async () => {
88
const sandbox = await Sandbox.create({
99
envs: { TEST_ENV_VAR: 'supertest' },
1010
})
@@ -14,52 +14,121 @@ sandboxTest.skipIf(isDebug)('env vars', async () => {
1414
`import os; x = os.getenv('TEST_ENV_VAR'); x`
1515
)
1616

17-
expect(result.results[0].text.trim()).toEqual('supertest')
17+
expect(result.results[0]?.text.trim()).toEqual('supertest')
1818
} finally {
1919
await sandbox.kill()
2020
}
2121
})
2222

23-
sandboxTest('env vars on sandbox', async ({ sandbox }) => {
23+
sandboxTest('env vars on sandbox (python)', async ({ sandbox }) => {
2424
const result = await sandbox.runCode(
2525
"import os; os.getenv('FOO')",
2626
{ envs: { FOO: 'bar' } }
2727
)
2828

29-
expect(result.results[0].text.trim()).toEqual('bar')
29+
expect(result.results[0]?.text.trim()).toEqual('bar')
3030
})
3131

32-
sandboxTest('env vars on sandbox override', async () => {
32+
// JavaScript tests
33+
sandboxTest.skipIf(isDebug)('env vars (javascript)', async () => {
3334
const sandbox = await Sandbox.create({
34-
envs: { FOO: 'bar', SBX: 'value' },
35+
envs: { TEST_ENV_VAR: 'supertest' },
3536
})
3637

3738
try {
38-
await sandbox.runCode(
39-
"import os; os.environ['FOO'] = 'bar'; os.environ['RUNTIME_ENV'] = 'js_runtime'"
39+
const result = await sandbox.runCode(
40+
`process.env.TEST_ENV_VAR`
4041
)
42+
43+
expect(result.results[0]?.text.trim()).toEqual('supertest')
44+
} finally {
45+
await sandbox.kill()
46+
}
47+
})
48+
49+
sandboxTest('env vars on sandbox (javascript)', async ({ sandbox }) => {
50+
const result = await sandbox.runCode(
51+
`process.env.FOO`,
52+
{ envs: { FOO: 'bar' } }
53+
)
54+
55+
expect(result.results[0]?.text.trim()).toEqual('bar')
56+
})
57+
58+
// R tests
59+
sandboxTest.skipIf(isDebug)('env vars (r)', async () => {
60+
const sandbox = await Sandbox.create({
61+
envs: { TEST_ENV_VAR: 'supertest' },
62+
})
63+
64+
try {
4165
const result = await sandbox.runCode(
42-
"import os; os.getenv('FOO')",
43-
{ envs: { FOO: 'baz' } }
66+
`Sys.getenv("TEST_ENV_VAR")`
4467
)
4568

46-
expect(result.results[0].text.trim()).toEqual('baz')
69+
expect(result.results[0]?.text.trim()).toEqual('supertest')
70+
} finally {
71+
await sandbox.kill()
72+
}
73+
})
4774

48-
const result2 = await sandbox.runCode(
49-
"import os; os.getenv('RUNTIME_ENV')"
75+
sandboxTest('env vars on sandbox (r)', async ({ sandbox }) => {
76+
const result = await sandbox.runCode(
77+
`Sys.getenv("FOO")`,
78+
{ envs: { FOO: 'bar' } }
79+
)
80+
81+
expect(result.results[0]?.text.trim()).toEqual('bar')
82+
})
83+
84+
// Java tests
85+
sandboxTest.skipIf(isDebug)('env vars (java)', async () => {
86+
const sandbox = await Sandbox.create({
87+
envs: { TEST_ENV_VAR: 'supertest' },
88+
})
89+
90+
try {
91+
const result = await sandbox.runCode(
92+
`System.getenv("TEST_ENV_VAR")`
5093
)
51-
expect(result2.results[0].text.trim()).toEqual('js_runtime')
5294

53-
if (!isDebug) {
54-
const result3 = await sandbox.runCode(
55-
"import os; os.getenv('SBX')"
56-
)
57-
expect(result3.results[0].text.trim()).toEqual('value')
58-
}
95+
expect(result.results[0]?.text.trim()).toEqual('supertest')
96+
} finally {
97+
await sandbox.kill()
98+
}
99+
})
100+
101+
sandboxTest('env vars on sandbox (java)', async ({ sandbox }) => {
102+
const result = await sandbox.runCode(
103+
`System.getenv("FOO")`,
104+
{ envs: { FOO: 'bar' } }
105+
)
106+
107+
expect(result.results[0]?.text.trim()).toEqual('bar')
108+
})
59109

60-
const result4 = await sandbox.runCode("import os; os.getenv('FOO')")
61-
expect(result4.results[0].text.trim()).toEqual('bar')
110+
// Bash tests
111+
sandboxTest.skipIf(isDebug)('env vars (bash)', async () => {
112+
const sandbox = await Sandbox.create({
113+
envs: { TEST_ENV_VAR: 'supertest' },
114+
})
115+
116+
try {
117+
const result = await sandbox.runCode(
118+
`echo $TEST_ENV_VAR`
119+
)
120+
121+
expect(result.results[0]?.text.trim()).toEqual('supertest')
62122
} finally {
63123
await sandbox.kill()
64124
}
65125
})
126+
127+
sandboxTest('env vars on sandbox (bash)', async ({ sandbox }) => {
128+
const result = await sandbox.runCode(
129+
`echo $FOO`,
130+
{ envs: { FOO: 'bar' } }
131+
)
132+
133+
expect(result.results[0]?.text.trim()).toEqual('bar')
134+
})

python/tests/async/test_async_env_vars.py

Lines changed: 91 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,100 @@
33
from e2b_code_interpreter.code_interpreter_async import AsyncSandbox
44

55

6-
# @pytest.mark.skip_debug()
7-
# async def test_env_vars_sandbox():
8-
# sbx = await AsyncSandbox.create(envs={"FOO": "bar"})
9-
# try:
10-
# result = await sbx.run_code("import os; os.getenv('FOO')")
11-
# assert result.text == "bar"
12-
# finally:
13-
# await sbx.kill()
6+
@pytest.mark.skip_debug()
7+
async def test_env_vars_sandbox():
8+
sbx = await AsyncSandbox.create(envs={"TEST_ENV_VAR": "supertest"})
9+
try:
10+
result = await sbx.run_code("import os; os.getenv('TEST_ENV_VAR')")
11+
assert result.text is not None
12+
assert result.text.strip() == "supertest"
13+
finally:
14+
await sbx.kill()
1415

1516

1617
async def test_env_vars_in_run_code(async_sandbox: AsyncSandbox):
1718
result = await async_sandbox.run_code(
1819
"import os; os.getenv('FOO')", envs={"FOO": "bar"}
1920
)
20-
assert result.text == "bar"
21-
22-
23-
#
24-
# async def test_env_vars_override(debug: bool):
25-
# sbx = await AsyncSandbox.create(envs={"FOO": "bar", "SBX": "value"})
26-
#
27-
# try:
28-
# await sbx.run_code(
29-
# "import os; os.environ['FOO'] = 'bar'; os.environ['RUNTIME_ENV'] = 'async_python_runtime'"
30-
# )
31-
# result = await sbx.run_code("import os; os.getenv('FOO')", envs={"FOO": "baz"})
32-
# assert result.text == "baz"
33-
#
34-
# # This can fail if running in debug mode (there's a race condition with the restart kernel test)
35-
# result = await sbx.run_code("import os; os.getenv('RUNTIME_ENV')")
36-
# assert result.text == "async_python_runtime"
37-
#
38-
# if not debug:
39-
# result = await sbx.run_code("import os; os.getenv('SBX')")
40-
# assert result.text == "value"
41-
#
42-
# # This can fail if running in debug mode (there's a race condition with the restart kernel test)
43-
# result = await sbx.run_code("import os; os.getenv('FOO')")
44-
# assert result.text == "bar"
45-
# finally:
46-
# await sbx.kill()
21+
assert result.text is not None
22+
assert result.text.strip() == "bar"
23+
24+
25+
# JavaScript tests
26+
@pytest.mark.skip_debug()
27+
async def test_env_vars_javascript_sandbox():
28+
sbx = await AsyncSandbox.create(envs={"TEST_ENV_VAR": "supertest"})
29+
try:
30+
result = await sbx.run_code("process.env.TEST_ENV_VAR")
31+
assert result.text is not None
32+
assert result.text.strip() == "supertest"
33+
finally:
34+
await sbx.kill()
35+
36+
37+
async def test_env_vars_javascript(async_sandbox: AsyncSandbox):
38+
result = await async_sandbox.run_code(
39+
"process.env.FOO", envs={"FOO": "bar"}
40+
)
41+
assert result.text is not None
42+
assert result.text.strip() == "bar"
43+
44+
45+
# R tests
46+
@pytest.mark.skip_debug()
47+
async def test_env_vars_r_sandbox():
48+
sbx = await AsyncSandbox.create(envs={"TEST_ENV_VAR": "supertest"})
49+
try:
50+
result = await sbx.run_code('Sys.getenv("TEST_ENV_VAR")')
51+
assert result.text is not None
52+
assert result.text.strip() == "supertest"
53+
finally:
54+
await sbx.kill()
55+
56+
57+
async def test_env_vars_r(async_sandbox: AsyncSandbox):
58+
result = await async_sandbox.run_code(
59+
'Sys.getenv("FOO")', envs={"FOO": "bar"}
60+
)
61+
assert result.text is not None
62+
assert result.text.strip() == "bar"
63+
64+
65+
# Java tests
66+
@pytest.mark.skip_debug()
67+
async def test_env_vars_java_sandbox():
68+
sbx = await AsyncSandbox.create(envs={"TEST_ENV_VAR": "supertest"})
69+
try:
70+
result = await sbx.run_code('System.getenv("TEST_ENV_VAR")')
71+
assert result.text is not None
72+
assert result.text.strip() == "supertest"
73+
finally:
74+
await sbx.kill()
75+
76+
77+
async def test_env_vars_java(async_sandbox: AsyncSandbox):
78+
result = await async_sandbox.run_code(
79+
'System.getenv("FOO")', envs={"FOO": "bar"}
80+
)
81+
assert result.text is not None
82+
assert result.text.strip() == "bar"
83+
84+
85+
# Bash tests
86+
@pytest.mark.skip_debug()
87+
async def test_env_vars_bash_sandbox():
88+
sbx = await AsyncSandbox.create(envs={"TEST_ENV_VAR": "supertest"})
89+
try:
90+
result = await sbx.run_code("echo $TEST_ENV_VAR")
91+
assert result.text is not None
92+
assert result.text.strip() == "supertest"
93+
finally:
94+
await sbx.kill()
95+
96+
97+
async def test_env_vars_bash(async_sandbox: AsyncSandbox):
98+
result = await async_sandbox.run_code(
99+
"echo $FOO", envs={"FOO": "bar"}
100+
)
101+
assert result.text is not None
102+
assert result.text.strip() == "bar"

0 commit comments

Comments
 (0)