Skip to content

Commit

Permalink
Merge pull request #2223 from pallets/env-var
Browse files Browse the repository at this point in the history
treat empty auto_envvar as None
  • Loading branch information
davidism committed Mar 28, 2022
2 parents ef11be6 + 8d7f03d commit 20c88f0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -52,6 +52,9 @@ Version 8.1.0
``name``. :issue:`2168`
- Shell completion prioritizes option values with option prefixes over
new options. :issue:`2040`
- Options that get an environment variable value using
``autoenvvar_prefix`` treat an empty value as ``None``, consistent
with a direct ``envvar``. :issue:`2146`


Version 8.0.4
Expand Down
5 changes: 4 additions & 1 deletion src/click/core.py
Expand Up @@ -2834,7 +2834,10 @@ def resolve_envvar_value(self, ctx: Context) -> t.Optional[str]:
envvar = f"{ctx.auto_envvar_prefix}_{self.name.upper()}"
rv = os.environ.get(envvar)

return rv
if rv:
return rv

return None

def value_from_envvar(self, ctx: Context) -> t.Optional[t.Any]:
rv: t.Optional[t.Any] = self.resolve_envvar_value(ctx)
Expand Down
7 changes: 4 additions & 3 deletions tests/test_options.py
Expand Up @@ -153,14 +153,15 @@ def test_init_bad_default_list(runner, multiple, nargs, default):
click.Option(["-a"], type=type, multiple=multiple, nargs=nargs, default=default)


def test_empty_envvar(runner):
@pytest.mark.parametrize("env_key", ["MYPATH", "AUTO_MYPATH"])
def test_empty_envvar(runner, env_key):
@click.command()
@click.option("--mypath", type=click.Path(exists=True), envvar="MYPATH")
def cli(mypath):
click.echo(f"mypath: {mypath}")

result = runner.invoke(cli, [], env={"MYPATH": ""})
assert result.exit_code == 0
result = runner.invoke(cli, env={env_key: ""}, auto_envvar_prefix="AUTO")
assert result.exception is None
assert result.output == "mypath: None\n"


Expand Down

0 comments on commit 20c88f0

Please sign in to comment.