Skip to content

Commit 6885e58

Browse files
committed
update tests
1 parent 4820190 commit 6885e58

File tree

2 files changed

+90
-73
lines changed

2 files changed

+90
-73
lines changed

demo.citty.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ for (const command of [main, ...Object.values(main.subCommands)]) {
5757

5858
for (const [argName, argConfig] of Object.entries(command.args || {})) {
5959
const optionKey = `--${argName}`;
60+
6061
if (argName === "port") {
6162
flagMap.set(optionKey, async (_, toComplete) => {
6263
const options = [
@@ -67,9 +68,7 @@ for (const command of [main, ...Object.values(main.subCommands)]) {
6768
? options.filter(comp => comp.action.startsWith(toComplete))
6869
: options;
6970
});
70-
}
71-
72-
if (argName === "host") {
71+
} else if (argName === "host") {
7372
flagMap.set(optionKey, async (_, toComplete) => {
7473
const options = [
7574
{ action: "localhost", description: "Localhost" },
@@ -79,9 +78,14 @@ for (const command of [main, ...Object.values(main.subCommands)]) {
7978
? options.filter(comp => comp.action.startsWith(toComplete))
8079
: options;
8180
});
82-
}
83-
84-
if (argName === "mode") {
81+
} else if (argName === "config") {
82+
flagMap.set(optionKey, async (_, toComplete) => {
83+
const configFiles = ["vite.config.ts", "vite.config.js"].filter(
84+
(file) => file.startsWith(toComplete)
85+
);
86+
return configFiles.map((file) => ({ action: file }));
87+
});
88+
} else if (argName === "mode") {
8589
flagMap.set(optionKey, async (_, toComplete) => {
8690
const options = [
8791
{ action: "development", description: "Development mode" },
@@ -91,6 +95,14 @@ for (const command of [main, ...Object.values(main.subCommands)]) {
9195
? options.filter(comp => comp.action.startsWith(toComplete))
9296
: options;
9397
});
98+
} else {
99+
flagMap.set(optionKey, async (_, toComplete) => {
100+
const flag = optionKey.startsWith("--") ? optionKey.slice(2) : optionKey;
101+
if (!toComplete || optionKey.startsWith(toComplete)) {
102+
return [{ action: optionKey, description: argConfig.description }];
103+
}
104+
return [];
105+
});
94106
}
95107
}
96108

tests/cli.test.ts

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,89 +13,94 @@ function runCommand(command: string): Promise<string> {
1313
});
1414
}
1515

16-
describe("CLI Completion Tests for CAC", () => {
17-
it("Completes Vite Commands Correctly", async () => {
18-
const output = await runCommand("pnpm tsx demo.cac.ts complete --");
19-
console.log("Command Output:", output);
20-
expect(output).toContain("src/");
21-
expect(output).toContain("./");
22-
// expect(output).toContain('--base');
23-
});
16+
const cliTools = ["cac", "citty"];
17+
18+
describe.each(cliTools)("cli completion tests for %s", (cliTool) => {
19+
const commandPrefix = `pnpm tsx demo.${cliTool}.ts complete --`;
20+
21+
// it("should complete vite commands", async () => {
22+
// const output = await runCommand(commandPrefix);
23+
// console.log(`[${cliTool}] Command Output:`, output);
24+
// expect(output).toContain("src/");
25+
// expect(output).toContain("./");
26+
// // expect(output).toContain('--base');
27+
// });
2428

25-
it("Completes CLI Options Correctly", async () => {
26-
const output = await runCommand("pnpm tsx demo.cac.ts complete -- --");
27-
console.log("Command Output:", output);
29+
it("should complete cli options", async () => {
30+
const output = await runCommand(`${commandPrefix} --`);
31+
console.log(`[${cliTool}] Command Output:`, output);
2832
expect(output).toContain("--port");
2933
expect(output).toContain("--config");
3034
expect(output).toContain("--base");
3135
expect(output).toContain("--logLevel");
3236
expect(output).toContain("--filter");
3337
expect(output).toContain("--mode");
3438
});
35-
});
3639

37-
describe("CLI Option Completion for Partial Inputs", () => {
38-
const optionTests = [
39-
{ partial: "--p", expected: "--port" },
40-
];
40+
describe("cli option completion tests", () => {
41+
const optionTests = [
42+
{ partial: "--p", expected: "--port" },
43+
];
4144

42-
test.each(optionTests)(
43-
"Completes Option When Given Partial Input '%s'",
44-
async ({ partial, expected }) => {
45-
const command = `pnpm tsx demo.cac.ts complete -- ${partial}`;
46-
const output = await runCommand(command);
47-
console.log(`Complete ${partial} Output:`, output);
48-
expect(output).toContain(expected);
49-
}
50-
);
51-
});
45+
test.each(optionTests)(
46+
"should complete option for partial input '%s'",
47+
async ({ partial, expected }) => {
48+
const command = `${commandPrefix} ${partial}`;
49+
const output = await runCommand(command);
50+
console.log(`[${cliTool}] Complete ${partial} Output:`, output);
51+
expect(output).toContain(expected);
52+
}
53+
);
54+
});
5255

53-
describe("CLI Option Completion When Options Are Already Specified", () => {
54-
const alreadySpecifiedTests = [
55-
{ specified: "--port", shouldNotContain: "--port" },
56-
];
56+
describe("cli option exclusion tests", () => {
57+
const alreadySpecifiedTests = [
58+
{ specified: "--port", shouldNotContain: "--port" },
59+
];
5760

58-
test.each(alreadySpecifiedTests)(
59-
"Does Not Suggest Already Specified Option '%s'",
60-
async ({ specified, shouldNotContain }) => {
61-
const command = `pnpm tsx demo.cac.ts complete -- ${specified} --`;
62-
const output = await runCommand(command);
63-
console.log(`Already Specified ${specified} Output:`, output);
64-
expect(output).not.toContain(shouldNotContain);
65-
}
66-
);
67-
});
61+
test.each(alreadySpecifiedTests)(
62+
"should not suggest already specified option '%s'",
63+
async ({ specified, shouldNotContain }) => {
64+
const command = `${commandPrefix} ${specified} --`;
65+
const output = await runCommand(command);
66+
console.log(`[${cliTool}] Already Specified ${specified} Output:`, output);
67+
expect(output).not.toContain(shouldNotContain);
68+
// expect(output).toContain("--base");
69+
}
70+
);
71+
});
6872

69-
describe("CLI Option Value Handling", () => {
73+
describe("cli option value handling", () => {
7074

71-
it("Resolves Port Value Correctly", async () => {
72-
const command = "pnpm tsx demo.cac.ts complete -- --port 3";
73-
const output = await runCommand(command);
74-
console.log("Conflicting Options Output:", output);
75-
expect(output).toContain("3000");
76-
});
75+
it("should resolve port value correctly", async () => {
76+
const command = `${commandPrefix} --port 3`;
77+
const output = await runCommand(command);
78+
console.log(`[${cliTool}] Port Value Output:`, output);
79+
expect(output).toContain("3000");
80+
});
7781

78-
it("Handles Conflicting Options Appropriately", async () => {
79-
const command = "pnpm tsx demo.cac.ts complete -- --port 3000 --";
80-
const output = await runCommand(command);
81-
console.log("Conflicting Options Output:", output);
82-
expect(output).not.toContain("--port");
83-
expect(output).toContain("--config");
84-
// expect(output).toContain("--base");
85-
});
82+
it("should handle conflicting options appropriately", async () => {
83+
const command = `${commandPrefix} --port 3000 --`;
84+
const output = await runCommand(command);
85+
console.log(`[${cliTool}] Conflicting Options Output:`, output);
86+
expect(output).not.toContain("--port");
87+
expect(output).toContain("--config");
88+
// expect(output).toContain("--base");
89+
});
8690

87-
it("Resolves Config Option Values Correctly", async () => {
88-
const command = "pnpm tsx demo.cac.ts complete -- --port 3000 --config vite.config";
89-
const output = await runCommand(command);
90-
console.log("Conflicting Options Output:", output);
91-
expect(output).toContain("vite.config.ts");
92-
expect(output).toContain("vite.config.js");
93-
});
91+
it("should resolve config option values correctly", async () => {
92+
const command = `${commandPrefix} --port 3000 --config vite.config`;
93+
const output = await runCommand(command);
94+
console.log(`[${cliTool}] Config Option Output:`, output);
95+
expect(output).toContain("vite.config.ts");
96+
expect(output).toContain("vite.config.js");
97+
});
9498

95-
it("Gracefully Handles Unknown Options with No Completions", async () => {
96-
const command = "pnpm tsx demo.cac.ts complete -- --unknownoption";
97-
const output = await runCommand(command);
98-
console.log("No Completion Available Output:", output);
99-
expect(output.trim()).toMatch(/^(:\d+)?$/);
99+
it("should handle unknown options with no completions", async () => {
100+
const command = `${commandPrefix} --unknownoption`;
101+
const output = await runCommand(command);
102+
console.log(`[${cliTool}] No Completion Available Output:`, output);
103+
expect(output.trim()).toMatch(/^(:\d+)?$/);
104+
});
100105
});
101106
});

0 commit comments

Comments
 (0)