mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
* fix: emit close on stdin stream end We where not emitting the `close` event when done reading stdin data. It took me way too long to find this but i kept searching in zig/cpp code of the stream implementations... Fixes: https://github.com/oven-sh/bun/issues/6713 * fix: don't emit close twice
158 lines
4.3 KiB
TypeScript
158 lines
4.3 KiB
TypeScript
import { spawn, spawnSync } from "bun";
|
|
import { describe, expect, it, test } from "bun:test";
|
|
import { bunExe } from "harness";
|
|
import { isatty } from "tty";
|
|
|
|
test("process.stdin", () => {
|
|
expect(process.stdin).toBeDefined();
|
|
expect(process.stdin.isTTY).toBe(isatty(0) ? true : undefined);
|
|
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.stdin - close(#6713)", async () => {
|
|
const { stdin, stdout } = spawn({
|
|
cmd: [bunExe(), import.meta.dir + "/process-stdin-echo.js", "close-event"],
|
|
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-CLOSE");
|
|
});
|
|
|
|
test("process.stdout", () => {
|
|
expect(process.stdout).toBeDefined();
|
|
expect(process.stdout.isTTY).toBe(isatty(1));
|
|
});
|
|
|
|
test("process.stderr", () => {
|
|
expect(process.stderr).toBeDefined();
|
|
expect(process.stderr.isTTY).toBe(isatty(2));
|
|
});
|
|
|
|
test("process.stdout - write", () => {
|
|
const { stdout } = spawnSync({
|
|
cmd: [bunExe(), import.meta.dir + "/stdio-test-instance.js"],
|
|
stdout: "pipe",
|
|
stdin: null,
|
|
stderr: null,
|
|
env: {
|
|
...process.env,
|
|
BUN_DEBUG_QUIET_LOGS: "1",
|
|
},
|
|
});
|
|
|
|
expect(stdout?.toString()).toBe(`hello worldhello again|😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌`);
|
|
});
|
|
|
|
test("process.stdout - write a lot (string)", () => {
|
|
const { stdout } = spawnSync({
|
|
cmd: [bunExe(), import.meta.dir + "/stdio-test-instance-a-lot.js"],
|
|
stdout: "pipe",
|
|
stdin: null,
|
|
stderr: null,
|
|
env: {
|
|
...process.env,
|
|
BUN_DEBUG_QUIET_LOGS: "1",
|
|
TEST_STDIO_STRING: "1",
|
|
},
|
|
});
|
|
|
|
expect(stdout?.toString()).toBe(
|
|
`hello worldhello again|😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌`.repeat(9999),
|
|
);
|
|
});
|
|
|
|
test("process.stdout - write a lot (bytes)", () => {
|
|
const { stdout } = spawnSync({
|
|
cmd: [bunExe(), import.meta.dir + "/stdio-test-instance-a-lot.js"],
|
|
stdout: "pipe",
|
|
stdin: null,
|
|
stderr: null,
|
|
env: {
|
|
...process.env,
|
|
BUN_DEBUG_QUIET_LOGS: "1",
|
|
},
|
|
});
|
|
expect(stdout?.toString()).toBe(
|
|
`hello worldhello again|😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌`.repeat(9999),
|
|
);
|
|
});
|