Skip to content

Commit 5370c93

Browse files
committed
updated JS SDK tests for env vars
1 parent b8f0f8e commit 5370c93

File tree

6 files changed

+307
-134
lines changed

6 files changed

+307
-134
lines changed

js/tests/envVars.test.ts

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

js/tests/envs/bash.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 () => {
8+
const sandbox = await Sandbox.create({
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.results[0]?.text.trim()).toEqual('supertest')
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.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 () => {
44+
const sandbox = await Sandbox.create({
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+
expect(result.results[0]?.text.trim()).toEqual('overwrite')
58+
} finally {
59+
await sandbox.kill()
60+
}
61+
})

js/tests/envs/deno.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { expect } from 'vitest'
2+
3+
import { isDebug, sandboxTest } from '../setup'
4+
import { Sandbox } from '../../src'
5+
6+
// Deno Env Vars
7+
sandboxTest.skipIf(isDebug)('env vars on sandbox (deno)', async () => {
8+
const sandbox = await Sandbox.create({
9+
envs: { TEST_ENV_VAR: 'supertest' },
10+
})
11+
12+
try {
13+
const result = await sandbox.runCode(
14+
`Deno.env.get('TEST_ENV_VAR')`,
15+
{
16+
language: 'deno',
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 (deno)', async ({ sandbox }) => {
27+
const result = await sandbox.runCode("Deno.env.get('FOO')", {
28+
envs: { FOO: 'bar' },
29+
language: 'deno',
30+
})
31+
32+
const result_empty = await sandbox.runCode(
33+
"Deno.env.get('FOO') ?? 'default'",
34+
{
35+
language: 'deno',
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 () => {
44+
const sandbox = await Sandbox.create({
45+
envs: { TEST_ENV_VAR: 'supertest' },
46+
})
47+
48+
try {
49+
const result = await sandbox.runCode(
50+
`Deno.env.get('TEST_ENV_VAR')`,
51+
{
52+
language: 'deno',
53+
envs: { TEST_ENV_VAR: 'overwrite' },
54+
}
55+
)
56+
57+
expect(result.results[0]?.text.trim()).toEqual('overwrite')
58+
} finally {
59+
await sandbox.kill()
60+
}
61+
})

js/tests/envs/java.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 () => {
8+
const sandbox = await Sandbox.create({
9+
envs: { TEST_ENV_VAR: 'supertest' },
10+
})
11+
12+
try {
13+
const result = await sandbox.runCode(
14+
`System.out.println(System.getenv("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.out.println(System.getenv("FOO"))`,
29+
{
30+
envs: { FOO: 'bar' },
31+
language: 'java',
32+
}
33+
)
34+
35+
const result_empty = await sandbox.runCode(
36+
`System.out.println(System.getenv("FOO") != null ? System.getenv("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 () => {
47+
const sandbox = await Sandbox.create({
48+
envs: { TEST_ENV_VAR: 'supertest' },
49+
})
50+
51+
try {
52+
const result = await sandbox.runCode(
53+
`System.out.println(System.getenv("TEST_ENV_VAR"))`,
54+
{
55+
language: 'java',
56+
envs: { TEST_ENV_VAR: 'overwrite' },
57+
}
58+
)
59+
60+
expect(result.results[0]?.text.trim()).toEqual('overwrite')
61+
} finally {
62+
await sandbox.kill()
63+
}
64+
})

js/tests/envs/js.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 () => {
8+
const sandbox = await Sandbox.create({
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 () => {
44+
const sandbox = await Sandbox.create({
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+
expect(result.results[0]?.text.trim()).toEqual('overwrite')
58+
} finally {
59+
await sandbox.kill()
60+
}
61+
})

0 commit comments

Comments
 (0)