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: parser doesn't validate against options parameter if the value is provided through an env var #474

Merged
merged 1 commit into from
Aug 16, 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
17 changes: 12 additions & 5 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
flags[token.flag] = await flag.parse(flags[token.flag], this.context)
} else {
const input = token.input
if (flag.options && !flag.options.includes(input)) {
throw new m.errors.FlagInvalidOptionError(flag, input)
}
this._validateOptions(flag, input)

// eslint-disable-next-line no-await-in-loop
const value = flag.parse ? await flag.parse(input, this.context) : input
Expand All @@ -201,8 +199,12 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
if (flags[k]) continue
if (flag.type === 'option' && flag.env) {
const input = process.env[flag.env]
// eslint-disable-next-line no-await-in-loop
if (input) flags[k] = await flag.parse(input, this.context)
if (input) {
this._validateOptions(flag, input)

// eslint-disable-next-line no-await-in-loop
flags[k] = await flag.parse(input, this.context)
}
}

if (!(k in flags) && flag.default !== undefined) {
Expand All @@ -216,6 +218,11 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
return flags
}

private _validateOptions(flag: OptionFlag<any>, input: string) {
if (flag.options && !flag.options.includes(input))
throw new m.errors.FlagInvalidOptionError(flag, input)
}

private async _argv(): Promise<any[]> {
const args: any[] = []
const tokens = this._argTokens
Expand Down
22 changes: 22 additions & 0 deletions test/parser/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,28 @@ See more help with --help`)

expect(message).to.equal('Expected --foo=invalidopt to be one of: myopt, myotheropt\nSee more help with --help')
})
it('fails when invalid env var', async () => {
let message = ''
process.env.TEST_FOO = 'invalidopt'
try {
await parse([], {
flags: {foo: flags.string({options: ['myopt', 'myotheropt'], env: 'TEST_FOO'})},
})
} catch (error: any) {
message = error.message
}

expect(message).to.equal('Expected --foo=invalidopt to be one of: myopt, myotheropt\nSee more help with --help')
})

it('accepts valid option env var', async () => {
process.env.TEST_FOO = 'myopt'

const out = await parse([], {
flags: {foo: flags.string({options: ['myopt', 'myotheropt'], env: 'TEST_FOO'})},
})
expect(out.flags.foo).to.equal('myopt')
})
})

describe('url flag', () => {
Expand Down