aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/child_process.test.ts
blob: 7c1315baf0cc282c6e9d61a0dd60b6235c5fbd49 (plain) (blame)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { describe, it as it_, expect as expect_ } from "bun:test";
import { gcTick } from "gc";
import { ChildProcess, spawn, execFile, exec, fork, spawnSync, execFileSync, execSync } from "node:child_process";
import { tmpdir } from "node:os";

const expect: typeof expect_ = (actual: unknown) => {
  gcTick();
  const ret = expect_(actual);
  gcTick();
  return ret;
};

const it: typeof it_ = (label, fn) => {
  const hasDone = fn.length === 1;
  if (fn.constructor.name === "AsyncFunction" && hasDone) {
    return it_(label, async done => {
      gcTick();
      await fn(done);
      gcTick();
    });
  } else if (hasDone) {
    return it_(label, done => {
      gcTick();
      fn(done);
      gcTick();
    });
  } else if (fn.constructor.name === "AsyncFunction") {
    return it_(label, async () => {
      gcTick();
      await fn(() => {});
      gcTick();
    });
  } else {
    return it_(label, () => {
      gcTick();
      fn(() => {});
      gcTick();
    });
  }
};

const debug = process.env.DEBUG ? console.log : () => {};

const platformTmpDir = require("fs").realpathSync(tmpdir());

// Semver regex: https://gist.github.com/jhorsman/62eeea161a13b80e39f5249281e17c39?permalink_comment_id=2896416#gistcomment-2896416
// Not 100% accurate, but good enough for this test
const SEMVER_REGEX =
  /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z\d][-a-zA-Z.\d]*)?(\+[a-zA-Z\d][-a-zA-Z.\d]*)?$/;

describe("ChildProcess.spawn()", () => {
  it("should emit `spawn` on spawn", async () => {
    const proc = new ChildProcess();
    const result = await new Promise(resolve => {
      proc.on("spawn", () => {
        resolve(true);
      });
      proc.spawn({ file: "bun", args: ["bun", "-v"] });
    });
    expect(result).toBe(true);
  });

  it("should emit `exit` when killed", async () => {
    const proc = new ChildProcess();
    const result = await new Promise(resolve => {
      proc.on("exit", () => {
        resolve(true);
      });

      proc.spawn({ file: "bun", args: ["bun", "-v"] });
      proc.kill();
    });
    expect(result).toBe(true);
  });
});

