Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
}
49 changes: 49 additions & 0 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
@@ -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,
);
});
Loading