Skip to content

Commit b497866

Browse files
committed
Merge remote-tracking branch 'origin/master' into delete-project-data
2 parents 4826f84 + 2c8aaa2 commit b497866

File tree

10 files changed

+457
-68
lines changed

10 files changed

+457
-68
lines changed

src/packages/backend/execute-code.test.ts

+136
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,139 @@ describe("test timeout", () => {
8989
}
9090
});
9191
});
92+
93+
describe("test env", () => {
94+
it("allows to specify environment variables", async () => {
95+
const { stdout, stderr, type } = await executeCode({
96+
command: "sh",
97+
args: ["-c", "echo $FOO;"],
98+
err_on_exit: false,
99+
bash: false,
100+
env: { FOO: "bar" },
101+
});
102+
expect(type).toBe("blocking");
103+
expect(stdout).toBe("bar\n");
104+
expect(stderr).toBe("");
105+
});
106+
});
107+
108+
describe("async", () => {
109+
it("use ID to get async result", async () => {
110+
const c = await executeCode({
111+
command: "sh",
112+
args: ["-c", "echo foo; sleep .5; echo bar; sleep .5; echo baz;"],
113+
bash: false,
114+
timeout: 10,
115+
async_call: true,
116+
});
117+
expect(c.type).toEqual("async");
118+
if (c.type !== "async") return;
119+
const { status, start, job_id } = c;
120+
expect(status).toEqual("running");
121+
expect(start).toBeGreaterThan(1);
122+
expect(typeof job_id).toEqual("string");
123+
if (typeof job_id !== "string") return;
124+
await new Promise((done) => setTimeout(done, 250));
125+
{
126+
const s = await executeCode({ async_get: job_id });
127+
expect(s.type).toEqual("async");
128+
if (s.type !== "async") return;
129+
expect(s.status).toEqual("running");
130+
// partial stdout result
131+
expect(s.stdout).toEqual("foo\n");
132+
expect(s.elapsed_s).toBeUndefined();
133+
expect(s.start).toBeGreaterThan(1);
134+
expect(s.exit_code).toEqual(0);
135+
}
136+
137+
await new Promise((done) => setTimeout(done, 900));
138+
{
139+
const s = await executeCode({ async_get: job_id });
140+
expect(s.type).toEqual("async");
141+
if (s.type !== "async") return;
142+
expect(s.status).toEqual("completed");
143+
expect(s.stdout).toEqual("foo\nbar\nbaz\n");
144+
expect(s.elapsed_s).toBeGreaterThan(0.1);
145+
expect(s.elapsed_s).toBeLessThan(3);
146+
expect(s.start).toBeGreaterThan(Date.now() - 10 * 1000);
147+
expect(s.stderr).toEqual("");
148+
expect(s.exit_code).toEqual(0);
149+
}
150+
});
151+
152+
it("error/err_on_exit=true", async () => {
153+
const c = await executeCode({
154+
command: ">&2 echo baz; exit 3",
155+
bash: true,
156+
async_call: true,
157+
err_on_exit: true, // default
158+
});
159+
expect(c.type).toEqual("async");
160+
if (c.type !== "async") return;
161+
const { job_id } = c;
162+
expect(typeof job_id).toEqual("string");
163+
if (typeof job_id !== "string") return;
164+
await new Promise((done) => setTimeout(done, 250));
165+
const s = await executeCode({ async_get: job_id });
166+
expect(s.type).toEqual("async");
167+
if (s.type !== "async") return;
168+
expect(s.status).toEqual("error");
169+
expect(s.stdout).toEqual("");
170+
expect(s.stderr).toEqual("baz\n");
171+
// any error is code 1 it seems?
172+
expect(s.exit_code).toEqual(1);
173+
});
174+
175+
// without err_on_exit, the call is "completed" and we get the correct exit code
176+
it("error/err_on_exit=false", async () => {
177+
const c = await executeCode({
178+
command: ">&2 echo baz; exit 3",
179+
bash: true,
180+
async_call: true,
181+
err_on_exit: false,
182+
});
183+
expect(c.type).toEqual("async");
184+
if (c.type !== "async") return;
185+
const { job_id } = c;
186+
expect(typeof job_id).toEqual("string");
187+
if (typeof job_id !== "string") return;
188+
await new Promise((done) => setTimeout(done, 250));
189+
const s = await executeCode({ async_get: job_id });
190+
expect(s.type).toEqual("async");
191+
if (s.type !== "async") return;
192+
expect(s.status).toEqual("completed");
193+
expect(s.stdout).toEqual("");
194+
expect(s.stderr).toEqual("baz\n");
195+
expect(s.exit_code).toEqual(3);
196+
});
197+
198+
it("trigger a timeout", async () => {
199+
const c = await executeCode({
200+
command: "sh",
201+
args: ["-c", "echo foo; sleep 1; echo bar;"],
202+
bash: false,
203+
timeout: 0.1,
204+
async_call: true,
205+
});
206+
expect(c.type).toEqual("async");
207+
if (c.type !== "async") return;
208+
const { status, start, job_id } = c;
209+
expect(status).toEqual("running");
210+
expect(start).toBeGreaterThan(1);
211+
expect(typeof job_id).toEqual("string");
212+
if (typeof job_id !== "string") return;
213+
await new Promise((done) => setTimeout(done, 250));
214+
const s = await executeCode({ async_get: job_id });
215+
expect(s.type).toEqual("async");
216+
if (s.type !== "async") return;
217+
expect(s.status).toEqual("error");
218+
expect(s.stdout).toEqual("");
219+
expect(s.elapsed_s).toBeGreaterThan(0.01);
220+
expect(s.elapsed_s).toBeLessThan(3);
221+
expect(s.start).toBeGreaterThan(1);
222+
expect(s.stderr).toEqual(
223+
"killed command 'sh -c echo foo; sleep 1; echo bar;'",
224+
);
225+
expect(s.exit_code).toEqual(1);
226+
});
227+
});

0 commit comments

Comments
 (0)