@@ -3,8 +3,8 @@ import { expect } from 'vitest'
3
3
import { isDebug , sandboxTest } from './setup'
4
4
import { Sandbox } from '../src'
5
5
6
- // 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.
7
- sandboxTest . skipIf ( isDebug ) ( 'env vars' , async ( ) => {
6
+ // Python tests
7
+ sandboxTest . skipIf ( isDebug ) ( 'env vars (python) ' , async ( ) => {
8
8
const sandbox = await Sandbox . create ( {
9
9
envs : { TEST_ENV_VAR : 'supertest' } ,
10
10
} )
@@ -14,52 +14,121 @@ sandboxTest.skipIf(isDebug)('env vars', async () => {
14
14
`import os; x = os.getenv('TEST_ENV_VAR'); x`
15
15
)
16
16
17
- expect ( result . results [ 0 ] . text . trim ( ) ) . toEqual ( 'supertest' )
17
+ expect ( result . results [ 0 ] ? .text . trim ( ) ) . toEqual ( 'supertest' )
18
18
} finally {
19
19
await sandbox . kill ( )
20
20
}
21
21
} )
22
22
23
- sandboxTest ( 'env vars on sandbox' , async ( { sandbox } ) => {
23
+ sandboxTest ( 'env vars on sandbox (python) ' , async ( { sandbox } ) => {
24
24
const result = await sandbox . runCode (
25
25
"import os; os.getenv('FOO')" ,
26
26
{ envs : { FOO : 'bar' } }
27
27
)
28
28
29
- expect ( result . results [ 0 ] . text . trim ( ) ) . toEqual ( 'bar' )
29
+ expect ( result . results [ 0 ] ? .text . trim ( ) ) . toEqual ( 'bar' )
30
30
} )
31
31
32
- sandboxTest ( 'env vars on sandbox override' , async ( ) => {
32
+ // JavaScript tests
33
+ sandboxTest . skipIf ( isDebug ) ( 'env vars (javascript)' , async ( ) => {
33
34
const sandbox = await Sandbox . create ( {
34
- envs : { FOO : 'bar' , SBX : 'value ' } ,
35
+ envs : { TEST_ENV_VAR : 'supertest ' } ,
35
36
} )
36
37
37
38
try {
38
- await sandbox . runCode (
39
- "import os; os.environ['FOO'] = 'bar'; os.environ['RUNTIME_ENV'] = 'js_runtime'"
39
+ const result = await sandbox . runCode (
40
+ `process.env.TEST_ENV_VAR`
40
41
)
42
+
43
+ expect ( result . results [ 0 ] ?. text . trim ( ) ) . toEqual ( 'supertest' )
44
+ } finally {
45
+ await sandbox . kill ( )
46
+ }
47
+ } )
48
+
49
+ sandboxTest ( 'env vars on sandbox (javascript)' , async ( { sandbox } ) => {
50
+ const result = await sandbox . runCode (
51
+ `process.env.FOO` ,
52
+ { envs : { FOO : 'bar' } }
53
+ )
54
+
55
+ expect ( result . results [ 0 ] ?. text . trim ( ) ) . toEqual ( 'bar' )
56
+ } )
57
+
58
+ // R tests
59
+ sandboxTest . skipIf ( isDebug ) ( 'env vars (r)' , async ( ) => {
60
+ const sandbox = await Sandbox . create ( {
61
+ envs : { TEST_ENV_VAR : 'supertest' } ,
62
+ } )
63
+
64
+ try {
41
65
const result = await sandbox . runCode (
42
- "import os; os.getenv('FOO')" ,
43
- { envs : { FOO : 'baz' } }
66
+ `Sys.getenv("TEST_ENV_VAR")`
44
67
)
45
68
46
- expect ( result . results [ 0 ] . text . trim ( ) ) . toEqual ( 'baz' )
69
+ expect ( result . results [ 0 ] ?. text . trim ( ) ) . toEqual ( 'supertest' )
70
+ } finally {
71
+ await sandbox . kill ( )
72
+ }
73
+ } )
47
74
48
- const result2 = await sandbox . runCode (
49
- "import os; os.getenv('RUNTIME_ENV')"
75
+ sandboxTest ( 'env vars on sandbox (r)' , async ( { sandbox } ) => {
76
+ const result = await sandbox . runCode (
77
+ `Sys.getenv("FOO")` ,
78
+ { envs : { FOO : 'bar' } }
79
+ )
80
+
81
+ expect ( result . results [ 0 ] ?. text . trim ( ) ) . toEqual ( 'bar' )
82
+ } )
83
+
84
+ // Java tests
85
+ sandboxTest . skipIf ( isDebug ) ( 'env vars (java)' , async ( ) => {
86
+ const sandbox = await Sandbox . create ( {
87
+ envs : { TEST_ENV_VAR : 'supertest' } ,
88
+ } )
89
+
90
+ try {
91
+ const result = await sandbox . runCode (
92
+ `System.getenv("TEST_ENV_VAR")`
50
93
)
51
- expect ( result2 . results [ 0 ] . text . trim ( ) ) . toEqual ( 'js_runtime' )
52
94
53
- if ( ! isDebug ) {
54
- const result3 = await sandbox . runCode (
55
- "import os; os.getenv('SBX')"
56
- )
57
- expect ( result3 . results [ 0 ] . text . trim ( ) ) . toEqual ( 'value' )
58
- }
95
+ expect ( result . results [ 0 ] ?. text . trim ( ) ) . toEqual ( 'supertest' )
96
+ } finally {
97
+ await sandbox . kill ( )
98
+ }
99
+ } )
100
+
101
+ sandboxTest ( 'env vars on sandbox (java)' , async ( { sandbox } ) => {
102
+ const result = await sandbox . runCode (
103
+ `System.getenv("FOO")` ,
104
+ { envs : { FOO : 'bar' } }
105
+ )
106
+
107
+ expect ( result . results [ 0 ] ?. text . trim ( ) ) . toEqual ( 'bar' )
108
+ } )
59
109
60
- const result4 = await sandbox . runCode ( "import os; os.getenv('FOO')" )
61
- expect ( result4 . results [ 0 ] . text . trim ( ) ) . toEqual ( 'bar' )
110
+ // Bash tests
111
+ sandboxTest . skipIf ( isDebug ) ( 'env vars (bash)' , async ( ) => {
112
+ const sandbox = await Sandbox . create ( {
113
+ envs : { TEST_ENV_VAR : 'supertest' } ,
114
+ } )
115
+
116
+ try {
117
+ const result = await sandbox . runCode (
118
+ `echo $TEST_ENV_VAR`
119
+ )
120
+
121
+ expect ( result . results [ 0 ] ?. text . trim ( ) ) . toEqual ( 'supertest' )
62
122
} finally {
63
123
await sandbox . kill ( )
64
124
}
65
125
} )
126
+
127
+ sandboxTest ( 'env vars on sandbox (bash)' , async ( { sandbox } ) => {
128
+ const result = await sandbox . runCode (
129
+ `echo $FOO` ,
130
+ { envs : { FOO : 'bar' } }
131
+ )
132
+
133
+ expect ( result . results [ 0 ] ?. text . trim ( ) ) . toEqual ( 'bar' )
134
+ } )
0 commit comments