diff --git a/docs/advanced.rst b/docs/advanced.rst index 2714c60c..28af1352 100644 --- a/docs/advanced.rst +++ b/docs/advanced.rst @@ -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: diff --git a/pipenv/utils/shell.py b/pipenv/utils/shell.py index 1668d757..2739021c 100644 --- a/pipenv/utils/shell.py +++ b/pipenv/utils/shell.py @@ -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): diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 703bfff6..2972b087 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -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