diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | click/core.py | 5 | ||||
-rw-r--r-- | tests/test_commands.py | 48 | ||||
-rw-r--r-- | tox.ini | 1 |
4 files changed, 53 insertions, 2 deletions
@@ -8,6 +8,7 @@ dist build docs/_build click.egg-info +venv/ .tox .cache .ropeproject diff --git a/click/core.py b/click/core.py index a8962be..9498850 100644 --- a/click/core.py +++ b/click/core.py @@ -1328,12 +1328,13 @@ class Parameter(object): def add_to_parser(self, parser, ctx): pass + def consume_value(self, ctx, opts): value = opts.get(self.name) if value is None: - value = ctx.lookup_default(self.name) - if value is None: value = self.value_from_envvar(ctx) + if value is None: + value = ctx.lookup_default(self.name) return value def type_cast_value(self, ctx, value): diff --git a/tests/test_commands.py b/tests/test_commands.py index e900c43..e8a9535 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- import re + import click +import pytest def test_other_command_invoke(runner): @@ -267,3 +269,49 @@ def test_subcommand_naming(runner): result = runner.invoke(cli, ['foo-bar']) assert not result.exception assert result.output.splitlines() == ['foo-bar'] + + +def test_environment_variables(runner): + @click.group() + def cli(): + pass + + @cli.command() + @click.option('--name', envvar='CLICK_NAME') + def foo(name): + click.echo(name) + + result = runner.invoke(cli, ['foo'], env={'CLICK_NAME': 'environment'}) + + assert not result.exception + assert result.output == 'environment\n' + + +# Ensures the variables are read in the following order: +# 1. CLI +# 2. Environment +# 3. Defaults +variable_precedence_testdata = [ + (['foo', '--name=cli'], {'CLICK_NAME': 'environment'}, 'cli\n'), + (['foo'], {'CLICK_NAME': 'environment'}, 'environment\n'), + (['foo'], None, 'defaults\n'), +] + + +@pytest.mark.parametrize("command,environment,expected", + variable_precedence_testdata) +def test_variable_precendence_00(runner, command, environment, expected): + @click.group() + def cli(): + pass + + @cli.command() + @click.option('--name', envvar='CLICK_NAME') + def foo(name): + click.echo(name) + + defaults = {'foo': {'name': 'defaults'}} + result = runner.invoke(cli, command, default_map=defaults, env=environment) + + assert not result.exception + assert result.output == expected @@ -1,5 +1,6 @@ [tox] envlist = py27,py34,py35,py36,pypy +skip_missing_interpreters = true [testenv] passenv = LANG |