diff options
author | Kenta Moriuchi <moriken@kimamass.com> | 2024-06-03 10:29:01 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-02 21:29:01 -0400 |
commit | eda43c46de12ed589fdbe62ba0574887cfbb3574 (patch) | |
tree | 9703b6c92b2e3ca21768d9ae6d82730c41e41157 /tests | |
parent | 754f21f0cd6b788ded48238c5acc3f635329d473 (diff) |
fix: validate integer values in `Deno.exitCode` setter (#24068)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/os_test.ts | 67 | ||||
-rw-r--r-- | tests/unit_node/process_test.ts | 8 |
2 files changed, 67 insertions, 8 deletions
diff --git a/tests/unit/os_test.ts b/tests/unit/os_test.ts index e24494854..30d8f26ee 100644 --- a/tests/unit/os_test.ts +++ b/tests/unit/os_test.ts @@ -302,3 +302,70 @@ Deno.test(function memoryUsage() { assert(typeof mem.external === "number"); assert(mem.rss >= mem.heapTotal); }); + +Deno.test("Deno.exitCode getter and setter", () => { + // Initial value is 0 + assertEquals(Deno.exitCode, 0); + + try { + // Set a new value + Deno.exitCode = 5; + assertEquals(Deno.exitCode, 5); + } finally { + // Reset to initial value + Deno.exitCode = 0; + } + + assertEquals(Deno.exitCode, 0); +}); + +Deno.test("Setting Deno.exitCode to non-number throws TypeError", () => { + // Throws on non-number values + assertThrows( + () => { + // @ts-expect-error Testing for runtime error + Deno.exitCode = "123"; + }, + TypeError, + "Exit code must be a number, got: 123 (string)", + ); + + // Throws on bigint values + assertThrows( + () => { + // @ts-expect-error Testing for runtime error + Deno.exitCode = 1n; + }, + TypeError, + "Exit code must be a number, got: 1 (bigint)", + ); +}); + +Deno.test("Setting Deno.exitCode to non-integer throws RangeError", () => { + // Throws on non-integer values + assertThrows( + () => { + Deno.exitCode = 3.14; + }, + RangeError, + "Exit code must be an integer, got: 3.14", + ); +}); + +Deno.test("Setting Deno.exitCode does not cause an immediate exit", () => { + let exited = false; + + const originalExit = Deno.exit; + // @ts-expect-error; read-only + Deno.exit = () => { + exited = true; + }; + + try { + Deno.exitCode = 1; + assertEquals(exited, false); + } finally { + Deno.exit = originalExit; + Deno.exitCode = 0; + } +}); diff --git a/tests/unit_node/process_test.ts b/tests/unit_node/process_test.ts index 5e2fb69c2..24fd3909d 100644 --- a/tests/unit_node/process_test.ts +++ b/tests/unit_node/process_test.ts @@ -814,10 +814,6 @@ Deno.test("process.exitCode in should change exit code", async () => { 127, ); await exitCodeTest( - "import process from 'node:process'; process.exitCode = 2.5;", - 2, - ); - await exitCodeTest( "import process from 'node:process'; process.exitCode = '10';", 10, ); @@ -825,10 +821,6 @@ Deno.test("process.exitCode in should change exit code", async () => { "import process from 'node:process'; process.exitCode = '0x10';", 16, ); - await exitCodeTest( - "import process from 'node:process'; process.exitCode = NaN;", - 1, - ); }); Deno.test("Deno.exit should override process exit", async () => { |