Skip to content

Commit

Permalink
Ignore unset environment variables (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
amcaplan committed Oct 31, 2022
1 parent 5db7ff3 commit 13c3d9e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/parser/parse.ts
Expand Up @@ -211,7 +211,7 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
for (const k of Object.keys(this.input.flags)) {
const flag = this.input.flags[k]
if (flags[k]) continue
if (flag.env) {
if (flag.env && Object.prototype.hasOwnProperty.call(process.env, flag.env)) {
const input = process.env[flag.env]
if (flag.type === 'option') {
if (input) {
Expand Down
20 changes: 20 additions & 0 deletions test/parser/parse.test.ts
Expand Up @@ -910,6 +910,26 @@ See more help with --help`)
delete process.env.TEST_FOO
})
}

it('ignores unset environment variables', async () => {
delete process.env.TEST_FOO
const out = await parse([], {
flags: {
foo: flags.boolean({env: 'TEST_FOO'}),
},
})
expect(out.flags.foo).to.be.undefined
})

it('uses default when environment variable is unset', async () => {
delete process.env.TEST_FOO
const out = await parse([], {
flags: {
foo: flags.boolean({env: 'TEST_FOO', default: true}),
},
})
expect(out.flags.foo).to.be.true
})
})
})

Expand Down

0 comments on commit 13c3d9e

Please sign in to comment.