Skip to content
Myr

Merge pull request #874 from rgreinho/ISSUE-873/variable-precedence

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix variable precedence

authored by Zachery Bir · committed by GitHub · parents df1a8f5, 535559a · Browse files · 4 files+53 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+53 added−2 removed

  • tests:+48 added−0 removed
  • other:+5 added−2 removed

4 files+53 added−2 removed

modified:click/core.py+3 added-2 removed
1326 unmodified lines
13271328 def add_to_parser(self, parser, ctx):1329 pass133013311332 def consume_value(self, ctx, opts):1333 value = opts.get(self.name)1334 if value is None:1334 value = ctx.lookup_default(self.name)1335 if value is None:1335 value = self.value_from_envvar(ctx)1336 if value is None:1337 value = ctx.lookup_default(self.name)1338 return value13391340 def type_cast_value(self, ctx, value):1341 """Given a value this runs it properly through the type system.
423 unmodified lines
modified:tests/test_commands.py+48 added
1# -*- coding: utf-8 -*-2import re34import click5import pytest678def test_other_command_invoke(runner):9 @click.command()
258 unmodified lines
268269 result = runner.invoke(cli, ['foo-bar'])270 assert not result.exception271 assert result.output.splitlines() == ['foo-bar']272273274def test_environment_variables(runner):275 @click.group()276 def cli():277 pass278279 @cli.command()280 @click.option('--name', envvar='CLICK_NAME')281 def foo(name):282 click.echo(name)283284 result = runner.invoke(cli, ['foo'], env={'CLICK_NAME': 'environment'})285286 assert not result.exception287 assert result.output == 'environment\n'288289290# Ensures the variables are read in the following order:291# 1. CLI292# 2. Environment293# 3. Defaults294variable_precedence_testdata = [295 (['foo', '--name=cli'], {'CLICK_NAME': 'environment'}, 'cli\n'),296 (['foo'], {'CLICK_NAME': 'environment'}, 'environment\n'),297 (['foo'], None, 'defaults\n'),298]299300301@pytest.mark.parametrize("command,environment,expected",302 variable_precedence_testdata)303def test_variable_precendence_00(runner, command, environment, expected):304 @click.group()305 def cli():306 pass307308 @cli.command()309 @click.option('--name', envvar='CLICK_NAME')310 def foo(name):311 click.echo(name)312313 defaults = {'foo': {'name': 'defaults'}}314 result = runner.invoke(cli, command, default_map=defaults, env=environment)315316 assert not result.exception317 assert result.output == expected
modified:.gitignore+1 added
6 unmodified lines
7dist8build9docs/_build10click.egg-info11venv/12.tox13.cache14.ropeproject15.idea
modified:tox.ini+1 added
1[tox]2envlist = py27,py34,py35,py36,pypy3skip_missing_interpreters = true45[testenv]6passenv = LANG7commands = {env:TEST_RUNNER:pytest} {posargs}
6 unmodified lines