@@ -3,8 +3,8 @@ import { expect } from 'vitest'
33import { isDebug , sandboxTest } from './setup'
44import { Sandbox } from '../src'
55
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 ( ) => {
88 const sandbox = await Sandbox . create ( {
99 envs : { TEST_ENV_VAR : 'supertest' } ,
1010 } )
@@ -14,52 +14,121 @@ sandboxTest.skipIf(isDebug)('env vars', async () => {
1414 `import os; x = os.getenv('TEST_ENV_VAR'); x`
1515 )
1616
17- expect ( result . results [ 0 ] . text . trim ( ) ) . toEqual ( 'supertest' )
17+ expect ( result . results [ 0 ] ? .text . trim ( ) ) . toEqual ( 'supertest' )
1818 } finally {
1919 await sandbox . kill ( )
2020 }
2121} )
2222
23- sandboxTest ( 'env vars on sandbox' , async ( { sandbox } ) => {
23+ sandboxTest ( 'env vars on sandbox (python) ' , async ( { sandbox } ) => {
2424 const result = await sandbox . runCode (
2525 "import os; os.getenv('FOO')" ,
2626 { envs : { FOO : 'bar' } }
2727 )
2828
29- expect ( result . results [ 0 ] . text . trim ( ) ) . toEqual ( 'bar' )
29+ expect ( result . results [ 0 ] ? .text . trim ( ) ) . toEqual ( 'bar' )
3030} )
3131
32- sandboxTest ( 'env vars on sandbox override' , async ( ) => {
32+ // JavaScript tests
33+ sandboxTest . skipIf ( isDebug ) ( 'env vars (javascript)' , async ( ) => {
3334 const sandbox = await Sandbox . create ( {
34- envs : { FOO : 'bar' , SBX : 'value ' } ,
35+ envs : { TEST_ENV_VAR : 'supertest ' } ,
3536 } )
3637
3738 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`
4041 )
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 {
4165 const result = await sandbox . runCode (
42- "import os; os.getenv('FOO')" ,
43- { envs : { FOO : 'baz' } }
66+ `Sys.getenv("TEST_ENV_VAR")`
4467 )
4568
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+ } )
4774
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")`
5093 )
51- expect ( result2 . results [ 0 ] . text . trim ( ) ) . toEqual ( 'js_runtime' )
5294
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+ } )
59109
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' )
62122 } finally {
63123 await sandbox . kill ( )
64124 }
65125} )
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