Compare commits

...

4 Commits

Author SHA1 Message Date
Jarred Sumner
07df10d088 Merge branch 'main' into jarred/console-iter 2024-03-27 21:48:54 -07:00
Jarred Sumner
07e8cdb103 Merge branch 'main' into jarred/console-iter 2024-03-27 16:52:28 -07:00
Jarred Sumner
17a04cdc4b Merge branch 'main' into jarred/console-iter 2024-03-26 13:20:17 -07:00
Jarred Sumner
814178cfc0 Don't pass kill(0) in console iterator test 2024-03-25 15:18:42 -07:00

View File

@@ -1,8 +1,10 @@
import { spawnSync, spawn } from "bun";
import { describe, expect, it } from "bun:test";
import { bunEnv, bunExe } from "harness";
import path from "path";
const dir = (out: string) => path.join(import.meta.dir, out);
describe("should work for static input", () => {
it("should work for static input", async () => {
const inputs = [
"hello world",
"hello world\n",
@@ -12,20 +14,19 @@ describe("should work for static input", () => {
"1",
"💕 Red Heart ✨ Sparkles 🔥 Fire\n💕 Red Heart ✨ Sparkles\n💕 Red Heart\n💕\n\nnormal",
];
for (let input of inputs) {
it(input.replaceAll("\n", "\\n"), () => {
const { stdout } = spawnSync({
cmd: [bunExe(), import.meta.dir + "/" + "console-iterator-run.ts"],
stdin: Buffer.from(input),
env: bunEnv,
});
expect(stdout.toString()).toBe(input.replaceAll("\n", ""));
async function run(input: string) {
const { stdout, exited } = spawn({
cmd: [bunExe(), dir("console-iterator-run.ts")],
stdin: Buffer.from(input),
env: bunEnv,
});
expect(await new Response(stdout).text()).toBe(input.replaceAll("\n", ""));
expect(await exited).toBe(0);
}
await Promise.all(inputs.map(run));
});
describe("should work for streaming input", () => {
it("should work for streaming input", async () => {
const inputs = [
"hello world",
"hello world\n",
@@ -35,32 +36,31 @@ describe("should work for streaming input", () => {
"1",
"💕 Red Heart ✨ Sparkles 🔥 Fire\n 💕 Red Heart ✨ Sparkles\n 💕 Red Heart\n 💕 \n\nnormal",
];
for (let input of inputs) {
it(input.replaceAll("\n", "\\n"), async () => {
const proc = spawn({
cmd: [bunExe(), import.meta.dir + "/" + "console-iterator-run.ts"],
stdin: "pipe",
stdout: "pipe",
env: bunEnv,
});
const { stdout, stdin } = proc;
stdin.write(input.slice(0, (input.length / 2) | 0));
stdin.flush();
await new Promise(resolve => setTimeout(resolve, 1));
stdin.write(input.slice((input.length / 2) | 0));
await stdin.end();
expect(await new Response(stdout).text()).toBe(input.replaceAll("\n", ""));
proc.kill(0);
async function run(input: string) {
const proc = spawn({
cmd: [bunExe(), dir("console-iterator-run.ts")],
stdin: "pipe",
stdout: "pipe",
env: bunEnv,
});
const { stdout, stdin } = proc;
stdin.write(input.slice(0, (input.length / 2) | 0));
stdin.flush();
await new Promise(resolve => setTimeout(resolve, 1));
stdin.write(input.slice((input.length / 2) | 0));
await stdin.end();
expect(await new Response(stdout).text()).toBe(input.replaceAll("\n", ""));
proc.kill();
}
await Promise.all(inputs.map(run));
});
// https://github.com/oven-sh/bun/issues/5175
it("can use the console iterator more than once", async () => {
const proc = spawn({
cmd: [bunExe(), import.meta.dir + "/" + "console-iterator-run-2.ts"],
cmd: [bunExe(), dir("console-iterator-run-2.ts")],
stdin: "pipe",
stdout: "pipe",
env: bunEnv,
@@ -70,5 +70,5 @@ it("can use the console iterator more than once", async () => {
await stdin.end();
expect(await new Response(stdout).text()).toBe('["hello","world"]["another"]');
proc.kill(0);
proc.kill();
});