Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/backend/bash.test.ts
1447 views
1
import bash from "@cocalc/backend/bash";
2
import { once } from "@cocalc/util/async-utils";
3
4
describe("test the bash child process spawner", () => {
5
it("echos 'hi'", async () => {
6
const child = await bash("echo 'hi'");
7
let out = "";
8
child.stdout.on("data", (data) => {
9
out += data.toString();
10
});
11
await once(child, "exit");
12
expect(out).toBe("hi\n");
13
});
14
15
it("runs a multiline bash script", async () => {
16
const child = await bash(`
17
sum=0
18
for i in {1..100}; do
19
sum=$((sum + i))
20
done
21
echo $sum
22
`);
23
let out = "";
24
child.stdout.on("data", (data) => {
25
out += data.toString();
26
});
27
await once(child, "exit");
28
expect(out).toBe("5050\n");
29
});
30
31
it("runs a bash script with ulimit to limit execution time", async () => {
32
const child = await bash(`
33
ulimit -t 1
34
while : ; do : ; done # infinite CPU
35
`);
36
const x = await once(child, "exit");
37
expect(x[1]).toBe("SIGKILL");
38
});
39
});
40
41