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
3 changes: 1 addition & 2 deletions contracts/cli-commands.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"schemaVersion": 1,
"cliVersion": "1.20.0",
"schemaVersion": 2,
"commands": [
{
"path": "add",
Expand Down
6 changes: 2 additions & 4 deletions src/contracts/cli-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ export function validateCommandSemantics(paths: string[], metadata: CommandSeman
}

export interface CliCommandContract {
schemaVersion: 1;
cliVersion: string;
schemaVersion: 2;
commands: ContractCommand[];
}
interface ContractCommand {
Expand Down Expand Up @@ -266,8 +265,7 @@ export function generateCommandContract(
};
visit(program, "");
return {
schemaVersion: 1,
cliVersion: program.version() ?? "",
schemaVersion: 2,
commands: commands.toSorted((a, b) => a.path.localeCompare(b.path)),
};
}
Expand Down
16 changes: 15 additions & 1 deletion tests/unit/cli-contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, test } from "vitest";
import { Command } from "commander";
import pkg from "../../package.json";
import { buildProgram, discoverCommandPaths } from "../../src/cli-program.ts";
import {
commandSemantics,
Expand Down Expand Up @@ -40,6 +41,7 @@ describe("CLI command contract", () => {
expect(first).toBeInstanceOf(Command);
expect(second).not.toBe(first);
expect(first.name()).toBe("arashi");
expect(first.version()).toBe(pkg.version);
});

test("discovers every registered command path exactly", () => {
Expand Down Expand Up @@ -108,11 +110,23 @@ describe("CLI command contract", () => {
);
expect(first).toBe(second);
const contract = JSON.parse(first);
expect(contract.schemaVersion).toBe(1);
expect(contract.schemaVersion).toBe(2);
expect(contract).not.toHaveProperty("cliVersion");
expect(contract.commands.map((command: { path: string }) => command.path)).toEqual(
expectedPaths,
);
expect(contract.commands[0]).toHaveProperty("options");
expect(first.endsWith("\n")).toBe(true);
});

test("does not change when only the runtime release version changes", () => {
const current = buildProgram({ includeHelpBanner: false });
const alternateRelease = new Command().name("arashi").version("999.0.0");
for (const command of buildProgram({ includeHelpBanner: false }).commands)
alternateRelease.addCommand(command);

expect(serializeCommandContract(generateCommandContract(current, commandSemantics))).toBe(
serializeCommandContract(generateCommandContract(alternateRelease, commandSemantics)),
);
});
});
Loading