From 6a821c2647037fa0cbaf4b75e16566fd7868bf04 Mon Sep 17 00:00:00 2001 From: easrng Date: Mon, 28 Apr 2025 21:09:46 -0400 Subject: [PATCH 1/3] feat(internal): improve format support in node and bun --- internal/format.ts | 26 +++++++++------- internal/format_test.ts | 69 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 12 deletions(-) diff --git a/internal/format.ts b/internal/format.ts index 6e186381e950..66611a6191c3 100644 --- a/internal/format.ts +++ b/internal/format.ts @@ -21,17 +21,21 @@ */ export function format(v: unknown): string { // deno-lint-ignore no-explicit-any - const { Deno } = globalThis as any; + const { Deno, process } = globalThis as any; + const options = { + depth: Infinity, + sorted: true, + trailingComma: true, + compact: false, + iterableLimit: Infinity, + // getters should be true in assertEquals. + getters: true, + strAbbreviateSize: Infinity, + }; + return typeof Deno?.inspect === "function" - ? Deno.inspect(v, { - depth: Infinity, - sorted: true, - trailingComma: true, - compact: false, - iterableLimit: Infinity, - // getters should be true in assertEquals. - getters: true, - strAbbreviateSize: Infinity, - }) + ? Deno.inspect(v, options) + : typeof process?.getBuiltinModule === "function" + ? process.getBuiltinModule("node:util").inspect(v, options) : `"${String(v).replace(/(?=["\\])/g, "\\")}"`; } diff --git a/internal/format_test.ts b/internal/format_test.ts index 777fcccc4859..3fe4651d3b76 100644 --- a/internal/format_test.ts +++ b/internal/format_test.ts @@ -103,15 +103,82 @@ Deno.test("format() doesn't truncate long strings in object", () => { ); }); -Deno.test("format() has fallback to String if Deno.inspect is not available", () => { +Deno.test("format() has fallback to util.inspect if Deno.inspect is not available", async (t) => { // Simulates the environment where Deno.inspect is not available const inspect = Deno.inspect; // deno-lint-ignore no-explicit-any delete (Deno as any).inspect; + try { + assertEquals(format([..."abcd"]), `[\n 'a',\n 'b',\n 'c',\n 'd',\n]`); + assertEquals(format({ a: 1, b: 2 }), `{\n a: 1,\n b: 2,\n}`); + await t.step("format() sorts properties", () => + assertEquals( + format({ b: 2, a: 1 }), + format({ a: 1, b: 2 }), + )); + + await t.step("format() wraps Object with getters", () => + assertEquals( + format(Object.defineProperty({}, "a", { + enumerable: true, + get() { + return 1; + }, + })), + `{ + a: [Getter: 1], +}`, + )); + + await t.step("format() wraps nested small objects", () => + assertEquals( + stripAnsiCode(format([{ x: { a: 1, b: 2 }, y: ["a", "b"] }])), + `[ + { + x: { + a: 1, + b: 2, + }, + y: [ + 'a', + 'b', + ], + }, +]`, + )); + + // Grouping is disabled. + await t.step("format() disables grouping", () => + assertEquals( + stripAnsiCode(format(["i", "i", "i", "i", "i", "i", "i"])), + `[ + 'i', + 'i', + 'i', + 'i', + 'i', + 'i', + 'i', +]`, + )); + } finally { + Deno.inspect = inspect; + } +}); + +Deno.test("format() has fallback to String if util.inspect and Deno.inspect are not available", () => { + // Simulates the environment where Deno.inspect and util.inspect are not available + const inspect = Deno.inspect; + // deno-lint-ignore no-explicit-any + delete (Deno as any).inspect; + // deno-lint-ignore no-explicit-any + const { process } = globalThis as any; + const getBuiltinModule = process.getBuiltinModule; try { assertEquals(format([..."abcd"]), `"a,b,c,d"`); assertEquals(format({ a: 1, b: 2 }), `"[object Object]"`); } finally { Deno.inspect = inspect; + process.getBuiltinModule = getBuiltinModule; } }); From de912217da4e999c7dff4ee7bbc03471c76372b4 Mon Sep 17 00:00:00 2001 From: easrng Date: Mon, 28 Apr 2025 21:32:01 -0400 Subject: [PATCH 2/3] add to node tests --- _tools/node_test_runner/run_test.mjs | 5 +- internal/format.ts | 31 ++++--- internal/format_test.ts | 131 +++++++++++++++------------ 3 files changed, 96 insertions(+), 71 deletions(-) diff --git a/_tools/node_test_runner/run_test.mjs b/_tools/node_test_runner/run_test.mjs index 187b53585eb2..364a5fa0285b 100644 --- a/_tools/node_test_runner/run_test.mjs +++ b/_tools/node_test_runner/run_test.mjs @@ -73,7 +73,10 @@ import "../../fs/unstable_lstat_test.ts"; import "../../fs/unstable_chmod_test.ts"; import "../../fs/unstable_umask_test.ts"; import "../../fs/unstable_utime_test.ts"; +import "../../internal/format_test.ts"; for (const testDef of testDefinitions) { - test(testDef.name, testDef.fn); + test(testDef.name, (t) => + testDef.fn({ step: (name, fn) => t.test(name, fn) }) + ); } diff --git a/internal/format.ts b/internal/format.ts index 66611a6191c3..f04d826287fc 100644 --- a/internal/format.ts +++ b/internal/format.ts @@ -22,20 +22,27 @@ export function format(v: unknown): string { // deno-lint-ignore no-explicit-any const { Deno, process } = globalThis as any; - const options = { - depth: Infinity, - sorted: true, - trailingComma: true, - compact: false, - iterableLimit: Infinity, - // getters should be true in assertEquals. - getters: true, - strAbbreviateSize: Infinity, - }; return typeof Deno?.inspect === "function" - ? Deno.inspect(v, options) + ? Deno.inspect(v, { + depth: Infinity, + sorted: true, + trailingComma: true, + compact: false, + iterableLimit: Infinity, + // getters should be true in assertEquals. + getters: true, + strAbbreviateSize: Infinity, + }) : typeof process?.getBuiltinModule === "function" - ? process.getBuiltinModule("node:util").inspect(v, options) + ? process.getBuiltinModule("node:util").inspect(v, { + depth: Infinity, + sorted: true, + compact: false, + iterableLimit: Infinity, + // getters should be true in assertEquals. + getters: true, + strAbbreviateSize: Infinity, + }) : `"${String(v).replace(/(?=["\\])/g, "\\")}"`; } diff --git a/internal/format_test.ts b/internal/format_test.ts index 3fe4651d3b76..8086d608fc29 100644 --- a/internal/format_test.ts +++ b/internal/format_test.ts @@ -21,46 +21,47 @@ ${red("- d")} ); }); -// Check that the diff formatter overrides some default behaviours of -// `Deno.inspect()` which are problematic for diffing. -Deno.test("format() overrides default behaviours of Deno.inspect", async (t) => { - // Wraps objects into multiple lines even when they are small. Prints trailing - // commas. - await t.step( - "format() always wraps objects into multiple lines and prints trailing commas", - () => - assertEquals( - stripAnsiCode(format({ a: 1, b: 2 })), - `{ +if (typeof Deno.inspect === "function") { + // Check that the diff formatter overrides some default behaviours of + // `Deno.inspect()` which are problematic for diffing. + Deno.test("format() overrides default behaviours of Deno.inspect", async (t) => { + // Wraps objects into multiple lines even when they are small. Prints trailing + // commas. + await t.step( + "format() always wraps objects into multiple lines and prints trailing commas", + () => + assertEquals( + stripAnsiCode(format({ a: 1, b: 2 })), + `{ a: 1, b: 2, }`, - ), - ); + ), + ); - await t.step("format() sorts properties", () => - assertEquals( - format({ b: 2, a: 1 }), - format({ a: 1, b: 2 }), - )); + await t.step("format() sorts properties", () => + assertEquals( + format({ b: 2, a: 1 }), + format({ a: 1, b: 2 }), + )); - await t.step("format() wraps Object with getters", () => - assertEquals( - format(Object.defineProperty({}, "a", { - enumerable: true, - get() { - return 1; - }, - })), - `{ + await t.step("format() wraps Object with getters", () => + assertEquals( + format(Object.defineProperty({}, "a", { + enumerable: true, + get() { + return 1; + }, + })), + `{ a: [Getter: 1], }`, - )); + )); - await t.step("format() wraps nested small objects", () => - assertEquals( - stripAnsiCode(format([{ x: { a: 1, b: 2 }, y: ["a", "b"] }])), - `[ + await t.step("format() wraps nested small objects", () => + assertEquals( + stripAnsiCode(format([{ x: { a: 1, b: 2 }, y: ["a", "b"] }])), + `[ { x: { a: 1, @@ -72,13 +73,13 @@ Deno.test("format() overrides default behaviours of Deno.inspect", async (t) => ], }, ]`, - )); + )); - // Grouping is disabled. - await t.step("format() disables grouping", () => - assertEquals( - stripAnsiCode(format(["i", "i", "i", "i", "i", "i", "i"])), - `[ + // Grouping is disabled. + await t.step("format() disables grouping", () => + assertEquals( + stripAnsiCode(format(["i", "i", "i", "i", "i", "i", "i"])), + `[ "i", "i", "i", @@ -87,21 +88,22 @@ Deno.test("format() overrides default behaviours of Deno.inspect", async (t) => "i", "i", ]`, - )); -}); - -Deno.test("format() doesn't truncate long strings in object", () => { - const str = format({ - foo: - "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", + )); }); - assertEquals( - str, - `{ + + Deno.test("format() doesn't truncate long strings in object", () => { + const str = format({ + foo: + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", + }); + assertEquals( + str, + `{ foo: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", }`, - ); -}); + ); + }); +} Deno.test("format() has fallback to util.inspect if Deno.inspect is not available", async (t) => { // Simulates the environment where Deno.inspect is not available @@ -109,8 +111,8 @@ Deno.test("format() has fallback to util.inspect if Deno.inspect is not availabl // deno-lint-ignore no-explicit-any delete (Deno as any).inspect; try { - assertEquals(format([..."abcd"]), `[\n 'a',\n 'b',\n 'c',\n 'd',\n]`); - assertEquals(format({ a: 1, b: 2 }), `{\n a: 1,\n b: 2,\n}`); + assertEquals(format([..."abcd"]), `[\n 'a',\n 'b',\n 'c',\n 'd'\n]`); + assertEquals(format({ a: 1, b: 2 }), `{\n a: 1,\n b: 2\n}`); await t.step("format() sorts properties", () => assertEquals( format({ b: 2, a: 1 }), @@ -126,7 +128,7 @@ Deno.test("format() has fallback to util.inspect if Deno.inspect is not availabl }, })), `{ - a: [Getter: 1], + a: [Getter: 1] }`, )); @@ -137,13 +139,13 @@ Deno.test("format() has fallback to util.inspect if Deno.inspect is not availabl { x: { a: 1, - b: 2, + b: 2 }, y: [ 'a', - 'b', - ], - }, + 'b' + ] + } ]`, )); @@ -158,9 +160,21 @@ Deno.test("format() has fallback to util.inspect if Deno.inspect is not availabl 'i', 'i', 'i', - 'i', + 'i' ]`, )); + await t.step("format() doesn't truncate long strings in object", () => { + const str = format({ + foo: + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", + }); + assertEquals( + str, + `{ + foo: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' +}`, + ); + }); } finally { Deno.inspect = inspect; } @@ -174,6 +188,7 @@ Deno.test("format() has fallback to String if util.inspect and Deno.inspect are // deno-lint-ignore no-explicit-any const { process } = globalThis as any; const getBuiltinModule = process.getBuiltinModule; + delete process.getBuiltinModule; try { assertEquals(format([..."abcd"]), `"a,b,c,d"`); assertEquals(format({ a: 1, b: 2 }), `"[object Object]"`); From aa39536e71348779d4cf3b7822ead67419eab927 Mon Sep 17 00:00:00 2001 From: easrng Date: Mon, 28 Apr 2025 21:37:40 -0400 Subject: [PATCH 3/3] don't run util.inspect tests in deno 1.x --- internal/format_test.ts | 105 +++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 51 deletions(-) diff --git a/internal/format_test.ts b/internal/format_test.ts index 8086d608fc29..ddbbaafcdb25 100644 --- a/internal/format_test.ts +++ b/internal/format_test.ts @@ -105,37 +105,39 @@ if (typeof Deno.inspect === "function") { }); } -Deno.test("format() has fallback to util.inspect if Deno.inspect is not available", async (t) => { - // Simulates the environment where Deno.inspect is not available - const inspect = Deno.inspect; - // deno-lint-ignore no-explicit-any - delete (Deno as any).inspect; - try { - assertEquals(format([..."abcd"]), `[\n 'a',\n 'b',\n 'c',\n 'd'\n]`); - assertEquals(format({ a: 1, b: 2 }), `{\n a: 1,\n b: 2\n}`); - await t.step("format() sorts properties", () => - assertEquals( - format({ b: 2, a: 1 }), - format({ a: 1, b: 2 }), - )); +// deno-lint-ignore no-explicit-any +if (typeof (globalThis as any).process?.getBuiltinModule === "function") { + Deno.test("format() has fallback to util.inspect if Deno.inspect is not available", async (t) => { + // Simulates the environment where Deno.inspect is not available + const inspect = Deno.inspect; + // deno-lint-ignore no-explicit-any + delete (Deno as any).inspect; + try { + assertEquals(format([..."abcd"]), `[\n 'a',\n 'b',\n 'c',\n 'd'\n]`); + assertEquals(format({ a: 1, b: 2 }), `{\n a: 1,\n b: 2\n}`); + await t.step("format() sorts properties", () => + assertEquals( + format({ b: 2, a: 1 }), + format({ a: 1, b: 2 }), + )); - await t.step("format() wraps Object with getters", () => - assertEquals( - format(Object.defineProperty({}, "a", { - enumerable: true, - get() { - return 1; - }, - })), - `{ + await t.step("format() wraps Object with getters", () => + assertEquals( + format(Object.defineProperty({}, "a", { + enumerable: true, + get() { + return 1; + }, + })), + `{ a: [Getter: 1] }`, - )); + )); - await t.step("format() wraps nested small objects", () => - assertEquals( - stripAnsiCode(format([{ x: { a: 1, b: 2 }, y: ["a", "b"] }])), - `[ + await t.step("format() wraps nested small objects", () => + assertEquals( + stripAnsiCode(format([{ x: { a: 1, b: 2 }, y: ["a", "b"] }])), + `[ { x: { a: 1, @@ -147,13 +149,13 @@ Deno.test("format() has fallback to util.inspect if Deno.inspect is not availabl ] } ]`, - )); + )); - // Grouping is disabled. - await t.step("format() disables grouping", () => - assertEquals( - stripAnsiCode(format(["i", "i", "i", "i", "i", "i", "i"])), - `[ + // Grouping is disabled. + await t.step("format() disables grouping", () => + assertEquals( + stripAnsiCode(format(["i", "i", "i", "i", "i", "i", "i"])), + `[ 'i', 'i', 'i', @@ -162,23 +164,24 @@ Deno.test("format() has fallback to util.inspect if Deno.inspect is not availabl 'i', 'i' ]`, - )); - await t.step("format() doesn't truncate long strings in object", () => { - const str = format({ - foo: - "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", - }); - assertEquals( - str, - `{ + )); + await t.step("format() doesn't truncate long strings in object", () => { + const str = format({ + foo: + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", + }); + assertEquals( + str, + `{ foo: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' }`, - ); - }); - } finally { - Deno.inspect = inspect; - } -}); + ); + }); + } finally { + Deno.inspect = inspect; + } + }); +} Deno.test("format() has fallback to String if util.inspect and Deno.inspect are not available", () => { // Simulates the environment where Deno.inspect and util.inspect are not available @@ -187,13 +190,13 @@ Deno.test("format() has fallback to String if util.inspect and Deno.inspect are delete (Deno as any).inspect; // deno-lint-ignore no-explicit-any const { process } = globalThis as any; - const getBuiltinModule = process.getBuiltinModule; - delete process.getBuiltinModule; + const getBuiltinModule = process?.getBuiltinModule; + delete process?.getBuiltinModule; try { assertEquals(format([..."abcd"]), `"a,b,c,d"`); assertEquals(format({ a: 1, b: 2 }), `"[object Object]"`); } finally { Deno.inspect = inspect; - process.getBuiltinModule = getBuiltinModule; + if (process) process.getBuiltinModule = getBuiltinModule; } });