From 517cd33eed786a8e9984727334b1688ab45b7410 Mon Sep 17 00:00:00 2001 From: Ashish Jha Date: Sun, 15 Nov 2026 19:12:29 +0530 Subject: [PATCH] fix: exit non-zero on unknown commands and options program.exitOverride() makes Commander throw a CommanderError instead of exiting, but the top-level catch dropped every CommanderError (and every non-Error throw), so `silo bogus-cmd` and `silo --bogus` exited 0 after printing the error. Keep Commander's exit code: 0 for --help/--version, 1 for errors. Add tests that run the real entry point and check the exit codes. Fixes #17 --- src/cli.ts | 11 +++++++---- tests/cli.test.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 tests/cli.test.ts diff --git a/src/cli.ts b/src/cli.ts index 8969079..806801d 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,4 +1,4 @@ -import { Command } from "commander"; +import { Command, CommanderError } from "commander"; import { assertNodeVersion } from "./utils/system.js"; import { VERSION } from "./version.js"; import "./frameworks/definitions.js"; @@ -427,8 +427,11 @@ program.exitOverride(); try { await program.parseAsync(process.argv); } catch (err) { - if (err instanceof Error && err.constructor.name !== "CommanderError") { - console.error(`\n ${err.message}\n`); - process.exit(1); + // Commander has already printed its message, help, or version text; keep + // its exit code (0 for --help/--version, 1 for unknown commands/options). + if (err instanceof CommanderError) { + process.exit(err.exitCode); } + console.error(`\n ${err instanceof Error ? err.message : String(err)}\n`); + process.exit(1); } diff --git a/tests/cli.test.ts b/tests/cli.test.ts new file mode 100644 index 0000000..7d6c919 --- /dev/null +++ b/tests/cli.test.ts @@ -0,0 +1,49 @@ +import { describe, it, expect, beforeAll, afterAll } from "vitest"; +import { execa } from "execa"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; + +// Runs the real CLI entry point (via tsx) with a throwaway HOME, so the exit +// code is exactly what a shell script would see. cli.ts parses argv on import, +// so it can't be exercised in-process. +const repoRoot = fileURLToPath(new URL("..", import.meta.url)); +let home: string; + +beforeAll(() => { + home = mkdtempSync(join(tmpdir(), "silo-cli-test-")); +}); + +afterAll(() => { + rmSync(home, { recursive: true, force: true }); +}); + +function silo(...args: string[]) { + return execa("node", ["--import", "tsx", "src/cli.ts", ...args], { + cwd: repoRoot, + env: { HOME: home }, + reject: false, + }); +} + +describe("CLI exit codes", () => { + it.each(["bogus-cmd", "--bogus"])( + "exits 1 for an unknown command or option (%s)", + async (arg) => { + const result = await silo(arg); + expect(result.exitCode).toBe(1); + expect(result.stderr).toMatch(/unknown (command|option)/); + }, + 30_000, + ); + + it.each(["--help", "--version"])( + "exits 0 for %s", + async (arg) => { + const result = await silo(arg); + expect(result.exitCode).toBe(0); + }, + 30_000, + ); +});