describe("spawn()", () => {
  it("should spawn a process", () => {
    const child = spawn("echo", ["hello"]);
    expect(!!child).toBe(true);
  });

  it("should disallow invalid filename", () => {
    let child;
    let child2;
    try {
      // @ts-ignore
      child = spawn(123);
      // @ts-ignore
      child2 = spawn(["echo", "hello"]);
    } catch (e) {}
    expect(!!child).toBe(false);
    expect(!!child2).toBe(false);
  });

  it("should allow stdout to be read via Node stream.Readable `data` events", async () => {
    const child = spawn("bun", ["-v"]);
    const result: string = await new Promise(resolve => {
      child.stdout.on("error", e => {
        console.error(e);
      });
      child.stdout.on("data", data => {
        debug(`stdout: ${data}`);
        resolve(data.toString());
      });
      child.stderr.on("data", data => {
        debug(`stderr: ${data}`);
      });
    });
    expect(SEMVER_REGEX.test(result.trim())).toBe(true);
  });

  it("should allow stdout to be read via .read() API", async done => {
    const child = spawn("bun", ["-v"]);
    const result: string = await new Promise(resolve => {
      let finalData = "";
      child.stdout.on("error", e => {
        done(e);
      });
      child.stdout.on("readable", () => {
        let data;

        while ((data = child.stdout.read()) !== null) {
          finalData += data.toString();
        }
        resolve(finalData);
      });
    });
    expect(SEMVER_REGEX.test(result.trim())).toBe(true);
    done();
  });

  it("should accept stdio option with 'ignore' for no stdio fds", async () => {
    const child1 = spawn("bun", ["-v"], {
      stdio: "ignore",
    });
    const child2 = spawn("bun", ["-v"], {
      stdio: ["ignore", "ignore", "ignore"],
    });

    expect(!!child1).toBe(true);
    expect(child1.stdin).toBe(null);
    expect(child1.stdout).toBe(null);
    expect(child1.stderr).toBe(null);

    expect(!!child2).toBe(true);
    expect(child2.stdin).toBe(null);
    expect(child2.stdout).toBe(null);
    expect(child2.stderr).toBe(null);
  });

  it("should allow us to set cwd", async () => {
    const child = spawn("pwd", { cwd: platformTmpDir });
    const result: string = await new Promise(resolve => {
      child.stdout.on("data", data => {
        resolve(data.toString());
      });
    });
    expect(result.trim()).toBe(platformTmpDir);
  });

  it("should allow us to write to stdin", async () => {
    const child = spawn("tee");
    const result: string = await new Promise(resolve => {
      child.stdin.write("hello");
      child.stdout.on("data", data => {
        resolve(data.toString());
      });
    });
    expect(result.trim()).toBe("hello");
  });

  it("should allow us to timeout hanging processes", async () => {
    const child = spawn("sleep", ["2"], { timeout: 3 });
    const start = performance.now();
    let end;
    await new Promise(resolve => {
      child.on("exit", () => {
        end = performance.now();
        resolve(true);
      });
    });
    expect(end - start < 2000).toBe(true);
  });

  it("should allow us to set env", async () => {
    const child = spawn("env", { env: { TEST: "test" } });
    const result: string = await new Promise(resolve => {
      child.stdout.on("data", data => {
        resolve(data.toString());
      });
    });
    expect(/TEST\=test/.test(result)).toBe(true);
  });

  it("should allow explicit setting of argv0", async () => {
    var resolve;
    const promise = new Promise<string>(resolve1 => {
      resolve = resolve1;
    });
    process.env.NO_COLOR = "1";
    const child = spawn("node", ["--help"], { argv0: "bun" });
    delete process.env.NO_COLOR;
    let msg = "";

    child.stdout.on("data", data => {
      msg += data.toString();
    });

    child.stdout.on("close", () => {
      resolve(msg);
    });

    const result = await promise;
    expect(/Open bun's Discord server/.test(result)).toBe(true);
  });

  it("should allow us to spawn in a shell", async () => {
    const result1: string = await new Promise(resolve => {
      const child1 = spawn("echo", ["$0"], { shell: true });
      child1.stdout.on("data", data => {
        resolve(data.toString());
      });
    });
    const result2: string = await new Promise(resolve => {
      const child2 = spawn("echo", ["$0"], { shell: "bash" });
      child2.stdout.on("data", data => {
        resolve(data.toString());
      });
    });
    expect(result1.trim()).toBe(Bun.which("sh"));
    expect(result2.trim()).toBe(Bun.which("bash"));
  });
  it("should spawn a process synchronously", () => {
    const { stdout } = spawnSync("echo", ["hello"], { encoding: "utf8" });
    expect(stdout.trim()).toBe("hello");
  });
});

describe("execFile()", () => {
  it("should execute a file", async () => {
    const result: Buffer = await new Promise((resolve, reject) => {
      execFile("bun", ["-v"], { encoding: "buffer" }, (error, stdout, stderr) => {
        if (error) {
          reject(error);
        }
        resolve(stdout);
      });
    });
    expect(SEMVER_REGEX.test(result.toString().trim())).toBe(true);
  });
});

describe("exec()", () => {
  it("should execute a command in a shell", async () => {
    const result: Buffer = await new Promise((resolve, reject) => {
      exec("bun -v", { encoding: "buffer" }, (error, stdout, stderr) => {
        if (error) {
          reject(error);
        }
        resolve(stdout);
      });
    });
    expect(SEMVER_REGEX.test(result.toString().trim())).toBe(true);
  });
});

describe("fork()", () => {
  it("should throw an error when used", () => {
    let err;
    try {
      fork("index.js");
    } catch (e) {
      err = e;
    }
    expect(err instanceof Error).toBe(true);
  });
});

describe("spawnSync()", () => {
  it("should spawn a process synchronously", () => {
    const { stdout } = spawnSync("echo", ["hello"], { encoding: "utf8" });
    expect(stdout.trim()).toBe("hello");
  });
});

describe("execFileSync()", () => {
  it("should execute a file synchronously", () => {
    const result = execFileSync("bun", ["-v"], { encoding: "utf8" });
    expect(SEMVER_REGEX.test(result.trim())).toBe(true);
  });

  it("should allow us to pass input to the command", () => {
    const result = execFileSync("node", [import.meta.dir + "/spawned-child.js", "STDIN"], {
      input: "hello world!",
      encoding: "utf8",
    });
    expect(result.trim()).toBe("data: hello world!");
  });
});

describe("execSync()", () => {
  it("should execute a command in the shell synchronously", () => {
    const result = execSync("bun -v", { encoding: "utf8" });
    expect(SEMVER_REGEX.test(result.trim())).toBe(true);
  });
});

describe("Bun.spawn()", () => {
  it("should return exit code 0 on successful execution", async () => {
    const proc = Bun.spawn({
      cmd: ["echo", "hello"],
      stdout: "pipe",
    });

    for await (const chunk of proc.stdout!) {
      const text = new TextDecoder().decode(chunk);
      expect(text.trim()).toBe("hello");
    }

    const result = await new Promise(resolve => {
      const maybeExited = Bun.peek(proc.exited);
      if (maybeExited === proc.exited) {
        proc.exited.then(code => resolve(code));
      } else {
        resolve(maybeExited);
      }
    });
    expect(result).toBe(0);
  });
  // it("should fail when given an invalid cwd", () => {
  //   const child = Bun.spawn({ cmd: ["echo", "hello"], cwd: "/invalid" });
  //   expect(child.pid).toBe(undefined);
  // });
});