Refine CLI version flag alias parsing and test expectations

This commit is contained in:
gitea committed 2026-07-29 22:28:38 +08:00
1 parent ab440024b8
commit 802c32c6bd
2 files changed
+60 -10

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
import { describe, expect, test } from "bun:test";
import { spawnSync } from "node:child_process";
import { join } from "node:path";
import { VERSION } from "./version.js";
const CLI_PATH = join(import.meta.dirname, "cli.ts");
function runCli(args: string[]) {
return spawnSync("bun", [CLI_PATH, ...args], {
encoding: "utf-8"
});
}
describe("CLI version & help handling", () => {
test.each([
["version"],
["v"],
["-v"],
["-version"],
["--version"],
["-V"],
["--v"],
["--V"]
])("version command alias '%s' returns version JSON", (flag) => {
const result = runCli([flag]);
expect(result.status).toBe(0);
const parsed = JSON.parse(result.stdout.trim());
expect(parsed.name).toBe("seal-home");
expect(parsed.version).toBe(VERSION);
});
test("help command prints standard help banner", () => {
const result = runCli(["help"]);
expect(result.status).toBe(0);
expect(result.stdout).toContain("seal-home CLI");
expect(result.stdout).toContain("Usage:");
});
test("invalid command displays unknown command error with help hint", () => {
const result = runCli(["invalid-command"]);
expect(result.status).toBe(1);
expect(result.stderr).toContain("Unknown command: invalid-command. Run 'seal-home help' for available commands.");
});
});
+16 -10
View File
@@ -40,12 +40,13 @@ async function main() {
const args = parseArgs(Bun.argv.slice(2));
const [area, action, maybeName] = args.command;
if (!area || area === "help" || args.options.help) {
printHelp();
return;
}
if (area === "version") {
if (
area === "version" ||
area === "v" ||
Boolean(args.options.version) ||
Boolean(args.options.v) ||
Boolean(args.options.V)
) {
printJson({
name: "seal-home",
version: VERSION
@@ -53,6 +54,11 @@ async function main() {
return;
}
if (!area || area === "help" || args.options.help) {
printHelp();
return;
}
if (area === "config" && action === "paths") {
printJson({
candidates: getEnterprisesDirCandidates()
@@ -475,7 +481,7 @@ async function main() {
return;
}
throw new Error(`Unknown command: ${args.command.join(" ")}`);
throw new Error(`Unknown command: ${args.command.join(" ")}. Run 'seal-home help' for available commands.`);
}
async function runServiceCommand(action?: string) {
@@ -1241,10 +1247,10 @@ function parseArgs(argv: string[]): ParsedArgs {
continue;
}
if (arg.startsWith("--")) {
const key = arg.slice(2);
if (arg.startsWith("-")) {
const key = arg.replace(/^-+/, "");
const next = argv[index + 1];
if (!next || next.startsWith("--")) {
if (!next || next.startsWith("-")) {
options[key] = true;
} else {
options[key] = next;