aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/child-process-stdio.test.js
blob: 5e295b2ca5ab9603f89aedb75e8e4fc8f7abe80b (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
import { describe, it, expect, beforeAll } from "bun:test";
import { spawn, execSync } from "node:child_process";
import { bunExe } from "bunExe";

const CHILD_PROCESS_FILE = import.meta.dir + "/spawned-child.js";
const OUT_FILE = import.meta.dir + "/stdio-test-out.txt";

describe("process.stdout", () => {
  it("should allow us to write to it", (done) => {
    const child = spawn(bunExe(), [CHILD_PROCESS_FILE, "STDOUT"]);
    child.stdout.setEncoding("utf8");
    child.stdout.on("data", (data) => {
      try {
        expect(data).toBe("stdout_test");
        done();
      } catch (err) {
        done(err);
      }
    });
  });
});

describe("process.stdin", () => {
  it("should allow us to read from stdin in readable mode", (done) => {
    const input = "hello\n";
    // Child should read from stdin and write it back
    const child = spawn(bunExe(), [CHILD_PROCESS_FILE, "STDIN", "READABLE"]);
    let data = "";
    child.stdout.setEncoding("utf8");
    child.stdout.on("data", (chunk) => {
      data += chunk;
    }).on("end", function() {
      try {
        expect(data).toBe(`data: ${input}`);
        done();
      } catch (err) {
        done(err);
      }
    });
    child.stdin.write(input);
    child.stdin.end();
  });

  it("should allow us to read from stdin via flowing mode", (done) => {
    const input = "hello\n";
    // Child should read from stdin and write it back
    const child = spawn(bunExe(), [CHILD_PROCESS_FILE, "STDIN", "FLOWING"]);
    let data = "";
    child.stdout.setEncoding("utf8");
    child.stdout.on("readable", () => {
      let chunk;
      while ((chunk = child.stdout.read()) !== null) {
        data += chunk;
      }
    }).on("end", function() {
      try {
        expect(data).toBe(`data: ${input}`);
        done();
      } catch (err) {
        done(err);
      }
    });
    child.stdin.write(input);
    child.stdin.end();
  });

  it("should allow us to read > 65kb from stdin", (done) => {
    const numReps = Math.ceil((66 * 1024) / 5);
    const input = "hello".repeat(numReps);
    // Child should read from stdin and write it back
    const child = spawn(bunExe(), [CHILD_PROCESS_FILE, "STDIN", "FLOWING"]);
    let data = "";
    child.stdout.setEncoding("utf8");
    child.stdout.on("readable", () => {
      let chunk;
      while ((chunk = child.stdout.read()) !== null) {
        data += chunk;
      }
    }).on("end", function() {
      try {
        expect(data).toBe(`data: ${input}`);
        done();
      } catch (err) {
        done(err);
      }
    });
    child.stdin.write(input);
    child.stdin.end();
  });

  it("should allow us to read from a file", () => {
    const result = execSync(
      `${bunExe()} ${CHILD_PROCESS_FILE} STDIN FLOWING < ${
        import.meta.dir
      }/readFileSync.txt`,
      { encoding: "utf8" },
    );
    expect(result).toEqual("data: File read successfully");
  });
});