fix: validate integer values in Deno.exitCode setter (#24068)

This commit is contained in:
Kenta Moriuchi 2024-06-03 10:29:01 +09:00 committed by GitHub
parent 754f21f0cd
commit eda43c46de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 90 additions and 135 deletions

View file

@ -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;
}
});

View file

@ -813,10 +813,6 @@ Deno.test("process.exitCode in should change exit code", async () => {
"import process from 'node:process'; process.exitCode = 127;",
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 () => {