[READ-ONLY] Mirror of https://github.com/bombshell-dev/tab. shell autocompletions for javascript CLIs
4

Configure Feed

Select the types of activity you want to include in your feed.

update tests

AmirSa12 (Dec 25, 2024, 3:03 PM +0330) 6885e583 48201905

+90 -73
+18 -6
demo.citty.ts
··· 57 57 58 58 for (const [argName, argConfig] of Object.entries(command.args || {})) { 59 59 const optionKey = `--${argName}`; 60 + 60 61 if (argName === "port") { 61 62 flagMap.set(optionKey, async (_, toComplete) => { 62 63 const options = [ ··· 67 68 ? options.filter(comp => comp.action.startsWith(toComplete)) 68 69 : options; 69 70 }); 70 - } 71 - 72 - if (argName === "host") { 71 + } else if (argName === "host") { 73 72 flagMap.set(optionKey, async (_, toComplete) => { 74 73 const options = [ 75 74 { action: "localhost", description: "Localhost" }, ··· 79 78 ? options.filter(comp => comp.action.startsWith(toComplete)) 80 79 : options; 81 80 }); 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") { 85 89 flagMap.set(optionKey, async (_, toComplete) => { 86 90 const options = [ 87 91 { action: "development", description: "Development mode" }, ··· 90 94 return toComplete 91 95 ? options.filter(comp => comp.action.startsWith(toComplete)) 92 96 : options; 97 + }); 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 []; 93 105 }); 94 106 } 95 107 }
+72 -67
tests/cli.test.ts
··· 13 13 }); 14 14 } 15 15 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 + // }); 24 28 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); 28 32 expect(output).toContain("--port"); 29 33 expect(output).toContain("--config"); 30 34 expect(output).toContain("--base"); ··· 32 36 expect(output).toContain("--filter"); 33 37 expect(output).toContain("--mode"); 34 38 }); 35 - }); 36 39 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 + ]; 41 44 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 + }); 52 55 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 + ]; 57 60 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 + }); 68 72 69 - describe("CLI Option Value Handling", () => { 73 + describe("cli option value handling", () => { 70 74 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 + }); 77 81 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 + }); 86 90 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 + }); 94 98 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 + }); 100 105 }); 101 106 });