mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
fix(node): setting process.exitCode should change exit code of process (#21429)
This commit is contained in:
parent
61a4e88e38
commit
f29075ae4c
2 changed files with 65 additions and 5 deletions
|
@ -681,9 +681,58 @@ Deno.test("process.memoryUsage.rss()", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("process.exitCode", () => {
|
Deno.test("process.exitCode", () => {
|
||||||
assert(process.exitCode === undefined);
|
assertEquals(process.exitCode, undefined);
|
||||||
process.exitCode = 127;
|
process.exitCode = 127;
|
||||||
assert(process.exitCode === 127);
|
assertEquals(process.exitCode, 127);
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
(process.exitCode as any) = "asdf";
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
assertEquals(process.exitCode as any, "asdf");
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
(process.exitCode as any) = "10";
|
||||||
|
process.exitCode = undefined; // reset
|
||||||
|
});
|
||||||
|
|
||||||
|
async function exitCodeTest(codeText: string, expectedExitCode: number) {
|
||||||
|
const command = new Deno.Command(Deno.execPath(), {
|
||||||
|
args: [
|
||||||
|
"eval",
|
||||||
|
codeText,
|
||||||
|
],
|
||||||
|
cwd: testDir,
|
||||||
|
});
|
||||||
|
const { code } = await command.output();
|
||||||
|
assertEquals(code, expectedExitCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
Deno.test("process.exitCode in should change exit code", async () => {
|
||||||
|
await exitCodeTest(
|
||||||
|
"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,
|
||||||
|
);
|
||||||
|
await exitCodeTest(
|
||||||
|
"import process from 'node:process'; process.exitCode = '0x10';",
|
||||||
|
16,
|
||||||
|
);
|
||||||
|
await exitCodeTest(
|
||||||
|
"import process from 'node:process'; process.exitCode = NaN;",
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("Deno.exit should override process exit", async () => {
|
||||||
|
await exitCodeTest(
|
||||||
|
"import process from 'node:process'; process.exitCode = 10; Deno.exit(12);",
|
||||||
|
12,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("process.config", () => {
|
Deno.test("process.config", () => {
|
||||||
|
|
|
@ -76,15 +76,16 @@ const notImplementedEvents = [
|
||||||
];
|
];
|
||||||
|
|
||||||
export const argv: string[] = [];
|
export const argv: string[] = [];
|
||||||
|
let globalProcessExitCode: number | undefined = undefined;
|
||||||
|
|
||||||
/** https://nodejs.org/api/process.html#process_process_exit_code */
|
/** https://nodejs.org/api/process.html#process_process_exit_code */
|
||||||
export const exit = (code?: number | string) => {
|
export const exit = (code?: number | string) => {
|
||||||
if (code || code === 0) {
|
if (code || code === 0) {
|
||||||
if (typeof code === "string") {
|
if (typeof code === "string") {
|
||||||
const parsedCode = parseInt(code);
|
const parsedCode = parseInt(code);
|
||||||
process.exitCode = isNaN(parsedCode) ? undefined : parsedCode;
|
globalProcessExitCode = isNaN(parsedCode) ? undefined : parsedCode;
|
||||||
} else {
|
} else {
|
||||||
process.exitCode = code;
|
globalProcessExitCode = code;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -426,7 +427,17 @@ class Process extends EventEmitter {
|
||||||
_exiting = _exiting;
|
_exiting = _exiting;
|
||||||
|
|
||||||
/** https://nodejs.org/api/process.html#processexitcode_1 */
|
/** https://nodejs.org/api/process.html#processexitcode_1 */
|
||||||
exitCode: undefined | number = undefined;
|
get exitCode() {
|
||||||
|
return globalProcessExitCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
set exitCode(code: number | undefined) {
|
||||||
|
globalProcessExitCode = code;
|
||||||
|
code = parseInt(code) || 0;
|
||||||
|
if (!isNaN(code)) {
|
||||||
|
ops.op_set_exit_code(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Typed as any to avoid importing "module" module for types
|
// Typed as any to avoid importing "module" module for types
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
|
|
Loading…
Reference in a new issue