Fix is_env_truthy

This commit is contained in:
Micah Smith
2022-11-02 11:58:29 -04:00
parent 1a714b05de
commit 258d82f21d
3 changed files with 50 additions and 10 deletions
+1 -1
View File
@@ -636,7 +636,7 @@ truthy value (i.e. ``"1"``)::
$ PIPENV_IGNORE_VIRTUALENVS=1
To explicitly disable a boolean option, assign to it a falsy value (i.e. ``"0"``).
To explicitly disable a boolean option, assign to it a falsey value (i.e. ``"0"``).
.. autoclass:: pipenv.environments.Setting
:members:
+13 -6
View File
@@ -395,21 +395,28 @@ def env_to_bool(val):
Convert **val** to boolean, returning True if truthy or False if falsey
:param Any val: The value to convert
:return: False if Falsey, True if truthy
:return: False if falsey, True if truthy
:rtype: bool
:raises:
ValueError: if val is not a valid boolean-like
"""
if isinstance(val, bool):
return val
if val.lower() in FALSE_VALUES:
return False
if val.lower() in TRUE_VALUES:
return True
try:
if val.lower() in FALSE_VALUES:
return False
if val.lower() in TRUE_VALUES:
return True
except AttributeError:
pass
raise ValueError(f"Value is not a valid boolean-like: {val}")
def is_env_truthy(name):
"""An environment variable is truthy if it exists and isn't one of (0, false, no, off)"""
return env_to_bool(os.getenv(name))
return env_to_bool(os.getenv(name, False))
def project_python(project, system=False):
+36 -3
View File
@@ -3,7 +3,6 @@ from unittest import mock
import pytest
import pipenv.utils.shell
from pipenv.utils import dependencies
from pipenv.utils import indexes
from pipenv.utils import internet
@@ -338,7 +337,7 @@ twine = "*"
)
@pytest.mark.skipif(os.name != "nt", reason="Windows file paths tested")
def test_win_normalize_drive(self, input_path, expected):
assert pipenv.utils.shell.normalize_drive(input_path) == expected
assert shell.normalize_drive(input_path) == expected
@pytest.mark.utils
@pytest.mark.parametrize(
@@ -352,7 +351,7 @@ twine = "*"
)
@pytest.mark.skipif(os.name == "nt", reason="*nix file paths tested")
def test_nix_normalize_drive(self, input_path, expected):
assert pipenv.utils.shell.normalize_drive(input_path) == expected
assert shell.normalize_drive(input_path) == expected
@pytest.mark.utils
@pytest.mark.parametrize(
@@ -490,3 +489,37 @@ twine = "*"
python = shell.project_python(project, system=True)
assert python == "/usr/local/bin/python3"
@pytest.mark.utils
@pytest.mark.parametrize(
"val, expected",
(
(True, True),
(False, False),
("true", True),
("1", True),
("off", False),
("0", False),
)
)
def test_env_to_bool(self, val, expected):
actual = shell.env_to_bool(val)
assert actual == expected
@pytest.mark.utils
def test_is_env_truthy_exists_true(self, monkeypatch):
name = "ZZZ"
monkeypatch.setenv(name, "1")
assert shell.is_env_truthy(name) is True
@pytest.mark.utils
def test_is_env_truthy_exists_false(self, monkeypatch):
name = "ZZZ"
monkeypatch.setenv(name, "0")
assert shell.is_env_truthy(name) is False
@pytest.mark.utils
def test_is_env_truthy_does_not_exisxt(self, monkeypatch):
name = "ZZZ"
monkeypatch.delenv(name, raising=False)
assert shell.is_env_truthy(name) is False