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

allow flags to have false value in when #557

Merged
merged 2 commits into from Dec 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/parser/validate.ts
Expand Up @@ -82,7 +82,7 @@ export async function validate(parse: {

function getPresentFlags(flags: Record<string, unknown>): string[] {
return Object.keys(flags).reduce((acc, key) => {
if (flags[key]) acc.push(key)
if (flags[key] !== undefined) acc.push(key)
return acc
}, [] as string[])
}
Expand Down Expand Up @@ -113,7 +113,7 @@ export async function validate(parse: {
continue
if (parse.output.metadata.flags && parse.output.metadata.flags[name]?.setFromDefault)
continue
if (parse.output.flags[flag]) {
if (parse.output.flags[flag] !== undefined) {
return {...base, status: 'failed', reason: `--${flag}=${parse.output.flags[flag]} cannot also be provided when using --${name}`}
}
}
Expand All @@ -126,7 +126,7 @@ export async function validate(parse: {
const resolved = await resolveFlags(flags)
const keys = getPresentFlags(resolved)
for (const flag of keys) {
if (flag !== name && parse.output.flags[flag]) {
if (flag !== name && parse.output.flags[flag] !== undefined) {
return {...base, status: 'failed', reason: `--${flag} cannot also be provided when using --${name}`}
}
}
Expand All @@ -137,7 +137,7 @@ export async function validate(parse: {
async function validateDependsOn(name: string, flags: FlagRelationship[]): Promise<Validation> {
const base = {name, validationFn: 'validateDependsOn'}
const resolved = await resolveFlags(flags)
const foundAll = Object.values(resolved).every(Boolean)
const foundAll = Object.values(resolved).every(val => val !== undefined)
if (!foundAll) {
const formattedFlags = Object.keys(resolved).map(f => `--${f}`).join(', ')
return {...base, status: 'failed', reason: `All of the following must be provided when using --${name}: ${formattedFlags}`}
Expand Down
82 changes: 82 additions & 0 deletions test/parser/validate.test.ts
Expand Up @@ -748,6 +748,88 @@ describe('validate', () => {
})
})

it('should pass if the specified flags whose when property resolves to true, flag has a false value', async () => {
const input = {
argv: [],
flags: {
cookies: {input: [], name: 'cookies'},
sprinkles: {input: [], name: 'sprinkles'},
dessert: {
input: [],
name: 'dessert',
relationships: [
{
type: 'all',
flags: [
'sprinkles',
{name: 'cookies', when: async () => Promise.resolve(true)},
],
},
],
},
},
args: [],
strict: true,
context: {},
'--': true,
}

const output = {
args: {},
argv: [],
flags: {sprinkles: true, dessert: 'ice-cream', cookies: false},
raw: [
{type: 'flag', flag: 'sprinkles', input: true},
{type: 'flag', flag: 'dessert', input: 'ice-cream'},
{type: 'flag', flag: 'cookies', input: false},
],
metadata: {},
}

// @ts-expect-error
await validate({input, output})
})

it('should fail if the specified flags whose when property resolves to true in exclusive, flag has a false value', async () => {
const input = {
argv: [],
flags: {
cookies: {input: [], name: 'cookies'},
sprinkles: {input: [], name: 'sprinkles'},
dessert: {
input: [],
name: 'dessert',
exclusive: [{name: 'cookies', when: async () => Promise.resolve(true)}],
},
},
args: [],
strict: true,
context: {},
'--': true,
}

const output = {
args: {},
argv: [],
flags: {sprinkles: true, dessert: 'ice-cream', cookies: false},
raw: [
{type: 'flag', flag: 'sprinkles', input: true},
{type: 'flag', flag: 'dessert', input: 'ice-cream'},
{type: 'flag', flag: 'cookies', input: false},
],
metadata: {},
}

try {
// @ts-expect-error
await validate({input, output})
assert.fail('should have thrown')
} catch (error) {
const err = error as CLIError
expect(err.message).to.include('--cookies=false cannot also be provided when using --dessert')
}
})

describe('mixed', () => {
const input = {
argv: [],
Expand Down