Skip to content
Open
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
8 changes: 4 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ export async function main(argv: string[]): Promise<number> {
const args = parseArgs(argv);
const { command, flags } = args;

if (flags["help"] || command === "help" || command === undefined) {
process.stdout.write(usage());
return command === undefined && !flags["help"] ? 2 : 0;
}
if (flags["version"]) {
process.stdout.write(`${VERSION}\n`);
return 0;
}
if (flags["help"] || command === "help" || command === undefined) {
process.stdout.write(usage());
return command === undefined && !flags["help"] ? 2 : 0;
}
if (command === "rules") {
printRules();
return 0;
Expand Down
33 changes: 31 additions & 2 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import { describe, expect, it } from "vitest";
import { collectHeaders, parseArgs } from "../src/cli.js";
import { afterEach, describe, expect, it, vi } from "vitest";
import { collectHeaders, main, parseArgs } from "../src/cli.js";

afterEach(() => {
vi.restoreAllMocks();
});

describe("top-level information flags", () => {
it("prints only the version and succeeds without a command", async () => {
const write = vi.spyOn(process.stdout, "write").mockImplementation(() => true);

await expect(main(["--version"])).resolves.toBe(0);

expect(write).toHaveBeenCalledOnce();
expect(write).toHaveBeenCalledWith(expect.stringMatching(/^\d+\.\d+\.\d+\n$/));
});

it("keeps a bare invocation as a usage error", async () => {
const write = vi.spyOn(process.stdout, "write").mockImplementation(() => true);

await expect(main([])).resolves.toBe(2);
expect(write).toHaveBeenCalledWith(expect.stringContaining("USAGE"));
});

it("keeps explicit help successful", async () => {
const write = vi.spyOn(process.stdout, "write").mockImplementation(() => true);

await expect(main(["--help"])).resolves.toBe(0);
expect(write).toHaveBeenCalledWith(expect.stringContaining("USAGE"));
});
});

describe("HTTP headers", () => {
it("collects every repeated --header flag", () => {
Expand Down
Loading