Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into release/13.x
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Oct 27, 2024
2 parents ba74762 + 5a90c8f commit bce4491
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 75 deletions.
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/** @type {import('jest').Config} */
const config = {
testEnvironment: 'node',
collectCoverage: true,
injectGlobals: false,
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': ['ts-jest'],
},
Expand Down
12 changes: 1 addition & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"commander": "~12.1.0"
},
"devDependencies": {
"@types/jest": "^29.2.6",
"@jest/globals": "^29.3.1",
"@types/node": "^20.11.7",
"commander": "~12.1.0",
"eslint": "^8.57.0",
Expand Down
6 changes: 4 additions & 2 deletions tests/assignment.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { expectAssignable, expectNotAssignable } from 'tsd';
import { Command, CommandUnknownOpts, Option } from '..';

if ('when assign Command to CommandUnknownOpts then no error') {
// 'when assign Command to CommandUnknownOpts then no error'
{
const c = new Command();
expectAssignable<CommandUnknownOpts>(c);
}

if ('when assign CommandUnknownOpts to Command then error') {
// 'when assign CommandUnknownOpts to Command then error'
{
const custom: CommandUnknownOpts = new Command();
expectNotAssignable<Command>(custom);
}
3 changes: 2 additions & 1 deletion tests/command-unknown-opts.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { expectType } from 'tsd';

// Fallback location for CommandUnknownOpts if not better file for the test.

if ('when CommandUnknownOpts then opts().foo is allowed but unknown') {
// 'when CommandUnknownOpts then opts().foo is allowed but unknown'
{
const program: CommandUnknownOpts = new Command();
const v = program.opts()['foo'];
expectType<unknown>(v);
Expand Down
26 changes: 16 additions & 10 deletions tests/create-argument.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,56 @@ import { Command, createArgument } from '..';

// Doing end-to-end test, rather than checking created Argument directly.

if ('when cmd.createArgument with required then type is string') {
// 'when cmd.createArgument with required then type is string'
{
const program = new Command();
program.addArgument(program.createArgument('<value>')).action((arg) => {
expectType<string>(arg);
});
}

if ('when cmd.createArgument with optional then type is string|undefined') {
// ('when cmd.createArgument with optional then type is string|undefined'
{
const program = new Command();
program.addArgument(program.createArgument('[value]')).action((arg) => {
expectType<string | undefined>(arg);
});
}

if ('when cmd.createArgument with variadic then type is string[]') {
// 'when cmd.createArgument with variadic then type is string[]'
{
const program = new Command();
program.addArgument(program.createArgument('<value...>')).action((arg) => {
expectType<string[]>(arg);
});
}

if ('when global createArgument with required then type is string') {
// 'when global createArgument with required then type is string'
{
const program = new Command();
program.addArgument(createArgument('<value>')).action((arg) => {
expectType<string>(arg);
});
}

if ('when global createArgument with optional then type is string|undefined') {
// 'when global createArgument with optional then type is string|undefined'
{
const program = new Command();
program.addArgument(createArgument('[value]')).action((arg) => {
expectType<string | undefined>(arg);
});
}

if ('when global createArgument with variadic then type is string[]') {
// 'when global createArgument with variadic then type is string[]'
{
const program = new Command();
program.addArgument(createArgument('<value...>')).action((arg) => {
expectType<string[]>(arg);
});
}

if ('when global createArgument with const choices then type is string union') {
// 'when global createArgument with const choices then type is string union'
{
const program = new Command();
program
.addArgument(createArgument('<value>').choices(['A', 'B', 'C'] as const))
Expand All @@ -54,9 +61,8 @@ if ('when global createArgument with const choices then type is string union') {
});
}

if (
'when global createArgument with variadic and const choices then type is array of string union'
) {
// 'when global createArgument with variadic and const choices then type is array of string union'
{
const program = new Command();
program
.addArgument(createArgument('<value...>').choices(['A', 'B', 'C'] as const))
Expand Down
32 changes: 16 additions & 16 deletions tests/create-option.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,62 @@ import { Command, createOption } from '..';

// Doing end-to-end test, rather than checking created Option directly.

if ('when cmd.createOption with boolean then type is boolean') {
// 'when cmd.createOption with boolean then type is boolean'
{
const program = new Command();
const foo = program
.addOption(program.createOption('-f, --foo', 'decription'))
.opts().foo;
expectType<true | undefined>(foo);
}

if ('when cmd.createOption with required option-argument then type is string') {
// 'when cmd.createOption with required option-argument then type is string'
{
const program = new Command();
const foo = program
.addOption(program.createOption('-f, --foo <value>', 'decription'))
.opts().foo;
expectType<string | undefined>(foo);
}

if (
'when cmd.createOption with optional option-argument then type is string|true'
) {
// 'when cmd.createOption with optional option-argument then type is string|true'
{
const program = new Command();
const foo = program
.addOption(program.createOption('-f, --foo [value]', 'decription'))
.opts().foo;
expectType<string | true | undefined>(foo);
}

if ('when global createOption with boolean then type is boolean') {
// 'when global createOption with boolean then type is boolean'
{
const program = new Command();
const foo = program
.addOption(createOption('-f, --foo', 'decription'))
.opts().foo;
expectType<true | undefined>(foo);
}

if (
'when global createOption with required option-argument then type is string'
) {
// 'when global createOption with required option-argument then type is string'
{
const program = new Command();
const foo = program
.addOption(createOption('-f, --foo <value>', 'decription'))
.opts().foo;
expectType<string | undefined>(foo);
}

if (
'when global createOption with optional option-argument then type is string|true'
) {
// 'when global createOption with optional option-argument then type is string|true'
{
const program = new Command();
const foo = program
.addOption(createOption('-f, --foo [value]', 'decription'))
.opts().foo;
expectType<string | true | undefined>(foo);
}

if ('when global createOption with const choices then type is string union') {
// 'when global createOption with const choices then type is string union'
{
const program = new Command();
const foo = program
.addOption(
Expand All @@ -71,9 +72,8 @@ if ('when global createOption with const choices then type is string union') {
expectType<'A' | 'B' | 'C' | undefined>(foo);
}

if (
'when global createOption with variadic and const choices then type is string union array'
) {
// 'when global createOption with variadic and const choices then type is string union array'
{
const program = new Command();
const foo = program
.addOption(
Expand Down
1 change: 1 addition & 0 deletions tests/global-createArgument.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from '@jest/globals';
import { createArgument } from '..';

test('when createArgument without description then argument has name', () => {
Expand Down
1 change: 1 addition & 0 deletions tests/global-createCommand.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from '@jest/globals';
import { createCommand } from '..';

test('when createCommand without name then command has empty name', () => {
Expand Down
1 change: 1 addition & 0 deletions tests/global-createOption.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from '@jest/globals';
import { createOption } from '..';

test('when createOption without description then option has flags', () => {
Expand Down
17 changes: 10 additions & 7 deletions tests/help.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,40 @@ import { Help, Command, CommandUnknownOpts, Option } from '..';
// Reminder: we pass the command into the Help methods using JavaScript so
// no type checking there, but subclass is TypeScript.

if (
'when subclass Help method with cmd arg as CommandUnknownOpts then no error'
) {
// 'when subclass Help method with cmd arg as CommandUnknownOpts then no error'
{
class MyHelp extends Help {
subcommandTerm(cmd: CommandUnknownOpts) {
return cmd.name();
}
}
}

if ('when subclass Help method with cmd arg as Command then no error') {
// 'when subclass Help method with cmd arg as Command then no error'
{
class MyHelp extends Help {
subcommandTerm(cmd: Command) {
return cmd.name();
}
}
}

if ('when pass type Command to visibleOptions then no error') {
// 'when pass type Command to visibleOptions then no error'
{
const program = new Command().option('--foo').argument('<one>');
program.createHelp().visibleOptions(program);
}

if ('when pass type CommandUnknownOpts to visibleOptions then no error') {
// 'when pass type CommandUnknownOpts to visibleOptions then no error'
{
const program: CommandUnknownOpts = new Command()
.option('--foo')
.argument('<one>');
program.createHelp().visibleOptions(program);
}

if ('when call visibleCommands then returns CommandUnknownOpts[]') {
// 'when call visibleCommands then returns CommandUnknownOpts[]'
{
const program = new Command().option('--foo').argument('<one>');
const vo = program.createHelp().visibleCommands(program);
expectType<CommandUnknownOpts[]>(vo);
Expand Down
18 changes: 12 additions & 6 deletions tests/hook.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expectType } from 'tsd';
import { Command } from '..';

if ('when add preAction hook then thisCommand strongly typed') {
// 'when add preAction hook then thisCommand strongly typed'
{
const program = new Command()
.option('-f, --foo <value>')
.hook('preAction', (thisCommand, actionCommand) => {
Expand All @@ -10,7 +11,8 @@ if ('when add preAction hook then thisCommand strongly typed') {
});
}

if ('when add preAction hook then activeCommand strongly typed') {
// 'when add preAction hook then activeCommand strongly typed'
{
const program = new Command()
.option('-f, --foo <value>')
.hook('preAction', (thisCommand, activeCommand) => {
Expand All @@ -19,7 +21,8 @@ if ('when add preAction hook then activeCommand strongly typed') {
});
}

if ('when add postAction hook then thisCommand strongly typed') {
// 'when add postAction hook then thisCommand strongly typed'
{
const program = new Command()
.option('-f, --foo <value>')
.hook('postAction', (thisCommand, actionCommand) => {
Expand All @@ -28,7 +31,8 @@ if ('when add postAction hook then thisCommand strongly typed') {
});
}

if ('when add postAction hook then activeCommand strongly typed') {
// 'when add postAction hook then activeCommand strongly typed'
{
const program = new Command()
.option('-f, --foo <value>')
.hook('postAction', (thisCommand, activeCommand) => {
Expand All @@ -37,7 +41,8 @@ if ('when add postAction hook then activeCommand strongly typed') {
});
}

if ('when add preSubcommand hook then thisCommand strongly typed') {
// 'when add preSubcommand hook then thisCommand strongly typed'
{
const program = new Command()
.option('-f, --foo <value>')
.hook('preSubcommand', (thisCommand, actionCommand) => {
Expand All @@ -46,7 +51,8 @@ if ('when add preSubcommand hook then thisCommand strongly typed') {
});
}

if ('when add preSubcommand hook then activeCommand strongly typed') {
// 'when add preSubcommand hook then activeCommand strongly typed'
{
const program = new Command()
.option('-f, --foo <value>')
.hook('preSubcommand', (thisCommand, activeCommand) => {
Expand Down
File renamed without changes.
6 changes: 4 additions & 2 deletions tests/option-value.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { expectType } from 'tsd';
import { Command } from '..';

if ('when getOptionValue is unknown then key is unknown') {
// 'when getOptionValue is unknown then key is unknown'
{
const program = new Command().option('-f, --foo');

const v = program.getOptionValue('bar');
expectType<unknown>(v);
}

if ('when getOptionValue result is typed then key is known') {
// 'when getOptionValue result is typed then key is known'
{
const program = new Command().option('-f, --foo');
const v = program.getOptionValue('foo');
expectType<true | undefined>(v);
Expand Down
Loading

0 comments on commit bce4491

Please sign in to comment.