Skip to content

Fixes env vars in languages other than Python #108

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
15cceb9
env var handling in languages other than python
mishushakov May 14, 2025
3d9cb5b
added bash to env var processing
mishushakov May 15, 2025
f34cf8f
reset env variables to global in case of override or delete them afte…
mishushakov May 21, 2025
cd0e3ef
Merge branch 'main' into environment-variables-are-not-accessible-whe…
mishushakov Jun 3, 2025
bcfba5d
moved kernel pre-init to lifespan
mishushakov Jun 3, 2025
f4a6584
removed unused 0001_envs.py
mishushakov Jun 3, 2025
872c426
Merge branch 'main' into environment-variables-are-not-accessible-whe…
jakubno Jun 6, 2025
ab53907
added tests
mishushakov Jun 6, 2025
60c1a06
changed logic for setting env vars like setting cwd
mishushakov Jun 6, 2025
b8f0f8e
print > logger
mishushakov Jun 6, 2025
5370c93
updated JS SDK tests for env vars
mishushakov Jun 6, 2025
fe6bd96
env vars tests for Python
mishushakov Jun 6, 2025
1ba8718
added env var tests for r
mishushakov Jun 6, 2025
ad92618
changed python env var setting using ipython syntax
mishushakov Jun 6, 2025
de3bb5d
fixed setting env vars in java
mishushakov Jun 6, 2025
1845152
fixes r
mishushakov Jun 6, 2025
45a18e0
updated tests (bash mostly)
mishushakov Jun 6, 2025
3bf7cdb
testing resetting to default in case of override
mishushakov Jun 6, 2025
7f9b10f
fix regression in python test
mishushakov Jun 6, 2025
76f15f8
async http client for requesting env vars
mishushakov Jun 6, 2025
ac16a84
updated tests - use correct template
mishushakov Jul 7, 2025
645f91d
fixes env vars in java
mishushakov Jul 8, 2025
2072a8e
set global envs on first execution
mishushakov Jul 8, 2025
02b4796
removed deno tests due timeout
mishushakov Jul 8, 2025
94804b4
fixes async python tests
mishushakov Jul 8, 2025
24eb4bd
fixes sync python tests
mishushakov Jul 8, 2025
7e93417
fixes r tests
mishushakov Jul 8, 2025
761d36c
small oversight in r async test
mishushakov Jul 8, 2025
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
65 changes: 0 additions & 65 deletions js/tests/envVars.test.ts

This file was deleted.

69 changes: 69 additions & 0 deletions js/tests/env_vars/bash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { expect } from 'vitest'

import { isDebug, sandboxTest } from '../setup'
import { Sandbox } from '../../src'

// Bash Env Vars
sandboxTest.skipIf(isDebug)('env vars on sandbox (bash)', async ({ template }) => {
const sandbox = await Sandbox.create(template, {
envs: { TEST_ENV_VAR: 'supertest' },
})

try {
const result = await sandbox.runCode(
`echo $TEST_ENV_VAR`,
{
language: 'bash',
}
)

expect(result.logs.stdout[0]).toEqual('supertest\n')
} finally {
await sandbox.kill()
}
})

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

const result_empty = await sandbox.runCode(
'echo ${FOO:-default}',
{
language: 'bash',
}
)

expect(result.logs.stdout[0]).toEqual('bar\n')
expect(result_empty.logs.stdout[0]).toEqual('default\n')
})

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

try {
const result = await sandbox.runCode(
`echo $TEST_ENV_VAR`,
{
language: 'bash',
envs: { TEST_ENV_VAR: 'overwrite' },
}
)

const result_global_default = await sandbox.runCode(
`echo $TEST_ENV_VAR`,
{
language: 'bash',
}
)

expect(result.logs.stdout[0]).toEqual('overwrite\n')
expect(result_global_default.logs.stdout[0]).toEqual('supertest\n')
} finally {
await sandbox.kill()
}
})
72 changes: 72 additions & 0 deletions js/tests/env_vars/java.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { expect } from 'vitest'

import { isDebug, sandboxTest } from '../setup'
import { Sandbox } from '../../src'

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

try {
const result = await sandbox.runCode(
`System.getProperty("TEST_ENV_VAR")`,
{
language: 'java',
}
)

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

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

const result_empty = await sandbox.runCode(
`System.getProperty("FOO", "default")`,
{
language: 'java',
}
)

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

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

try {
const result = await sandbox.runCode(
`System.getProperty("TEST_ENV_VAR")`,
{
language: 'java',
envs: { TEST_ENV_VAR: 'overwrite' },
}
)

const result_global_default = await sandbox.runCode(
`System.getProperty("TEST_ENV_VAR")`,
{
language: 'java',
}
)

expect(result.results[0]?.text.trim()).toEqual('overwrite')
expect(result_global_default.results[0]?.text.trim()).toEqual('supertest')
} finally {
await sandbox.kill()
}
})
69 changes: 69 additions & 0 deletions js/tests/env_vars/js.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { expect } from 'vitest'

import { isDebug, sandboxTest } from '../setup'
import { Sandbox } from '../../src'

// JavaScript Env Vars
sandboxTest.skipIf(isDebug)('env vars on sandbox (javascript)', async ({ template }) => {
const sandbox = await Sandbox.create(template, {
envs: { TEST_ENV_VAR: 'supertest' },
})

try {
const result = await sandbox.runCode(
`process.env.TEST_ENV_VAR`,
{
language: 'javascript',
}
)

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

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

const result_empty = await sandbox.runCode(
"process.env.FOO || 'default'",
{
language: 'javascript',
}
)

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

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

try {
const result = await sandbox.runCode(
`process.env.TEST_ENV_VAR`,
{
language: 'javascript',
envs: { TEST_ENV_VAR: 'overwrite' },
}
)

const result_global_default = await sandbox.runCode(
`process.env.TEST_ENV_VAR`,
{
language: 'javascript',
}
)

expect(result.results[0]?.text.trim()).toEqual('overwrite')
expect(result_global_default.results[0]?.text.trim()).toEqual('supertest')
} finally {
await sandbox.kill()
}
})
69 changes: 69 additions & 0 deletions js/tests/env_vars/python.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { expect } from 'vitest'

import { isDebug, sandboxTest } from '../setup'
import { Sandbox } from '../../src'

// Python Env Vars
sandboxTest.skipIf(isDebug)('env vars on sandbox (python)', async ({ template }) => {
const sandbox = await Sandbox.create(template, {
envs: { TEST_ENV_VAR: 'supertest' },
})

try {
const result = await sandbox.runCode(
`import os; os.getenv('TEST_ENV_VAR')`,
{
language: 'python',
}
)

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

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

const result_empty = await sandbox.runCode(
"import os; os.getenv('FOO', 'default')",
{
language: 'python',
}
)

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

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

try {
const result = await sandbox.runCode(
`import os; os.getenv('TEST_ENV_VAR')`,
{
language: 'python',
envs: { TEST_ENV_VAR: 'overwrite' },
}
)

const result_global_default = await sandbox.runCode(
`import os; os.getenv('TEST_ENV_VAR')`,
{
language: 'python',
}
)

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