@cocalc/backend
Version:
CoCalc backend functionality: functionality used by either the hub, the next.js server or the project.
90 lines • 3.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const execute_code_1 = require("./execute-code");
describe("hello world", () => {
it("runs hello world", async () => {
const { stdout } = await (0, execute_code_1.executeCode)({
command: "echo",
args: ["hello world"],
});
expect(stdout).toBe("hello world\n");
});
});
describe("tests involving bash mode", () => {
it("runs normal code in bash", async () => {
const { stdout } = await (0, execute_code_1.executeCode)({ command: "echo 'abc' | wc -c" });
// on GitHub actions the output of wc is different than on other machines,
// so we normalize by trimming.
expect(stdout.trim()).toBe("4");
});
it("reports missing executable in non-bash mode", async () => {
try {
await (0, execute_code_1.executeCode)({
command: "this_does_not_exist",
args: ["nothing"],
bash: false,
});
}
catch (err) {
expect(err).toContain("ENOENT");
}
});
it("reports missing executable in non-bash mode when ignoring on exit", async () => {
try {
await (0, execute_code_1.executeCode)({
command: "this_does_not_exist",
args: ["nothing"],
err_on_exit: false,
bash: false,
});
}
catch (err) {
expect(err).toContain("ENOENT");
}
});
it("ignores errors otherwise if err_on_exit is false", async () => {
const { stdout, stderr, exit_code } = await (0, execute_code_1.executeCode)({
command: "sh",
args: ["-c", "echo foo; exit 42"],
err_on_exit: false,
bash: false,
});
expect(stdout).toBe("foo\n");
expect(stderr).toBe("");
expect(exit_code).toBe(42);
});
});
describe("test timeout", () => {
it("kills if timeout reached", async () => {
const t = new Date().valueOf();
try {
await (0, execute_code_1.executeCode)({ command: "sleep 60", timeout: 0.1 });
expect(false).toBe(true);
}
catch (err) {
expect(err).toContain("killed command");
expect(new Date().valueOf() - t).toBeGreaterThan(90);
expect(new Date().valueOf() - t).toBeLessThan(500);
}
});
it("doesn't kill when timeout not reached", async () => {
const t = new Date().valueOf();
await (0, execute_code_1.executeCode)({ command: "sleep 0.1", timeout: 0.5 });
expect(new Date().valueOf() - t).toBeGreaterThan(90);
});
it("kills in non-bash mode if timeout reached", async () => {
try {
await (0, execute_code_1.executeCode)({
command: "sh",
args: ["-c", "sleep 5"],
bash: false,
timeout: 0.1,
});
expect(false).toBe(true);
}
catch (err) {
expect(err).toContain("killed command");
}
});
});
//# sourceMappingURL=execute-code.test.js.map