Files
bun.sh/test/js/node/process/process-stdio.test.ts
Jarred Sumner 76ced7c6ed WIP Make process.stdout sync on windows (#9398)
* Make some things sync on windows

* WIP

* WIP

* remove uses to uv_default_loop

* remove a compiler warning on windows

* edfghjk

* Windows build fixes

* Fixup

* bundows

* Add quotes

* Fix --cwd arg on Windows

* comment

* move this up

* Fix some tests

* `mv` tests pass

* spawn.test passes again

* Allow .sh file extension for Windows

* Unmark failing tests

* env test pass

* windows

* Fix some tests

* Update ProcessBindingTTYWrap.cpp

* Update CMakeLists.txt

* Set tmpdir

* Make it 5s on windows

* Fixup

* Fixup

* Update ProcessBindingTTYWrap.cpp

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: dave caruso <me@paperdave.net>
2024-03-18 19:35:34 -07:00

160 lines
4.4 KiB
TypeScript

import { spawn, spawnSync } from "bun";
import { describe, expect, it, test } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { isatty } from "tty";
import path from "path";
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);
});
const files = {
echo: path.join(import.meta.dir, "process-stdin-echo.js"),
};
test("process.stdin - read", async () => {
const { stdin, stdout } = spawn({
cmd: [bunExe(), files.echo],
stdout: "pipe",
stdin: "pipe",
stderr: "inherit",
env: {
...bunEnv,
},
});
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(), files.echo, "resume"],
stdout: "pipe",
stdin: "pipe",
stderr: null,
env: bunEnv,
});
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(), files.echo, "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();
// isTTY returns true or undefined in Node.js
expect(process.stdout.isTTY).toBe((isatty(1) || undefined) as any);
});
test("process.stderr", () => {
expect(process.stderr).toBeDefined();
// isTTY returns true or undefined in Node.js
expect(process.stderr.isTTY).toBe((isatty(2) || undefined) as any);
});
test("process.stdout - write", () => {
const { stdout } = spawnSync({
cmd: [bunExe(), path.join(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(), path.join(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(), path.join(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),
);
});