Skip to content

Added tests for env vars for languages other than Python #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 92 additions & 23 deletions js/tests/envVars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import { isDebug, sandboxTest } from './setup'
import { Sandbox } from '../src'

// 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.
sandboxTest.skipIf(isDebug)('env vars', async () => {
// Python tests
sandboxTest.skipIf(isDebug)('env vars (python)', async () => {
const sandbox = await Sandbox.create({
envs: { TEST_ENV_VAR: 'supertest' },
})
Expand All @@ -14,52 +14,121 @@
`import os; x = os.getenv('TEST_ENV_VAR'); x`
)

expect(result.results[0].text.trim()).toEqual('supertest')
expect(result.results[0]?.text.trim()).toEqual('supertest')
} finally {
await sandbox.kill()
}
})

sandboxTest('env vars on sandbox', async ({ sandbox }) => {
sandboxTest('env vars on sandbox (python)', async ({ sandbox }) => {
const result = await sandbox.runCode(
"import os; os.getenv('FOO')",
{ envs: { FOO: 'bar' } }
)

expect(result.results[0].text.trim()).toEqual('bar')
expect(result.results[0]?.text.trim()).toEqual('bar')
})

sandboxTest('env vars on sandbox override', async () => {
// JavaScript tests
sandboxTest.skipIf(isDebug)('env vars (javascript)', async () => {
const sandbox = await Sandbox.create({
envs: { FOO: 'bar', SBX: 'value' },
envs: { TEST_ENV_VAR: 'supertest' },
})

try {
await sandbox.runCode(
"import os; os.environ['FOO'] = 'bar'; os.environ['RUNTIME_ENV'] = 'js_runtime'"
const result = await sandbox.runCode(
`process.env.TEST_ENV_VAR`
)

expect(result.results[0]?.text.trim()).toEqual('supertest')

Check failure on line 43 in js/tests/envVars.test.ts

View workflow job for this annotation

GitHub Actions / js-sdk / JS SDK - Build and test

tests/envVars.test.ts > env vars (javascript)

AssertionError: expected undefined to deeply equal 'supertest' - Expected: "supertest" + Received: undefined ❯ tests/envVars.test.ts:43:44
} finally {
await sandbox.kill()
}
})

sandboxTest('env vars on sandbox (javascript)', async ({ sandbox }) => {
const result = await sandbox.runCode(
`process.env.FOO`,
{ envs: { FOO: 'bar' } }
)

expect(result.results[0]?.text.trim()).toEqual('bar')

Check failure on line 55 in js/tests/envVars.test.ts

View workflow job for this annotation

GitHub Actions / js-sdk / JS SDK - Build and test

tests/envVars.test.ts > env vars on sandbox (javascript)

AssertionError: expected undefined to deeply equal 'bar' - Expected: "bar" + Received: undefined ❯ tests/envVars.test.ts:55:42
})

// R tests
sandboxTest.skipIf(isDebug)('env vars (r)', async () => {
const sandbox = await Sandbox.create({
envs: { TEST_ENV_VAR: 'supertest' },
})

try {
const result = await sandbox.runCode(
"import os; os.getenv('FOO')",
{ envs: { FOO: 'baz' } }
`Sys.getenv("TEST_ENV_VAR")`
)

expect(result.results[0].text.trim()).toEqual('baz')
expect(result.results[0]?.text.trim()).toEqual('supertest')

Check failure on line 69 in js/tests/envVars.test.ts

View workflow job for this annotation

GitHub Actions / js-sdk / JS SDK - Build and test

tests/envVars.test.ts > env vars (r)

AssertionError: expected undefined to deeply equal 'supertest' - Expected: "supertest" + Received: undefined ❯ tests/envVars.test.ts:69:44
} finally {
await sandbox.kill()
}
})

const result2 = await sandbox.runCode(
"import os; os.getenv('RUNTIME_ENV')"
sandboxTest('env vars on sandbox (r)', async ({ sandbox }) => {
const result = await sandbox.runCode(
`Sys.getenv("FOO")`,
{ envs: { FOO: 'bar' } }
)

expect(result.results[0]?.text.trim()).toEqual('bar')

Check failure on line 81 in js/tests/envVars.test.ts

View workflow job for this annotation

GitHub Actions / js-sdk / JS SDK - Build and test

tests/envVars.test.ts > env vars on sandbox (r)

AssertionError: expected undefined to deeply equal 'bar' - Expected: "bar" + Received: undefined ❯ tests/envVars.test.ts:81:42
})

// Java tests
sandboxTest.skipIf(isDebug)('env vars (java)', async () => {
const sandbox = await Sandbox.create({
envs: { TEST_ENV_VAR: 'supertest' },
})

try {
const result = await sandbox.runCode(
`System.getenv("TEST_ENV_VAR")`
)
expect(result2.results[0].text.trim()).toEqual('js_runtime')

if (!isDebug) {
const result3 = await sandbox.runCode(
"import os; os.getenv('SBX')"
)
expect(result3.results[0].text.trim()).toEqual('value')
}
expect(result.results[0]?.text.trim()).toEqual('supertest')

Check failure on line 95 in js/tests/envVars.test.ts

View workflow job for this annotation

GitHub Actions / js-sdk / JS SDK - Build and test

tests/envVars.test.ts > env vars (java)

AssertionError: expected undefined to deeply equal 'supertest' - Expected: "supertest" + Received: undefined ❯ tests/envVars.test.ts:95:44
} finally {
await sandbox.kill()
}
})

sandboxTest('env vars on sandbox (java)', async ({ sandbox }) => {
const result = await sandbox.runCode(
`System.getenv("FOO")`,
{ envs: { FOO: 'bar' } }
)

expect(result.results[0]?.text.trim()).toEqual('bar')

Check failure on line 107 in js/tests/envVars.test.ts

View workflow job for this annotation

GitHub Actions / js-sdk / JS SDK - Build and test

tests/envVars.test.ts > env vars on sandbox (java)

AssertionError: expected undefined to deeply equal 'bar' - Expected: "bar" + Received: undefined ❯ tests/envVars.test.ts:107:42
})

const result4 = await sandbox.runCode("import os; os.getenv('FOO')")
expect(result4.results[0].text.trim()).toEqual('bar')
// Bash tests
sandboxTest.skipIf(isDebug)('env vars (bash)', async () => {
const sandbox = await Sandbox.create({
envs: { TEST_ENV_VAR: 'supertest' },
})

try {
const result = await sandbox.runCode(
`echo $TEST_ENV_VAR`
)

expect(result.results[0]?.text.trim()).toEqual('supertest')

Check failure on line 121 in js/tests/envVars.test.ts

View workflow job for this annotation

GitHub Actions / js-sdk / JS SDK - Build and test

tests/envVars.test.ts > env vars (bash)

AssertionError: expected undefined to deeply equal 'supertest' - Expected: "supertest" + Received: undefined ❯ tests/envVars.test.ts:121:44
} finally {
await sandbox.kill()
}
})

sandboxTest('env vars on sandbox (bash)', async ({ sandbox }) => {
const result = await sandbox.runCode(
`echo $FOO`,
{ envs: { FOO: 'bar' } }
)

expect(result.results[0]?.text.trim()).toEqual('bar')

Check failure on line 133 in js/tests/envVars.test.ts

View workflow job for this annotation

GitHub Actions / js-sdk / JS SDK - Build and test

tests/envVars.test.ts > env vars on sandbox (bash)

AssertionError: expected undefined to deeply equal 'bar' - Expected: "bar" + Received: undefined ❯ tests/envVars.test.ts:133:42
})
126 changes: 91 additions & 35 deletions python/tests/async/test_async_env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,100 @@
from e2b_code_interpreter.code_interpreter_async import AsyncSandbox


# @pytest.mark.skip_debug()
# async def test_env_vars_sandbox():
# sbx = await AsyncSandbox.create(envs={"FOO": "bar"})
# try:
# result = await sbx.run_code("import os; os.getenv('FOO')")
# assert result.text == "bar"
# finally:
# await sbx.kill()
@pytest.mark.skip_debug()
async def test_env_vars_sandbox():
sbx = await AsyncSandbox.create(envs={"TEST_ENV_VAR": "supertest"})
try:
result = await sbx.run_code("import os; os.getenv('TEST_ENV_VAR')")
assert result.text is not None
assert result.text.strip() == "supertest"
finally:
await sbx.kill()


async def test_env_vars_in_run_code(async_sandbox: AsyncSandbox):
result = await async_sandbox.run_code(
"import os; os.getenv('FOO')", envs={"FOO": "bar"}
)
assert result.text == "bar"


#
# async def test_env_vars_override(debug: bool):
# sbx = await AsyncSandbox.create(envs={"FOO": "bar", "SBX": "value"})
#
# try:
# await sbx.run_code(
# "import os; os.environ['FOO'] = 'bar'; os.environ['RUNTIME_ENV'] = 'async_python_runtime'"
# )
# result = await sbx.run_code("import os; os.getenv('FOO')", envs={"FOO": "baz"})
# assert result.text == "baz"
#
# # This can fail if running in debug mode (there's a race condition with the restart kernel test)
# result = await sbx.run_code("import os; os.getenv('RUNTIME_ENV')")
# assert result.text == "async_python_runtime"
#
# if not debug:
# result = await sbx.run_code("import os; os.getenv('SBX')")
# assert result.text == "value"
#
# # This can fail if running in debug mode (there's a race condition with the restart kernel test)
# result = await sbx.run_code("import os; os.getenv('FOO')")
# assert result.text == "bar"
# finally:
# await sbx.kill()
assert result.text is not None
assert result.text.strip() == "bar"


# JavaScript tests
@pytest.mark.skip_debug()
async def test_env_vars_javascript_sandbox():
sbx = await AsyncSandbox.create(envs={"TEST_ENV_VAR": "supertest"})
try:
result = await sbx.run_code("process.env.TEST_ENV_VAR")
assert result.text is not None
assert result.text.strip() == "supertest"
finally:
await sbx.kill()


async def test_env_vars_javascript(async_sandbox: AsyncSandbox):
result = await async_sandbox.run_code(
"process.env.FOO", envs={"FOO": "bar"}
)
assert result.text is not None
assert result.text.strip() == "bar"


# R tests
@pytest.mark.skip_debug()
async def test_env_vars_r_sandbox():
sbx = await AsyncSandbox.create(envs={"TEST_ENV_VAR": "supertest"})
try:
result = await sbx.run_code('Sys.getenv("TEST_ENV_VAR")')
assert result.text is not None
assert result.text.strip() == "supertest"
finally:
await sbx.kill()


async def test_env_vars_r(async_sandbox: AsyncSandbox):
result = await async_sandbox.run_code(
'Sys.getenv("FOO")', envs={"FOO": "bar"}
)
assert result.text is not None
assert result.text.strip() == "bar"


# Java tests
@pytest.mark.skip_debug()
async def test_env_vars_java_sandbox():
sbx = await AsyncSandbox.create(envs={"TEST_ENV_VAR": "supertest"})
try:
result = await sbx.run_code('System.getenv("TEST_ENV_VAR")')
assert result.text is not None
assert result.text.strip() == "supertest"
finally:
await sbx.kill()


async def test_env_vars_java(async_sandbox: AsyncSandbox):
result = await async_sandbox.run_code(
'System.getenv("FOO")', envs={"FOO": "bar"}
)
assert result.text is not None
assert result.text.strip() == "bar"


# Bash tests
@pytest.mark.skip_debug()
async def test_env_vars_bash_sandbox():
sbx = await AsyncSandbox.create(envs={"TEST_ENV_VAR": "supertest"})
try:
result = await sbx.run_code("echo $TEST_ENV_VAR")
assert result.text is not None
assert result.text.strip() == "supertest"
finally:
await sbx.kill()


async def test_env_vars_bash(async_sandbox: AsyncSandbox):
result = await async_sandbox.run_code(
"echo $FOO", envs={"FOO": "bar"}
)
assert result.text is not None
assert result.text.strip() == "bar"
Loading
Loading