Repository navigation
Expand file tree
/
Copy pathchild-process-exec.test.ts
More file actions
119 lines (108 loc) · 4.27 KB
/
Copy pathchild-process-exec.test.ts
File metadata and controls
119 lines (108 loc) · 4.27 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { describe, expect, test } from "bun:test";
import { bunExe, isWindows } from "harness";
import { exec } from "node:child_process";
const SIZE = 262145;
// https://github.com/oven-sh/bun/issues/5319
describe.concurrent("child_process.exec", () => {
const shell = Bun.which(isWindows ? "powershell" : "bash");
describe.each(["stdout", "stderr"])("%s", io => {
let script;
if (isWindows) {
if (io === "stdout") {
script = `[Console]::Out.Write.Invoke('=' * ${SIZE})`;
} else {
script = `[Console]::Error.Write.Invoke('=' * ${SIZE})`;
}
} else {
if (io === "stdout") {
script = `printf '=%.0s' {1..${SIZE}}`;
} else {
script = `printf '=%.0s' {1..${SIZE}} 1>&2`;
}
}
test("no encoding", async () => {
const { resolve, reject, promise } = Promise.withResolvers();
exec(script, { maxBuffer: 1024 * 1024 * 10, encoding: "buffer", shell }, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({ stdout, stderr });
}
});
const { stdout, stderr } = await promise;
const out = io === "stdout" ? stdout : stderr;
const other = io === "stdout" ? stderr : stdout;
expect(out).toHaveLength(SIZE);
expect(out).toBeInstanceOf(Buffer);
expect(other).toEqual(Buffer.alloc(0));
});
test("Infinity maxBuffer", async () => {
const { resolve, reject, promise } = Promise.withResolvers();
exec(script, { maxBuffer: Infinity, shell }, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({ stdout, stderr });
}
});
const { stdout, stderr } = await promise;
const out = io === "stdout" ? stdout : stderr;
const other = io === "stdout" ? stderr : stdout;
expect(out).toHaveLength(SIZE);
expect(other).toBe("");
});
test("large output", async () => {
const { resolve, reject, promise } = Promise.withResolvers();
exec(script, { maxBuffer: 1024 * 1024 * 10, shell }, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({ stdout, stderr });
}
});
const { stdout, stderr } = await promise;
const out = io === "stdout" ? stdout : stderr;
const other = io === "stdout" ? stderr : stdout;
expect(out).toHaveLength(SIZE);
expect(other).toBe("");
});
test("exceeding maxBuffer should throw", async () => {
const { resolve, reject, promise } = Promise.withResolvers();
exec(script, { maxBuffer: 1024 * 100, shell }, (err, stdout, stderr) => {
resolve({ stdout, stderr, err });
});
const { stdout, stderr, err } = await promise;
expect(err.message).toContain("maxBuffer length exceeded");
expect(err.message).toContain(io);
const out = io === "stdout" ? stdout : stderr;
const other = io === "stdout" ? stderr : stdout;
expect(out.trim()).toHaveLength(1024 * 100);
expect(other).toBe("");
});
test("exceeding maxBuffer should truncate output length", async () => {
const { resolve, reject, promise } = Promise.withResolvers();
exec(script, { maxBuffer: 1024 * 255 - 1, shell }, (err, stdout, stderr) => {
resolve({ stdout, stderr, err });
});
const { stdout, stderr, err } = await promise;
expect(err.message).toContain("maxBuffer length exceeded");
expect(err.message).toContain(io);
const out = (io === "stdout" ? stdout : stderr).trim();
const other = (io === "stdout" ? stderr : stdout).trim();
expect(out.length).toBeLessThanOrEqual(1024 * 255 - 1);
expect(out.length).toBeGreaterThan(1024 * 100);
expect(other).toBe("");
});
});
});
test.concurrent("exec with verbatim arguments", async () => {
const { resolve, reject, promise } = Promise.withResolvers();
const fixture = require.resolve("./fixtures/child-process-echo-argv.js");
const child = exec(`${bunExe()} ${fixture} tasklist /FI "IMAGENAME eq chrome.exe"`, (err, stdout, stderr) => {
if (err) return reject(err);
return resolve({ stdout, stderr });
});
expect(!!child).toBe(true);
const { stdout } = await promise;
expect(stdout.trim().split("\n")).toEqual([`tasklist`, `/FI`, `IMAGENAME eq chrome.exe`]);
});