From 6b1fefd45a3b10074f180906b823de65cb1801b4 Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Thu, 18 Jul 2024 13:49:38 +0900 Subject: [PATCH] test: set exit code to 0 to get invalid args tests to pass (#316) Currently the CI on the main branch is red because non-zero exit code has started to be considered as test failure since Deno 1.44.0. ``` ERRORS parseArgsForLogSubcommand ... specify invalid format in since => ./src/subcommands/logs_test.ts:51:11 error: Error: Test case finished with exit code set to 1. await t.step("specify invalid format in since", () => { ^ at exitSanitizer (ext:cli/40_test.js:113:15) at async Object.outerWrapped [as fn] (ext:cli/40_test.js:134:14) at async TestContext.step (ext:cli/40_test.js:492:22) at async file:///Users/runner/work/deployctl/deployctl/src/subcommands/logs_test.ts:51:3 parseArgsForLogSubcommand ... specify invalid format in until => ./src/subcommands/logs_test.ts:55:11 error: Error: Test case finished with exit code set to 1. await t.step("specify invalid format in until", () => { ^ at exitSanitizer (ext:cli/40_test.js:113:15) at async Object.outerWrapped [as fn] (ext:cli/40_test.js:134:14) at async TestContext.step (ext:cli/40_test.js:492:22) at async file:///Users/runner/work/deployctl/deployctl/src/subcommands/logs_test.ts:55:3 FAILURES ``` This commit works around this issue by forcibly setting exit code to 0. --- src/subcommands/logs_test.ts | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/subcommands/logs_test.ts b/src/subcommands/logs_test.ts index 4f64d7a7..6bf2ffa3 100644 --- a/src/subcommands/logs_test.ts +++ b/src/subcommands/logs_test.ts @@ -1,13 +1,33 @@ import { parseArgsForLogSubcommand } from "./logs.ts"; -import { assertEquals, assertThrows } from "jsr:@std/assert@0.217"; +import { + assertEquals, + assertNotEquals, + assertThrows, +} from "jsr:@std/assert@0.217"; import { parseArgs } from "../args.ts"; Deno.test("parseArgsForLogSubcommand", async (t) => { - const parseHelper = (args: string[]) => - // NOTE: We omit `logs` subcommand from the arguments passed to `parseArgs()` - // in order to match the actual behavior; the first positional argument is - // removed using `args._.shift()` in `deployctl.ts`. - parseArgsForLogSubcommand(parseArgs(args)); + const parseHelper = (args: string[]) => { + // For this test, the subcommand name should not be included in `args`. + assertNotEquals(args.at(0), "logs"); + + try { + return parseArgsForLogSubcommand(parseArgs(args)); + } catch (e) { + // Since Deno v1.44.0, when `Deno.exitCode` was introduced, test cases + // with non-zero exit code has been treated as failure, causing some tests + // to fail unexpectedly (not sure if this behavior change is intended). + // To avoid this, we set `Deno.exitCode` to 0 before giving control back + // to each test case. + // https://github.com/denoland/deno/pull/23609 + // deno-lint-ignore no-explicit-any + if ((Deno as any).exitCode !== undefined) { + // deno-lint-ignore no-explicit-any + (Deno as any).exitCode = 0; + } + throw e; + } + }; await t.step("specify help", () => { const got = parseHelper(["--help"]);