Skip to content

Commit 1ba8718

Browse files
committed
added env var tests for r
1 parent fe6bd96 commit 1ba8718

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

js/tests/env_vars/r.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { expect } from 'vitest'
2+
3+
import { isDebug, sandboxTest } from '../setup'
4+
import { Sandbox } from '../../src'
5+
6+
// R Env Vars
7+
sandboxTest.skipIf(isDebug)('env vars on sandbox (R)', async () => {
8+
const sandbox = await Sandbox.create({
9+
envs: { TEST_ENV_VAR: 'supertest' },
10+
})
11+
12+
try {
13+
const result = await sandbox.runCode(`Sys.getenv("TEST_ENV_VAR")`, {
14+
language: 'r',
15+
})
16+
17+
expect(result.results[0]?.text.trim()).toEqual('supertest')
18+
} finally {
19+
await sandbox.kill()
20+
}
21+
})
22+
23+
sandboxTest('env vars per execution (R)', async ({ sandbox }) => {
24+
const result = await sandbox.runCode('Sys.getenv("FOO")', {
25+
envs: { FOO: 'bar' },
26+
})
27+
28+
const result_empty = await sandbox.runCode(
29+
'Sys.getenv("FOO", unset = "default")',
30+
{
31+
language: 'r',
32+
}
33+
)
34+
35+
expect(result.results[0]?.text.trim()).toEqual('bar')
36+
expect(result_empty.results[0]?.text.trim()).toEqual('default')
37+
})
38+
39+
sandboxTest.skipIf(isDebug)('env vars overwrite', async () => {
40+
const sandbox = await Sandbox.create({
41+
envs: { TEST_ENV_VAR: 'supertest' },
42+
})
43+
44+
try {
45+
const result = await sandbox.runCode(`Sys.getenv("TEST_ENV_VAR")`, {
46+
language: 'r',
47+
envs: { TEST_ENV_VAR: 'overwrite' },
48+
})
49+
50+
expect(result.results[0]?.text.trim()).toEqual('overwrite')
51+
} finally {
52+
await sandbox.kill()
53+
}
54+
})
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pytest
2+
from e2b_code_interpreter.code_interpreter_async import AsyncSandbox
3+
4+
@pytest.mark.skip_debug()
5+
async def test_env_vars_on_sandbox():
6+
sandbox = await AsyncSandbox.create(envs={"TEST_ENV_VAR": "supertest"})
7+
try:
8+
result = await sandbox.run_code(
9+
"Sys.getenv('TEST_ENV_VAR')",
10+
language="r"
11+
)
12+
assert result.text is not None
13+
assert result.text.strip() == "supertest"
14+
finally:
15+
await sandbox.kill()
16+
17+
async def test_env_vars_per_execution():
18+
sandbox = await AsyncSandbox.create()
19+
try:
20+
result = await sandbox.run_code(
21+
"Sys.getenv('FOO')",
22+
envs={"FOO": "bar"},
23+
language="r"
24+
)
25+
26+
result_empty = await sandbox.run_code(
27+
"Sys.getenv('FOO', unset = 'default')",
28+
language="r"
29+
)
30+
31+
assert result.text is not None
32+
assert result.text.strip() == "bar"
33+
assert result_empty.text is not None
34+
assert result_empty.text.strip() == "default"
35+
finally:
36+
await sandbox.kill()
37+
38+
@pytest.mark.skip_debug()
39+
async def test_env_vars_overwrite():
40+
sandbox = await AsyncSandbox.create(envs={"TEST_ENV_VAR": "supertest"})
41+
try:
42+
result = await sandbox.run_code(
43+
"Sys.getenv('TEST_ENV_VAR')",
44+
language="r",
45+
envs={"TEST_ENV_VAR": "overwrite"}
46+
)
47+
assert result.text is not None
48+
assert result.text.strip() == "overwrite"
49+
finally:
50+
await sandbox.kill()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import pytest
2+
from e2b_code_interpreter import Sandbox
3+
4+
@pytest.mark.skip_debug()
5+
def test_env_vars_on_sandbox():
6+
sandbox = Sandbox(envs={"TEST_ENV_VAR": "supertest"})
7+
try:
8+
result = sandbox.run_code(
9+
"Sys.getenv('TEST_ENV_VAR')",
10+
language="r"
11+
)
12+
assert result.results[0].text.strip() == "supertest"
13+
finally:
14+
sandbox.kill()
15+
16+
def test_env_vars_per_execution():
17+
sandbox = Sandbox()
18+
try:
19+
result = sandbox.run_code(
20+
"Sys.getenv('FOO')",
21+
envs={"FOO": "bar"},
22+
language="r"
23+
)
24+
25+
result_empty = sandbox.run_code(
26+
"Sys.getenv('FOO', unset = 'default')",
27+
language="r"
28+
)
29+
30+
assert result.results[0].text.strip() == "bar"
31+
assert result_empty.results[0].text.strip() == "default"
32+
finally:
33+
sandbox.kill()
34+
35+
@pytest.mark.skip_debug()
36+
def test_env_vars_overwrite():
37+
sandbox = Sandbox(envs={"TEST_ENV_VAR": "supertest"})
38+
try:
39+
result = sandbox.run_code(
40+
"Sys.getenv('TEST_ENV_VAR')",
41+
language="r",
42+
envs={"TEST_ENV_VAR": "overwrite"}
43+
)
44+
assert result.results[0].text.strip() == "overwrite"
45+
finally:
46+
sandbox.kill()

0 commit comments

Comments
 (0)