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: ignore unset environment variables #536

Merged
merged 1 commit into from Oct 31, 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
2 changes: 1 addition & 1 deletion src/parser/parse.ts
Expand Up @@ -207,7 +207,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
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be 100% clear, both of these tests fail when run against main, because in both cases the absent env var is interpreted as falsey:

  2 failing

  1) parse
       env
         boolean
           ignores unset environment variables:
     AssertionError: expected false to be undefined
      at Context.<anonymous> (test/parser/parse.test.ts:921:36)

  2) parse
       env
         boolean
           uses default when environment variable is unset:

      AssertionError: expected false to be true
      + expected - actual

      -false
      +true
      
      at Context.<anonymous> (test/parser/parse.test.ts:931:36)

})
})

Expand Down