diff options
author | 2022-12-17 06:40:41 +0200 | |
---|---|---|
committer | 2022-12-16 20:40:41 -0800 | |
commit | 68fd43313de13e58a405b4dde530471420aa784a (patch) | |
tree | 2b7fb89aafd428f7722edbfc444f756db4e10c27 /test | |
parent | b06ca07eee8783ac4fd923ade8fc12f1db2ff068 (diff) | |
download | bun-68fd43313de13e58a405b4dde530471420aa784a.tar.gz bun-68fd43313de13e58a405b4dde530471420aa784a.tar.zst bun-68fd43313de13e58a405b4dde530471420aa784a.zip |
add tests for `process.stdin` (#1621)
Diffstat (limited to 'test')
-rw-r--r-- | test/bun.js/process-stdin-echo.js | 11 | ||||
-rw-r--r-- | test/bun.js/process-stdio.test.ts | 71 |
2 files changed, 81 insertions, 1 deletions
diff --git a/test/bun.js/process-stdin-echo.js b/test/bun.js/process-stdin-echo.js new file mode 100644 index 000000000..63133d654 --- /dev/null +++ b/test/bun.js/process-stdin-echo.js @@ -0,0 +1,11 @@ +process.stdin.setEncoding("utf8"); +process.stdin.on("data", (data) => { + process.stdout.write(data); +}); +process.stdin.once("end", () => { + process.stdout.write("ENDED"); +}); +if (process.argv[2] == "resume") { + process.stdout.write("RESUMED"); + process.stdin.resume(); +} diff --git a/test/bun.js/process-stdio.test.ts b/test/bun.js/process-stdio.test.ts index 45db37d05..df82f5791 100644 --- a/test/bun.js/process-stdio.test.ts +++ b/test/bun.js/process-stdio.test.ts @@ -1,14 +1,83 @@ -import { spawnSync } from "bun"; +import { spawn, spawnSync } from "bun"; import { describe, expect, it, test } from "bun:test"; import { bunExe } from "bunExe"; import { isatty } from "tty"; test("process.stdin", () => { expect(process.stdin).toBeDefined(); + expect(process.stdout.isTTY).toBe(isatty(0)); expect(process.stdin.on("close", function() {})).toBe(process.stdin); expect(process.stdin.once("end", function() {})).toBe(process.stdin); }); +test("process.stdin - read", async () => { + const { stdin, stdout } = spawn({ + cmd: [bunExe(), import.meta.dir + "/process-stdin-echo.js"], + stdout: "pipe", + stdin: "pipe", + stderr: null, + env: { + ...process.env, + BUN_DEBUG_QUIET_LOGS: "1", + }, + }); + expect(stdin).toBeDefined(); + expect(stdout).toBeDefined(); + var lines = [ + "Get Emoji", + "— All Emojis to ✂️ Copy and 📋 Paste", + "👌", + "", + ]; + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + setTimeout(() => { + if (line) { + stdin?.write(line + "\n"); + stdin?.flush(); + } else { + stdin?.end(); + } + }, i * 200); + } + var text = await new Response(stdout).text(); + expect(text).toBe(lines.join("\n") + "ENDED"); +}); + +test("process.stdin - resume", async () => { + const { stdin, stdout } = spawn({ + cmd: [bunExe(), import.meta.dir + "/process-stdin-echo.js", "resume"], + stdout: "pipe", + stdin: "pipe", + stderr: null, + env: { + ...process.env, + BUN_DEBUG_QUIET_LOGS: "1", + }, + }); + expect(stdin).toBeDefined(); + expect(stdout).toBeDefined(); + var lines = [ + "Get Emoji", + "— All Emojis to ✂️ Copy and 📋 Paste", + "👌", + "", + ]; + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + setTimeout(() => { + if (line) { + stdin?.write(line + "\n"); + stdin?.flush(); + } else { + stdin?.end(); + } + }, i * 200); + } + var text = await new Response(stdout).text(); + expect(text).toBe("RESUMED" + lines.join("\n") + "ENDED"); +}); + test("process.stdout", () => { expect(process.stdout).toBeDefined(); expect(process.stdout.isTTY).toBe(isatty(1)); |