Add default kwarg to get_from_env

This commit is contained in:
Micah Smith
2022-10-25 10:56:09 -04:00
parent 932e98637a
commit d1adb504be
2 changed files with 31 additions and 9 deletions
+4 -2
View File
@@ -25,7 +25,7 @@ def _is_env_truthy(name):
return os.environ.get(name).lower() not in FALSE_VALUES
def get_from_env(arg, prefix="PIPENV", check_for_negation=True):
def get_from_env(arg, prefix="PIPENV", check_for_negation=True, default=None):
"""
Check the environment for a variable, returning its truthy or stringified value
@@ -36,6 +36,8 @@ def get_from_env(arg, prefix="PIPENV", check_for_negation=True):
:param str prefix: The prefix to attach to the variable, defaults to "PIPENV"
:param bool check_for_negation: Whether to check for ``<PREFIX>_NO_<arg>``, defaults
to True
:param Optional[Union[str, bool]] default: The value to return if the environment variable does
not exist, defaults to None
:return: The value from the environment if available
:rtype: Optional[Union[str, bool]]
"""
@@ -56,7 +58,7 @@ def get_from_env(arg, prefix="PIPENV", check_for_negation=True):
return not env_to_bool(value)
except ValueError:
return value
return None
return default
def normalize_pipfile_path(p):
+27 -7
View File
@@ -31,19 +31,17 @@ def test_get_from_env(arg, prefix, use_negation):
main_expected_value = False
# use negation means if the normal variable isnt set we will check
# for the negated version
negative_expected_value = (
True if is_negative else None
)
negative_expected_value = True if is_negative else None
if is_positive:
assert (
environments.get_from_env(
var_to_set, prefix, check_for_negation=use_negation
var_to_set, prefix=prefix, check_for_negation=use_negation
)
is main_expected_value
)
assert (
environments.get_from_env(
opposite_var, prefix, check_for_negation=use_negation
opposite_var, prefix=prefix, check_for_negation=use_negation
)
is negative_expected_value
)
@@ -54,7 +52,7 @@ def test_get_from_env(arg, prefix, use_negation):
# get NO_BLAH -- expecting this to be True
assert (
environments.get_from_env(
var_to_set, prefix, check_for_negation=use_negation
var_to_set, prefix=prefix, check_for_negation=use_negation
)
is negative_expected_value
)
@@ -62,7 +60,29 @@ def test_get_from_env(arg, prefix, use_negation):
# but otherwise should be None
assert (
environments.get_from_env(
opposite_var, prefix, check_for_negation=use_negation
opposite_var, prefix=prefix, check_for_negation=use_negation
)
is main_expected_value
)
@pytest.mark.environments
@pytest.mark.parametrize(
"check_for_negation, default",
list(itertools.product((True, False), (None, "default", 1))),
)
def test_get_from_env_default(check_for_negation, default):
"""When the desired env var does"""
arg = "ENABLE_SOMETHING"
prefix = "FAKEPREFIX"
envvar = f"{prefix}_{arg}"
negated_envvar = f"{prefix}_NO_{arg}"
with temp_environ():
os.environ.pop(envvar, None)
os.environ.pop(negated_envvar, None)
assert (
environments.get_from_env(
arg, prefix=prefix, check_for_negation=check_for_negation, default=default
)
== default
)