Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow zero default duration flags #470

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/flags/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export const durationFlag = Flags.custom<Duration, DurationFlagConfig>({
parse: async (input, _, opts) => validate(input, opts),
// eslint-disable-next-line @typescript-eslint/require-await
default: async (context) =>
context.options.defaultValue ? toDuration(context.options.defaultValue, context.options.unit) : undefined,
typeof context.options.defaultValue === 'number'
? toDuration(context.options.defaultValue, context.options.unit)
: undefined,
});

const validate = (input: string, config: DurationFlagConfig): Duration => {
Expand Down
23 changes: 23 additions & 0 deletions test/unit/flags/duration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ describe('duration flag', () => {
});
});

describe('defaultValue zero', () => {
const buildProps = {
flags: {
wait: durationFlag({
unit: 'hours',
description: 'test',
defaultValue: 0,
char: 'w',
}),
},
};
it('passes', async () => {
const out = await Parser.parse(['--wait=10'], buildProps);
expect(out.flags.wait?.quantity).to.equal(10);
expect(out.flags.wait?.unit).to.equal(Duration.Unit.HOURS);
});
it('passes using defaultValue', async () => {
const out = await Parser.parse([], buildProps);
expect(out.flags.wait?.quantity).to.equal(0);
expect(out.flags.wait?.unit).to.equal(Duration.Unit.HOURS);
});
});

describe('validation with no options and weeks unit', () => {
const defaultValue = 33;
const buildProps = {
Expand Down