|
1 | 1 | /** |
2 | 2 | * Tests for command parsing. |
3 | 3 | * |
4 | | - * TODO: Rewrite for new combinator API |
| 4 | + * These tests focus on command dispatching and parsing behaviors complementary |
| 5 | + * to the tests in bargs.test.ts. |
5 | 6 | */ |
| 7 | +import { expect, expectAsync } from 'bupkis'; |
6 | 8 | import { describe, it } from 'node:test'; |
7 | 9 |
|
| 10 | +import { bargs, handle } from '../src/bargs.js'; |
| 11 | +import { opt, pos } from '../src/opt.js'; |
| 12 | + |
8 | 13 | describe('command parsing', () => { |
9 | | - it.todo('parses a command with options'); |
10 | | - it.todo('parses command positionals'); |
11 | | - it.todo('calls command handler'); |
12 | | - it.todo('uses defaultCommand when no command given'); |
13 | | - it.todo('throws on unknown command'); |
14 | | - it.todo('merges global and command options'); |
| 14 | + it('parses a command with options', async () => { |
| 15 | + let result: unknown; |
| 16 | + |
| 17 | + const cli = bargs('test-cli').command( |
| 18 | + 'greet', |
| 19 | + opt.options({ |
| 20 | + loud: opt.boolean({ default: false }), |
| 21 | + name: opt.string({ default: 'world' }), |
| 22 | + }), |
| 23 | + ({ values }) => { |
| 24 | + result = values; |
| 25 | + }, |
| 26 | + ); |
| 27 | + |
| 28 | + await cli.parseAsync(['greet', '--name', 'Alice', '--loud']); |
| 29 | + |
| 30 | + expect(result, 'to satisfy', { |
| 31 | + loud: true, |
| 32 | + name: 'Alice', |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('parses command positionals', async () => { |
| 37 | + let result: unknown; |
| 38 | + |
| 39 | + const cli = bargs('test-cli').command( |
| 40 | + 'greet', |
| 41 | + pos.positionals( |
| 42 | + pos.string({ name: 'name', required: true }), |
| 43 | + pos.number({ default: 1, name: 'times' }), |
| 44 | + ), |
| 45 | + ({ positionals }) => { |
| 46 | + result = positionals; |
| 47 | + }, |
| 48 | + ); |
| 49 | + |
| 50 | + await cli.parseAsync(['greet', 'Alice', '3']); |
| 51 | + |
| 52 | + expect(result, 'to satisfy', ['Alice', 3]); |
| 53 | + }); |
| 54 | + |
| 55 | + it('calls command handler', async () => { |
| 56 | + let handlerCalled = false; |
| 57 | + let receivedValues: unknown; |
| 58 | + let receivedPositionals: unknown; |
| 59 | + |
| 60 | + const cli = bargs('test-cli').command( |
| 61 | + 'echo', |
| 62 | + handle( |
| 63 | + pos.positionals(pos.string({ name: 'message', required: true })), |
| 64 | + ({ positionals, values }) => { |
| 65 | + handlerCalled = true; |
| 66 | + receivedValues = values; |
| 67 | + receivedPositionals = positionals; |
| 68 | + }, |
| 69 | + ), |
| 70 | + ); |
| 71 | + |
| 72 | + await cli.parseAsync(['echo', 'Hello!']); |
| 73 | + |
| 74 | + expect(handlerCalled, 'to be', true); |
| 75 | + expect(receivedPositionals, 'to satisfy', ['Hello!']); |
| 76 | + expect(receivedValues, 'to satisfy', {}); |
| 77 | + }); |
| 78 | + |
| 79 | + it('uses defaultCommand when no command given', async () => { |
| 80 | + let result: unknown; |
| 81 | + |
| 82 | + const cli = bargs('test-cli') |
| 83 | + .command('run', opt.options({ verbose: opt.boolean() }), ({ values }) => { |
| 84 | + result = { command: 'run', values }; |
| 85 | + }) |
| 86 | + .command('build', opt.options({}), () => { |
| 87 | + result = { command: 'build' }; |
| 88 | + }) |
| 89 | + .defaultCommand('run'); |
| 90 | + |
| 91 | + await cli.parseAsync(['--verbose']); |
| 92 | + |
| 93 | + expect(result, 'to satisfy', { |
| 94 | + command: 'run', |
| 95 | + values: { verbose: true }, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('throws on unknown command', async () => { |
| 100 | + const cli = bargs('test-cli') |
| 101 | + .command( |
| 102 | + 'add', |
| 103 | + handle(opt.options({}), () => {}), |
| 104 | + ) |
| 105 | + .command( |
| 106 | + 'remove', |
| 107 | + handle(opt.options({}), () => {}), |
| 108 | + ); |
| 109 | + |
| 110 | + await expectAsync( |
| 111 | + cli.parseAsync(['unknown']), |
| 112 | + 'to reject with error satisfying', |
| 113 | + /Unknown command: unknown/, |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it('merges global and command options', async () => { |
| 118 | + let result: unknown; |
| 119 | + |
| 120 | + const cli = bargs('test-cli') |
| 121 | + .globals( |
| 122 | + opt.options({ |
| 123 | + config: opt.string({ default: './config.json' }), |
| 124 | + verbose: opt.boolean({ default: false }), |
| 125 | + }), |
| 126 | + ) |
| 127 | + .command( |
| 128 | + 'deploy', |
| 129 | + opt.options({ |
| 130 | + env: opt.enum(['dev', 'staging', 'prod'], { default: 'dev' }), |
| 131 | + force: opt.boolean({ default: false }), |
| 132 | + }), |
| 133 | + ({ values }) => { |
| 134 | + result = values; |
| 135 | + }, |
| 136 | + ); |
| 137 | + |
| 138 | + await cli.parseAsync(['deploy', '--verbose', '--env', 'prod', '--force']); |
| 139 | + |
| 140 | + expect(result, 'to satisfy', { |
| 141 | + config: './config.json', |
| 142 | + env: 'prod', |
| 143 | + force: true, |
| 144 | + verbose: true, |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it('parses command with mixed options and positionals', async () => { |
| 149 | + let result: unknown; |
| 150 | + |
| 151 | + const cli = bargs('test-cli').command( |
| 152 | + 'copy', |
| 153 | + handle( |
| 154 | + pos.positionals( |
| 155 | + pos.string({ name: 'source', required: true }), |
| 156 | + pos.string({ name: 'dest', required: true }), |
| 157 | + )( |
| 158 | + opt.options({ |
| 159 | + force: opt.boolean({ aliases: ['f'] }), |
| 160 | + recursive: opt.boolean({ aliases: ['r'] }), |
| 161 | + }), |
| 162 | + ), |
| 163 | + ({ positionals, values }) => { |
| 164 | + result = { positionals, values }; |
| 165 | + }, |
| 166 | + ), |
| 167 | + ); |
| 168 | + |
| 169 | + await cli.parseAsync(['copy', '-rf', 'src/', 'dst/']); |
| 170 | + |
| 171 | + expect(result, 'to satisfy', { |
| 172 | + positionals: ['src/', 'dst/'], |
| 173 | + values: { force: true, recursive: true }, |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + it('passes positionals through to default command', async () => { |
| 178 | + let result: unknown; |
| 179 | + |
| 180 | + const cli = bargs('test-cli') |
| 181 | + .command( |
| 182 | + 'run', |
| 183 | + pos.positionals(pos.variadic('string', { name: 'files' })), |
| 184 | + ({ positionals }) => { |
| 185 | + result = positionals; |
| 186 | + }, |
| 187 | + ) |
| 188 | + .defaultCommand('run'); |
| 189 | + |
| 190 | + // When no command matches, positionals go to default command |
| 191 | + await cli.parseAsync(['file1.js', 'file2.js', 'file3.js']); |
| 192 | + |
| 193 | + expect(result, 'to satisfy', [['file1.js', 'file2.js', 'file3.js']]); |
| 194 | + }); |
15 | 195 | }); |
0 commit comments