Skip to content

Commit 8511114

Browse files
mishushakovjakubno
andauthored
Fixes env vars in languages other than Python (#108)
* env var handling in languages other than python * added bash to env var processing * reset env variables to global in case of override or delete them after execution * moved kernel pre-init to lifespan * removed unused 0001_envs.py * added tests * changed logic for setting env vars like setting cwd * print > logger * updated JS SDK tests for env vars * env vars tests for Python * added env var tests for r * changed python env var setting using ipython syntax * fixed setting env vars in java * fixes r * updated tests (bash mostly) * testing resetting to default in case of override * fix regression in python test * async http client for requesting env vars * updated tests - use correct template * fixes env vars in java * set global envs on first execution * removed deno tests due timeout * fixes async python tests * fixes sync python tests * fixes r tests * small oversight in r async test --------- Co-authored-by: Jakub Novák <jakub@e2b.dev>
1 parent 3ec1fcd commit 8511114

24 files changed

+980
-279
lines changed

js/tests/envVars.test.ts

Lines changed: 0 additions & 65 deletions
This file was deleted.

js/tests/env_vars/bash.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { expect } from 'vitest'
2+
3+
import { isDebug, sandboxTest } from '../setup'
4+
import { Sandbox } from '../../src'
5+
6+
// Bash Env Vars
7+
sandboxTest.skipIf(isDebug)('env vars on sandbox (bash)', async ({ template }) => {
8+
const sandbox = await Sandbox.create(template, {
9+
envs: { TEST_ENV_VAR: 'supertest' },
10+
})
11+
12+
try {
13+
const result = await sandbox.runCode(
14+
`echo $TEST_ENV_VAR`,
15+
{
16+
language: 'bash',
17+
}
18+
)
19+
20+
expect(result.logs.stdout[0]).toEqual('supertest\n')
21+
} finally {
22+
await sandbox.kill()
23+
}
24+
})
25+
26+
sandboxTest('env vars per execution (bash)', async ({ sandbox }) => {
27+
const result = await sandbox.runCode('echo $FOO', {
28+
envs: { FOO: 'bar' },
29+
language: 'bash',
30+
})
31+
32+
const result_empty = await sandbox.runCode(
33+
'echo ${FOO:-default}',
34+
{
35+
language: 'bash',
36+
}
37+
)
38+
39+
expect(result.logs.stdout[0]).toEqual('bar\n')
40+
expect(result_empty.logs.stdout[0]).toEqual('default\n')
41+
})
42+
43+
sandboxTest.skipIf(isDebug)('env vars overwrite', async ({ template }) => {
44+
const sandbox = await Sandbox.create(template, {
45+
envs: { TEST_ENV_VAR: 'supertest' },
46+
})
47+
48+
try {
49+
const result = await sandbox.runCode(
50+
`echo $TEST_ENV_VAR`,
51+
{
52+
language: 'bash',
53+
envs: { TEST_ENV_VAR: 'overwrite' },
54+
}
55+
)
56+
57+
const result_global_default = await sandbox.runCode(
58+
`echo $TEST_ENV_VAR`,
59+
{
60+
language: 'bash',
61+
}
62+
)
63+
64+
expect(result.logs.stdout[0]).toEqual('overwrite\n')
65+
expect(result_global_default.logs.stdout[0]).toEqual('supertest\n')
66+
} finally {
67+
await sandbox.kill()
68+
}
69+
})

js/tests/env_vars/java.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { expect } from 'vitest'
2+
3+
import { isDebug, sandboxTest } from '../setup'
4+
import { Sandbox } from '../../src'
5+
6+
// Java Env Vars
7+
sandboxTest.skipIf(isDebug)('env vars on sandbox (java)', async ({ template }) => {
8+
const sandbox = await Sandbox.create(template, {
9+
envs: { TEST_ENV_VAR: 'supertest' },
10+
})
11+
12+
try {
13+
const result = await sandbox.runCode(
14+
`System.getProperty("TEST_ENV_VAR")`,
15+
{
16+
language: 'java',
17+
}
18+
)
19+
20+
expect(result.results[0]?.text.trim()).toEqual('supertest')
21+
} finally {
22+
await sandbox.kill()
23+
}
24+
})
25+
26+
sandboxTest('env vars per execution (java)', async ({ sandbox }) => {
27+
const result = await sandbox.runCode(
28+
`System.getProperty("FOO")`,
29+
{
30+
envs: { FOO: 'bar' },
31+
language: 'java',
32+
}
33+
)
34+
35+
const result_empty = await sandbox.runCode(
36+
`System.getProperty("FOO", "default")`,
37+
{
38+
language: 'java',
39+
}
40+
)
41+
42+
expect(result.results[0]?.text.trim()).toEqual('bar')
43+
expect(result_empty.results[0]?.text.trim()).toEqual('default')
44+
})
45+
46+
sandboxTest.skipIf(isDebug)('env vars overwrite', async ({ template }) => {
47+
const sandbox = await Sandbox.create(template, {
48+
envs: { TEST_ENV_VAR: 'supertest' },
49+
})
50+
51+
try {
52+
const result = await sandbox.runCode(
53+
`System.getProperty("TEST_ENV_VAR")`,
54+
{
55+
language: 'java',
56+
envs: { TEST_ENV_VAR: 'overwrite' },
57+
}
58+
)
59+
60+
const result_global_default = await sandbox.runCode(
61+
`System.getProperty("TEST_ENV_VAR")`,
62+
{
63+
language: 'java',
64+
}
65+
)
66+
67+
expect(result.results[0]?.text.trim()).toEqual('overwrite')
68+
expect(result_global_default.results[0]?.text.trim()).toEqual('supertest')
69+
} finally {
70+
await sandbox.kill()
71+
}
72+
})

js/tests/env_vars/js.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { expect } from 'vitest'
2+
3+
import { isDebug, sandboxTest } from '../setup'
4+
import { Sandbox } from '../../src'
5+
6+
// JavaScript Env Vars
7+
sandboxTest.skipIf(isDebug)('env vars on sandbox (javascript)', async ({ template }) => {
8+
const sandbox = await Sandbox.create(template, {
9+
envs: { TEST_ENV_VAR: 'supertest' },
10+
})
11+
12+
try {
13+
const result = await sandbox.runCode(
14+
`process.env.TEST_ENV_VAR`,
15+
{
16+
language: 'javascript',
17+
}
18+
)
19+
20+
expect(result.results[0]?.text.trim()).toEqual('supertest')
21+
} finally {
22+
await sandbox.kill()
23+
}
24+
})
25+
26+
sandboxTest('env vars per execution (javascript)', async ({ sandbox }) => {
27+
const result = await sandbox.runCode("process.env.FOO", {
28+
envs: { FOO: 'bar' },
29+
language: 'javascript',
30+
})
31+
32+
const result_empty = await sandbox.runCode(
33+
"process.env.FOO || 'default'",
34+
{
35+
language: 'javascript',
36+
}
37+
)
38+
39+
expect(result.results[0]?.text.trim()).toEqual('bar')
40+
expect(result_empty.results[0]?.text.trim()).toEqual('default')
41+
})
42+
43+
sandboxTest.skipIf(isDebug)('env vars overwrite', async ({ template }) => {
44+
const sandbox = await Sandbox.create(template, {
45+
envs: { TEST_ENV_VAR: 'supertest' },
46+
})
47+
48+
try {
49+
const result = await sandbox.runCode(
50+
`process.env.TEST_ENV_VAR`,
51+
{
52+
language: 'javascript',
53+
envs: { TEST_ENV_VAR: 'overwrite' },
54+
}
55+
)
56+
57+
const result_global_default = await sandbox.runCode(
58+
`process.env.TEST_ENV_VAR`,
59+
{
60+
language: 'javascript',
61+
}
62+
)
63+
64+
expect(result.results[0]?.text.trim()).toEqual('overwrite')
65+
expect(result_global_default.results[0]?.text.trim()).toEqual('supertest')
66+
} finally {
67+
await sandbox.kill()
68+
}
69+
})

js/tests/env_vars/python.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { expect } from 'vitest'
2+
3+
import { isDebug, sandboxTest } from '../setup'
4+
import { Sandbox } from '../../src'
5+
6+
// Python Env Vars
7+
sandboxTest.skipIf(isDebug)('env vars on sandbox (python)', async ({ template }) => {
8+
const sandbox = await Sandbox.create(template, {
9+
envs: { TEST_ENV_VAR: 'supertest' },
10+
})
11+
12+
try {
13+
const result = await sandbox.runCode(
14+
`import os; os.getenv('TEST_ENV_VAR')`,
15+
{
16+
language: 'python',
17+
}
18+
)
19+
20+
expect(result.results[0]?.text.trim()).toEqual('supertest')
21+
} finally {
22+
await sandbox.kill()
23+
}
24+
})
25+
26+
sandboxTest('env vars per execution (python)', async ({ sandbox }) => {
27+
const result = await sandbox.runCode("import os; os.getenv('FOO')", {
28+
envs: { FOO: 'bar' },
29+
language: 'python',
30+
})
31+
32+
const result_empty = await sandbox.runCode(
33+
"import os; os.getenv('FOO', 'default')",
34+
{
35+
language: 'python',
36+
}
37+
)
38+
39+
expect(result.results[0]?.text.trim()).toEqual('bar')
40+
expect(result_empty.results[0]?.text.trim()).toEqual('default')
41+
})
42+
43+
sandboxTest.skipIf(isDebug)('env vars overwrite', async ({ template }) => {
44+
const sandbox = await Sandbox.create(template, {
45+
envs: { TEST_ENV_VAR: 'supertest' },
46+
})
47+
48+
try {
49+
const result = await sandbox.runCode(
50+
`import os; os.getenv('TEST_ENV_VAR')`,
51+
{
52+
language: 'python',
53+
envs: { TEST_ENV_VAR: 'overwrite' },
54+
}
55+
)
56+
57+
const result_global_default = await sandbox.runCode(
58+
`import os; os.getenv('TEST_ENV_VAR')`,
59+
{
60+
language: 'python',
61+
}
62+
)
63+
64+
expect(result.results[0]?.text.trim()).toEqual('overwrite')
65+
expect(result_global_default.results[0]?.text.trim()).toEqual('supertest')
66+
} finally {
67+
await sandbox.kill()
68+
}
69+
})

0 commit comments

Comments
 (0)