-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathshell.test.ts
More file actions
63 lines (54 loc) · 1.55 KB
/
Copy pathshell.test.ts
File metadata and controls
63 lines (54 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { expect } from "chai";
import fs from "fs";
import { shell, ShellError } from "../../src/utils/shell.js";
describe("shell utility", () => {
const scriptPath = "test-script.sh";
const scriptData = `
#!/bin/bash
sleep .01
echo "hello"
`.trim();
before("Write test script", () => {
fs.writeFileSync(scriptPath, scriptData);
fs.chmodSync(scriptPath, "0755"); // +x
});
it("Execute a command without crashing", async () => {
// Check that the output is correct
const output = await shell(`sh ${scriptPath}`);
expect(output).to.equal("hello");
// Check that it errors on timeout
const errorMessage = await shell(`sh ${scriptPath}`, { timeout: 1 }).catch(
e => e.message
);
expect(errorMessage).to.include("TIMEOUT");
});
it("Show a rich typed error", async () => {
const cmd = "cat does-not-exist";
const error: ShellError | null = await shell(cmd)
.then(() => null)
.catch(e => e);
if (!error) throw Error("Command did not throw");
expect(error.message).to.equal(
`Command failed: cat does-not-exist
cat: does-not-exist: No such file or directory
stdout:
stderr: cat: does-not-exist: No such file or directory
`,
"wrong error.message"
);
expect({
cmd: error.cmd,
code: error.code,
stdout: error.stdout,
stderr: error.stderr
}).to.deep.equal({
cmd: "cat does-not-exist",
code: 1,
stdout: "",
stderr: "cat: does-not-exist: No such file or directory\n"
});
});
after(() => {
fs.unlinkSync(scriptPath);
});
});