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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
import { readableStreamToText, spawn, spawnSync, write } from "bun";
import { describe, expect, it } from "bun:test";
import { gcTick as _gcTick } from "gc";
import { rmdirSync, unlinkSync, rmSync, writeFileSync } from "node:fs";
for (let [gcTick, label] of [
[_gcTick, "gcTick"],
[() => {}, "no gc tick"],
]) {
describe(label, () => {
describe("spawnSync", () => {
const hugeString = "hello".repeat(10000).slice();
it("as an array", () => {
const { stdout } = spawnSync(["echo", "hi"]);
// stdout is a Buffer
const text = stdout.toString();
expect(text).toBe("hi\n");
});
it("Uint8Array works as stdin", async () => {
const { stdout, stderr } = spawnSync({
cmd: ["cat"],
stdin: new TextEncoder().encode(hugeString),
});
expect(stdout.toString()).toBe(hugeString);
expect(stderr.byteLength).toBe(0);
});
});
describe("spawn", () => {
const hugeString = "hello".repeat(10000).slice();
it("as an array", async () => {
const { stdout, exited } = spawn(["echo", "hello"], {
stdout: "pipe",
});
gcTick();
expect(await new Response(stdout).text()).toBe("hello\n");
});
it("as an array with options object", async () => {
const { stdout } = spawn(["printenv", "FOO"], {
cwd: "/tmp",
env: {
...process.env,
FOO: "bar",
},
stdin: null,
stdout: "pipe",
stderr: "inherit",
});
const text = await new Response(stdout).text();
expect(text).toBe("bar\n");
});
it("Uint8Array works as stdin", async () => {
rmSync("/tmp/out.123.txt", { force: true });
gcTick();
const { exited } = spawn({
cmd: ["cat"],
stdin: new TextEncoder().encode(hugeString),
stdout: Bun.file("/tmp/out.123.txt"),
});
await exited;
expect(await Bun.file("/tmp/out.123.txt").text()).toBe(hugeString);
});
it("Blob works as stdin", async () => {
rmSync("/tmp/out.123.txt", { force: true });
gcTick();
const { exited } = spawn({
cmd: ["cat"],
stdin: new Blob([new TextEncoder().encode(hugeString)]),
stdout: Bun.file("/tmp/out.123.txt"),
});
await exited;
expect(await Bun.file("/tmp/out.123.txt").text()).toBe(hugeString);
});
it("Bun.file() works as stdout", async () => {
rmSync("/tmp/out.123.txt", { force: true });
gcTick();
const { exited } = spawn({
cmd: ["echo", "hello"],
stdout: Bun.file("/tmp/out.123.txt"),
});
await exited;
gcTick();
expect(await Bun.file("/tmp/out.123.txt").text()).toBe("hello\n");
});
it("Bun.file() works as stdin", async () => {
await write(Bun.file("/tmp/out.456.txt"), "hello there!");
gcTick();
const { stdout } = spawn({
cmd: ["cat"],
stdout: "pipe",
stdin: Bun.file("/tmp/out.456.txt"),
});
gcTick();
expect(await readableStreamToText(stdout)).toBe("hello there!");
});
it("Bun.file() works as stdin and stdout", async () => {
writeFileSync("/tmp/out.456.txt", "hello!");
gcTick();
writeFileSync("/tmp/out.123.txt", "wrong!");
gcTick();
const { exited } = spawn({
cmd: ["cat"],
stdout: Bun.file("/tmp/out.123.txt"),
stdin: Bun.file("/tmp/out.456.txt"),
});
gcTick();
await exited;
expect(await Bun.file("/tmp/out.456.txt").text()).toBe("hello!");
gcTick();
expect(await Bun.file("/tmp/out.123.txt").text()).toBe("hello!");
});
it("stdout can be read", async () => {
await Bun.write("/tmp/out.txt", hugeString);
gcTick();
const { stdout } = spawn({
cmd: ["cat", "/tmp/out.txt"],
stdout: "pipe",
});
gcTick();
const text = await readableStreamToText(stdout);
gcTick();
expect(text).toBe(hugeString);
});
it("stdin can be read and stdout can be written", async () => {
const proc = spawn({
cmd: ["bash", import.meta.dir + "/bash-echo.sh"],
stdout: "pipe",
stdin: "pipe",
stderr: "inherit",
});
proc.stdin.write(hugeString);
await proc.stdin.end(true);
var text = "";
var reader = proc.stdout.getReader();
var done = false;
while (!done) {
var { value, done } = await reader.read();
if (value) text += new TextDecoder().decode(value);
if (done && text.length === 0) {
reader.releaseLock();
reader = proc.stdout.getReader();
done = false;
}
}
expect(text.trim().length).toBe(hugeString.length);
expect(text.trim()).toBe(hugeString);
gcTick();
await proc.exited;
});
describe("pipe", () => {
function huge() {
return spawn({
cmd: ["echo", hugeString],
stdout: "pipe",
stdin: "pipe",
stderr: "inherit",
});
}
function helloWorld() {
return spawn({
cmd: ["echo", "hello"],
stdout: "pipe",
stdin: "pipe",
});
}
const fixtures = [
[helloWorld, "hello"],
[huge, hugeString],
];
for (const [callback, fixture] of fixtures) {
describe(fixture.slice(0, 12), () => {
describe("should should allow reading stdout", () => {
it("before exit", async () => {
const process = callback();
const output = await readableStreamToText(process.stdout);
const expected = fixture + "\n";
expect(output.length).toBe(expected.length);
expect(output).toBe(expected);
await process.exited;
});
it("before exit (chunked)", async () => {
const process = callback();
var output = "";
var reader = process.stdout.getReader();
var done = false;
while (!done) {
var { value, done } = await reader.read();
if (value) output += new TextDecoder().decode(value);
}
const expected = fixture + "\n";
expect(output.length).toBe(expected.length);
expect(output).toBe(expected);
await process.exited;
});
it("after exit", async () => {
const process = callback();
await process.stdin.end();
const output = await readableStreamToText(process.stdout);
const expected = fixture + "\n";
expect(output.length).toBe(expected.length);
expect(output).toBe(expected);
await process.exited;
});
});
});
}
});
});
});
}
|