-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathenvVars.test.ts
More file actions
65 lines (53 loc) · 1.77 KB
/
envVars.test.ts
File metadata and controls
65 lines (53 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { expect } from 'vitest'
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 () => {
const sandbox = await Sandbox.create({
envs: { TEST_ENV_VAR: 'supertest' },
})
try {
const result = await sandbox.runCode(
`import os; x = os.getenv('TEST_ENV_VAR'); x`
)
expect(result.results[0].text.trim()).toEqual('supertest')
} finally {
await sandbox.kill()
}
})
sandboxTest('env vars on sandbox', async ({ sandbox }) => {
const result = await sandbox.runCode(
"import os; os.getenv('FOO')",
{ envs: { FOO: 'bar' } }
)
expect(result.results[0].text.trim()).toEqual('bar')
})
sandboxTest('env vars on sandbox override', async () => {
const sandbox = await Sandbox.create({
envs: { FOO: 'bar', SBX: 'value' },
})
try {
await sandbox.runCode(
"import os; os.environ['FOO'] = 'bar'; os.environ['RUNTIME_ENV'] = 'js_runtime'"
)
const result = await sandbox.runCode(
"import os; os.getenv('FOO')",
{ envs: { FOO: 'baz' } }
)
expect(result.results[0].text.trim()).toEqual('baz')
const result2 = await sandbox.runCode(
"import os; os.getenv('RUNTIME_ENV')"
)
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')
}
const result4 = await sandbox.runCode("import os; os.getenv('FOO')")
expect(result4.results[0].text.trim()).toEqual('bar')
} finally {
await sandbox.kill()
}
})