|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import path from "node:path"; |
| 3 | +import { describe, expect, test } from "vitest"; |
| 4 | + |
| 5 | +function runCli(args: string[]) { |
| 6 | + const cliPath = path.resolve(process.cwd(), "src/cli.ts"); |
| 7 | + return spawnSync("bun", [cliPath, ...args], { |
| 8 | + cwd: process.cwd(), |
| 9 | + encoding: "utf8", |
| 10 | + }); |
| 11 | +} |
| 12 | + |
| 13 | +describe("cli", () => { |
| 14 | + test("shows root help with command list", () => { |
| 15 | + const result = runCli(["--help"]); |
| 16 | + |
| 17 | + expect(result.status).toBe(0); |
| 18 | + expect(result.stdout).toContain("Commands:"); |
| 19 | + expect(result.stdout).toContain("add [...args]"); |
| 20 | + expect(result.stdout).toContain("generate-lock [...args]"); |
| 21 | + }); |
| 22 | + |
| 23 | + test("shows subcommand help", () => { |
| 24 | + const result = runCli(["find", "--help"]); |
| 25 | + |
| 26 | + expect(result.status).toBe(0); |
| 27 | + expect(result.stdout).toContain("Usage:"); |
| 28 | + expect(result.stdout).toContain("$ skillet find [...args]"); |
| 29 | + }); |
| 30 | + |
| 31 | + test("prints version from environment override", () => { |
| 32 | + const cliPath = path.resolve(process.cwd(), "src/cli.ts"); |
| 33 | + const result = spawnSync("bun", [cliPath, "--version"], { |
| 34 | + cwd: process.cwd(), |
| 35 | + encoding: "utf8", |
| 36 | + env: { ...process.env, SKILLET_VERSION: "1.2.3" }, |
| 37 | + }); |
| 38 | + |
| 39 | + expect(result.status).toBe(0); |
| 40 | + expect(result.stdout).toContain("skillet/1.2.3"); |
| 41 | + }); |
| 42 | + |
| 43 | + test("returns clear error for unknown command", () => { |
| 44 | + const result = runCli(["nope"]); |
| 45 | + |
| 46 | + expect(result.status).not.toBe(0); |
| 47 | + expect(result.stderr).toContain("Unknown command: nope"); |
| 48 | + expect(result.stderr).not.toContain("TypeError"); |
| 49 | + }); |
| 50 | + |
| 51 | + test("returns non-zero with clear message for invalid global flag", () => { |
| 52 | + const result = runCli(["--bogus"]); |
| 53 | + |
| 54 | + expect(result.status).not.toBe(0); |
| 55 | + expect(result.stderr).toContain("Unknown option: --bogus"); |
| 56 | + }); |
| 57 | +}); |
0 commit comments