From 9976e3f528bae74667a2f4742c4b165b7f68c76d Mon Sep 17 00:00:00 2001 From: dave caruso Date: Thu, 14 Sep 2023 00:39:36 -0400 Subject: fix(runtime): make most globals configurable/deletable, allow resuming the console iterator (#5216) * Fix #5177 * Fix #5175 * make most globals deletable/overridable * not done * cool * a * done * fix test * oops * yippee --- test/js/bun/console/console-iterator-run-2.ts | 16 ++++++ test/js/bun/console/console-iterator-run.js | 3 - test/js/bun/console/console-iterator-run.ts | 3 + test/js/bun/console/console-iterator.test.js | 63 -------------------- test/js/bun/console/console-iterator.test.ts | 82 +++++++++++++++++++++++++++ test/js/bun/deletable-globals-fixture.js | 10 ++++ test/js/bun/globals.test.js | 22 +++++++ 7 files changed, 133 insertions(+), 66 deletions(-) create mode 100644 test/js/bun/console/console-iterator-run-2.ts delete mode 100644 test/js/bun/console/console-iterator-run.js create mode 100644 test/js/bun/console/console-iterator-run.ts delete mode 100644 test/js/bun/console/console-iterator.test.js create mode 100644 test/js/bun/console/console-iterator.test.ts create mode 100644 test/js/bun/deletable-globals-fixture.js (limited to 'test') diff --git a/test/js/bun/console/console-iterator-run-2.ts b/test/js/bun/console/console-iterator-run-2.ts new file mode 100644 index 000000000..dba658821 --- /dev/null +++ b/test/js/bun/console/console-iterator-run-2.ts @@ -0,0 +1,16 @@ +async function readInput() { + let items = []; + for await (const line of console) { + if (line == "break") { + break; + } + items.push(line); + } + return items; +} + +const a = await readInput(); +console.write(JSON.stringify(a)); + +const b = await readInput(); +console.write(JSON.stringify(b)); diff --git a/test/js/bun/console/console-iterator-run.js b/test/js/bun/console/console-iterator-run.js deleted file mode 100644 index 7664c85a1..000000000 --- a/test/js/bun/console/console-iterator-run.js +++ /dev/null @@ -1,3 +0,0 @@ -for await (const line of console) { - console.write(line); -} diff --git a/test/js/bun/console/console-iterator-run.ts b/test/js/bun/console/console-iterator-run.ts new file mode 100644 index 000000000..7664c85a1 --- /dev/null +++ b/test/js/bun/console/console-iterator-run.ts @@ -0,0 +1,3 @@ +for await (const line of console) { + console.write(line); +} diff --git a/test/js/bun/console/console-iterator.test.js b/test/js/bun/console/console-iterator.test.js deleted file mode 100644 index 89894fd72..000000000 --- a/test/js/bun/console/console-iterator.test.js +++ /dev/null @@ -1,63 +0,0 @@ -import { spawnSync, spawn } from "bun"; -import { describe, expect, it } from "bun:test"; -import { bunExe } from "harness"; - -describe("should work for static input", () => { - const inputs = [ - "hello world", - "hello world\n", - "hello world\n\n", - "hello world\n\n\n", - "Hello\nWorld\n", - "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.js"], - stdin: Buffer.from(input), - env: { - BUN_DEBUG_QUIET_LOGS: "1", - }, - }); - expect(stdout.toString()).toBe(input.replaceAll("\n", "")); - }); - } -}); - -describe("should work for streaming input", () => { - const inputs = [ - "hello world", - "hello world\n", - "hello world\n\n", - "hello world\n\n\n", - "Hello\nWorld\n", - "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.js"], - stdin: "pipe", - stdout: "pipe", - env: { - BUN_DEBUG_QUIET_LOGS: "1", - }, - }); - 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)); - stdin.flush(); - stdin.end(); - - expect(await new Response(stdout).text()).toBe(input.replaceAll("\n", "")); - proc.kill(0); - }); - } -}); diff --git a/test/js/bun/console/console-iterator.test.ts b/test/js/bun/console/console-iterator.test.ts new file mode 100644 index 000000000..6c1062544 --- /dev/null +++ b/test/js/bun/console/console-iterator.test.ts @@ -0,0 +1,82 @@ +import { spawnSync, spawn } from "bun"; +import { describe, expect, it } from "bun:test"; +import { bunExe } from "harness"; + +describe("should work for static input", () => { + const inputs = [ + "hello world", + "hello world\n", + "hello world\n\n", + "hello world\n\n\n", + "Hello\nWorld\n", + "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: { + BUN_DEBUG_QUIET_LOGS: "1", + }, + }); + expect(stdout.toString()).toBe(input.replaceAll("\n", "")); + }); + } +}); + +describe("should work for streaming input", () => { + const inputs = [ + "hello world", + "hello world\n", + "hello world\n\n", + "hello world\n\n\n", + "Hello\nWorld\n", + "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: { + BUN_DEBUG_QUIET_LOGS: "1", + }, + }); + 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)); + stdin.flush(); + stdin.end(); + + expect(await new Response(stdout).text()).toBe(input.replaceAll("\n", "")); + proc.kill(0); + }); + } +}); + +// 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"], + stdin: "pipe", + stdout: "pipe", + env: { + BUN_DEBUG_QUIET_LOGS: "1", + }, + }); + const { stdout, stdin } = proc; + stdin.write("hello\nworld\nbreak\nanother\nbreak\n"); + stdin.flush(); + stdin.end(); + + expect(await new Response(stdout).text()).toBe('["hello","world"]["another"]'); + proc.kill(0); +}); diff --git a/test/js/bun/deletable-globals-fixture.js b/test/js/bun/deletable-globals-fixture.js new file mode 100644 index 000000000..b67b9ac43 --- /dev/null +++ b/test/js/bun/deletable-globals-fixture.js @@ -0,0 +1,10 @@ +const { ok, strictEqual: eql } = require("assert"); + +const globals = ["Blob", "fetch", "Headers", "Request", "Response", "setTimeout", "clearTimeout", "setInterval"]; +for (let name of globals) { + ok(delete globalThis[name]); + eql(globalThis[name], undefined); + globalThis[name] = 123; + eql(globalThis[name], 123); +} +console.log("--pass--"); diff --git a/test/js/bun/globals.test.js b/test/js/bun/globals.test.js index fd291d8bc..3dd129e6a 100644 --- a/test/js/bun/globals.test.js +++ b/test/js/bun/globals.test.js @@ -1,4 +1,6 @@ import { expect, it, describe } from "bun:test"; +import { bunEnv, bunExe } from "harness"; +import path from "path"; it("extendable", () => { const classes = [Blob, TextDecoder, TextEncoder, Request, Response, Headers, HTMLRewriter, Bun.Transpiler, Buffer]; @@ -123,3 +125,23 @@ describe("File", () => { expect(await foo.text()).toBe("foo"); }); }); + +it("globals are deletable", () => { + const { stdout, exitCode } = Bun.spawnSync({ + cmd: [bunExe(), "run", path.join(import.meta.dir, "deletable-globals-fixture.js")], + env: bunEnv, + stderr: "inherit", + }); + + expect(stdout.toString().trim().endsWith("--pass--")).toBe(true); + expect(exitCode).toBe(0); +}); + +it("self is a getter", () => { + const descriptor = Object.getOwnPropertyDescriptor(globalThis, "self"); + expect(descriptor.get).toBeInstanceOf(Function); + expect(descriptor.set).toBeInstanceOf(Function); + expect(descriptor.enumerable).toBe(true); + expect(descriptor.configurable).toBe(true); + expect(globalThis.self).toBe(globalThis); +}); -- cgit v1.2.3