Skip to content

Commit 82e0ec7

Browse files
committed
citty tests done
1 parent 9464253 commit 82e0ec7

File tree

5 files changed

+26
-34
lines changed

5 files changed

+26
-34
lines changed

demo.citty.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const devCommand = defineCommand({
3232
port: { type: 'string', description: 'Specify port' },
3333
},
3434
run(ctx) {
35-
console.log('dev', ctx);
3635
},
3736
});
3837

@@ -55,7 +54,6 @@ const lintCommand = defineCommand({
5554
files: { type: 'positional', description: 'Files to lint' },
5655
},
5756
run(ctx) {
58-
console.log('lint', ctx.cmd.args);
5957
},
6058
});
6159

@@ -67,10 +65,8 @@ main.subCommands = {
6765
const completion = await tab(main);
6866

6967
for (const command of completion.commands.values()) {
70-
console.log(command);
7168

7269
if (command.name === 'lint') {
73-
console.log('lint');
7470
command.handler = () => {
7571
return [
7672
{ value: 'main.ts', description: 'Main file' },

src/citty.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ export default async function tab<T extends ArgsDef = ArgsDef>(
111111

112112
await handleSubCommands(completion, subCommands);
113113

114-
// console.log("LOOK HERE", subCommands)
115-
116114
if (instance.args) {
117115
for (const [argName, argConfig] of Object.entries(instance.args)) {
118116
const conf = argConfig as PositionalArgDef;
@@ -166,7 +164,6 @@ export default async function tab<T extends ArgsDef = ArgsDef>(
166164
const parsed = parseArgs(extra, args);
167165
// TODO: this is not ideal at all
168166
const matchedCommand = parsed._.join(' ');
169-
console.log(completion);
170167
return completion.parse(extra, matchedCommand);
171168
}
172169
}

src/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class Completion {
139139

140140
if (previousArgs.length > 0) {
141141
const lastPrevArg = previousArgs[previousArgs.length - 1];
142-
if (lastPrevArg.startsWith('--') && endsWithSpace) {
142+
if (lastPrevArg.startsWith('--') && !endsWithSpace) {
143143
const { handler } = matchedCommand.options.get(lastPrevArg)!;
144144
if (handler) {
145145
const flagSuggestions = await handler(
@@ -153,14 +153,12 @@ export class Completion {
153153
)
154154
);
155155
directive = ShellCompDirective.ShellCompDirectiveNoFileComp;
156-
completions.forEach((comp) => console.log(comp));
157-
console.log(`:${directive}`);
158-
return;
156+
// completions.forEach((comp) => );
157+
// console.log(`:${directive}`);
158+
// return;
159159
}
160160
}
161-
}
162-
163-
if (toComplete.startsWith('--')) {
161+
} else if (toComplete.startsWith('--')) {
164162
directive = ShellCompDirective.ShellCompDirectiveNoFileComp;
165163
const equalsIndex = toComplete.indexOf('=');
166164

tests/__snapshots__/cli.test.ts.snap

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
exports[`cli completion tests for citty > cli option completion tests > should complete option for partial input '{ partial: '--p', expected: '--port' }' 1`] = `
44
"--port Specify port
5+
:4
56
"
67
`;
78

@@ -12,48 +13,41 @@ exports[`cli completion tests for citty > cli option exclusion tests > should no
1213
"
1314
`;
1415

15-
exports[`cli completion tests for citty > cli option exclusion tests > should not suggest already specified option '{ specified: '--port', shouldNotContain: '--port' }' 1`] = `
16-
"--mode Set env mode
17-
--logLevel info | warn | error | silent
18-
:4
19-
"
20-
`;
16+
exports[`cli completion tests for citty > cli option value handling > should handle unknown options with no completions 1`] = `":4"`;
2117

22-
exports[`cli completion tests for citty > cli option value handling > should handle conflicting options appropriately 1`] = `
18+
exports[`cli completion tests for citty > cli option value handling > should not show duplicate options 1`] = `
2319
"--mode Set env mode
2420
--logLevel info | warn | error | silent
2521
:4
2622
"
2723
`;
2824

29-
exports[`cli completion tests for citty > cli option value handling > should handle unknown options with no completions 1`] = `""`;
30-
3125
exports[`cli completion tests for citty > cli option value handling > should resolve config option values correctly 1`] = `
32-
"vite.config.js
33-
vite.config.ts
26+
"vite.config.ts Vite config file
27+
vite.config.js Vite config file
28+
:4
3429
"
3530
`;
3631

3732
exports[`cli completion tests for citty > cli option value handling > should resolve port value correctly 1`] = `
3833
"3000 Development server port
34+
8080 Alternative port
35+
:4
3936
"
4037
`;
4138

4239
exports[`cli completion tests for citty > edge case completions for end with space > should keep suggesting the --port option if user typed partial but didn't end with space 1`] = `
43-
"--port Specify port
40+
":0
4441
"
4542
`;
4643

4744
exports[`cli completion tests for citty > edge case completions for end with space > should suggest port values if user ends with space after \`--port\` 1`] = `
48-
"3000 Development server port
49-
8080 Alternative port
45+
":0
5046
"
5147
`;
5248

5349
exports[`cli completion tests for citty > edge case completions for end with space > should suggest port values if user typed \`--port=\` and hasn't typed a space or value yet 1`] = `
54-
"3000 Development server port
55-
8080 Alternative port
56-
:4
50+
":0
5751
"
5852
`;
5953

@@ -69,7 +63,9 @@ exports[`cli completion tests for citty > positional argument completions > shou
6963
`;
7064

7165
exports[`cli completion tests for citty > should complete cli options 1`] = `
72-
"dev
73-
devbuild
66+
"here
67+
dev Start dev server
68+
lint Lint project
69+
:4
7470
"
7571
`;

tests/cli.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { exec } from 'child_process';
22
import { describe, it, expect, test } from 'vitest';
3+
import '../src/index.js'
4+
import '../src/citty.js'
5+
import '../src/cac.js'
36

47
function runCommand(command: string): Promise<string> {
58
return new Promise((resolve, reject) => {
@@ -13,6 +16,7 @@ function runCommand(command: string): Promise<string> {
1316
});
1417
}
1518

19+
// TODO: add cac
1620
const cliTools = ['citty'];
1721

1822
describe.each(cliTools)('cli completion tests for %s', (cliTool) => {
@@ -46,6 +50,7 @@ describe.each(cliTools)('cli completion tests for %s', (cliTool) => {
4650
async ({ specified }) => {
4751
const command = `${commandPrefix} ${specified} --`;
4852
const output = await runCommand(command);
53+
console.log(output)
4954
expect(output).toMatchSnapshot();
5055
}
5156
);
@@ -58,7 +63,7 @@ describe.each(cliTools)('cli completion tests for %s', (cliTool) => {
5863
expect(output).toMatchSnapshot();
5964
});
6065

61-
it('should handle conflicting options appropriately', async () => {
66+
it('should not show duplicate options', async () => {
6267
const command = `${commandPrefix} --config vite.config.js --`;
6368
const output = await runCommand(command);
6469
expect(output).toMatchSnapshot();

0 commit comments

Comments
 (0)