From 7c681cc139fcae933e38caacacd238b3e88cbd61 Mon Sep 17 00:00:00 2001 From: Haplo Date: Thu, 12 Jul 2018 02:36:36 -0500 Subject: [PATCH 1/5] Add ability to suppress nested venv courtesy notice Added an environment variable, PIPENV_SUPPRESS_NESTED_WARNING, to be used to suppress the courtesy notice from warn_in_virtualenv. Changed wording of the courtesy notice to mention suppression variable. Fixes #2527. --- pipenv/core.py | 15 ++++++++++++--- pipenv/environments.py | 8 ++++++++ tests/unit/test_core.py | 20 ++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/unit/test_core.py diff --git a/pipenv/core.py b/pipenv/core.py index 4ecf5d4a..4ffc69a6 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -1612,19 +1612,28 @@ def format_pip_output(out, r=None): def warn_in_virtualenv(): - from .environments import PIPENV_USE_SYSTEM, PIPENV_VIRTUALENV + from .environments import ( + PIPENV_USE_SYSTEM, + PIPENV_VIRTUALENV, + PIPENV_SUPPRESS_NESTED_WARNING, + ) # Only warn if pipenv isn't already active. pipenv_active = os.environ.get("PIPENV_ACTIVE") - if (PIPENV_USE_SYSTEM or PIPENV_VIRTUALENV) and not pipenv_active: + if ( + (PIPENV_USE_SYSTEM or PIPENV_VIRTUALENV) + and not (pipenv_active or PIPENV_SUPPRESS_NESTED_WARNING) + ): click.echo( "{0}: Pipenv found itself running within a virtual environment, " "so it will automatically use that environment, instead of " "creating its own for any project. You can set " "{1} to force pipenv to ignore that environment and create " - "its own instead.".format( + "its own instead. You can set {2} to suppress this " + "warning.".format( crayons.green("Courtesy Notice"), crayons.normal("PIPENV_IGNORE_VIRTUALENVS=1", bold=True), + crayons.normal("PIPENV_SUPPRESS_NESTED_WARNING=1", bold=True), ), err=True, ) diff --git a/pipenv/environments.py b/pipenv/environments.py index f241172a..f89fdf92 100644 --- a/pipenv/environments.py +++ b/pipenv/environments.py @@ -162,6 +162,14 @@ PIPENV_SHELL_FANCY = bool(os.environ.get("PIPENV_SHELL_FANCY")) Default is to use the compatibility shell if possible. """ +PIPENV_SUPPRESS_NESTED_WARNING = bool(os.environ.get( + 'PIPENV_SUPPRESS_NESTED_WARNING')) +"""If set, suppress the courtesy notice regarding nested virtual environments. + +Default is to show a courtesy notice when pipenv finds itself running inside +another virtual environment. +""" + PIPENV_TIMEOUT = int(os.environ.get("PIPENV_TIMEOUT", 120)) """Max number of seconds Pipenv will wait for virtualenv creation to complete. diff --git a/tests/unit/test_core.py b/tests/unit/test_core.py new file mode 100644 index 00000000..460075f4 --- /dev/null +++ b/tests/unit/test_core.py @@ -0,0 +1,20 @@ +import io + +import pytest +import mock +from contextlib import redirect_stderr + +from pipenv.core import warn_in_virtualenv + + +@mock.patch('pipenv.environments.PIPENV_VIRTUALENV', 'totallyrealenv') +@mock.patch('pipenv.environments.PIPENV_SUPPRESS_NESTED_WARNING', '1') +@pytest.mark.core +def test_suppress_nested_venv_warning(): + f = io.StringIO() + # Capture the stderr of warn_in_virtualenv to test for the presence of the + # courtesy notice. + with redirect_stderr(f): + warn_in_virtualenv() + output = f.getvalue() + assert 'Courtesy Notice' not in output From 118469db3b3fb35cb3c12fe7e0c6a54b4f1a0cbe Mon Sep 17 00:00:00 2001 From: Ethan Fulbright Date: Fri, 13 Jul 2018 21:15:46 -0500 Subject: [PATCH 2/5] Add verbosity int variable, use capsys in test_core Changed PIPENV_SUPPRESS_NESTED_WARNING to PIPENV_VERBOSITY, an integer value that defaults to 0 (verbose). Changed test in test_core.py to use capsys to capture standard output/error in order to improve compatibility with Python 2. --- pipenv/core.py | 6 +++--- pipenv/environments.py | 14 ++++++-------- tests/unit/test_core.py | 15 +++++---------- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/pipenv/core.py b/pipenv/core.py index 4ffc69a6..f8006eac 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -1615,14 +1615,14 @@ def warn_in_virtualenv(): from .environments import ( PIPENV_USE_SYSTEM, PIPENV_VIRTUALENV, - PIPENV_SUPPRESS_NESTED_WARNING, + PIPENV_VERBOSITY, ) # Only warn if pipenv isn't already active. pipenv_active = os.environ.get("PIPENV_ACTIVE") if ( (PIPENV_USE_SYSTEM or PIPENV_VIRTUALENV) - and not (pipenv_active or PIPENV_SUPPRESS_NESTED_WARNING) + and not (pipenv_active or PIPENV_VERBOSITY > 0) ): click.echo( "{0}: Pipenv found itself running within a virtual environment, " @@ -1633,7 +1633,7 @@ def warn_in_virtualenv(): "warning.".format( crayons.green("Courtesy Notice"), crayons.normal("PIPENV_IGNORE_VIRTUALENVS=1", bold=True), - crayons.normal("PIPENV_SUPPRESS_NESTED_WARNING=1", bold=True), + crayons.normal("PIPENV_VERBOSITY=1", bold=True), ), err=True, ) diff --git a/pipenv/environments.py b/pipenv/environments.py index f89fdf92..d76426fe 100644 --- a/pipenv/environments.py +++ b/pipenv/environments.py @@ -25,6 +25,12 @@ Some people don't like colors in their terminals, for some reason. Default is to show colors. """ +PIPENV_VERBOSITY = int(os.environ.get("PIPENV_VERBOSITY", "0")) +"""Verbosity setting for pipenv. Higher values make pipenv less verbose. + +Default is 0, for maximum verbosity. +""" + # Tells Pipenv which Python to default to, when none is provided. PIPENV_DEFAULT_PYTHON_VERSION = os.environ.get("PIPENV_DEFAULT_PYTHON_VERSION") """Use this Python version when creating new virtual environments by default. @@ -162,14 +168,6 @@ PIPENV_SHELL_FANCY = bool(os.environ.get("PIPENV_SHELL_FANCY")) Default is to use the compatibility shell if possible. """ -PIPENV_SUPPRESS_NESTED_WARNING = bool(os.environ.get( - 'PIPENV_SUPPRESS_NESTED_WARNING')) -"""If set, suppress the courtesy notice regarding nested virtual environments. - -Default is to show a courtesy notice when pipenv finds itself running inside -another virtual environment. -""" - PIPENV_TIMEOUT = int(os.environ.get("PIPENV_TIMEOUT", 120)) """Max number of seconds Pipenv will wait for virtualenv creation to complete. diff --git a/tests/unit/test_core.py b/tests/unit/test_core.py index 460075f4..7b19c085 100644 --- a/tests/unit/test_core.py +++ b/tests/unit/test_core.py @@ -1,20 +1,15 @@ -import io - import pytest import mock -from contextlib import redirect_stderr from pipenv.core import warn_in_virtualenv @mock.patch('pipenv.environments.PIPENV_VIRTUALENV', 'totallyrealenv') -@mock.patch('pipenv.environments.PIPENV_SUPPRESS_NESTED_WARNING', '1') +@mock.patch('pipenv.environments.PIPENV_VERBOSITY', 1) @pytest.mark.core -def test_suppress_nested_venv_warning(): - f = io.StringIO() +def test_suppress_nested_venv_warning(capsys): # Capture the stderr of warn_in_virtualenv to test for the presence of the # courtesy notice. - with redirect_stderr(f): - warn_in_virtualenv() - output = f.getvalue() - assert 'Courtesy Notice' not in output + warn_in_virtualenv() + output, err = capsys.readouterr() + assert 'Courtesy Notice' not in err From 7abd45f02df02dc45cd95671deb980d9286bea93 Mon Sep 17 00:00:00 2001 From: Ethan Fulbright Date: Fri, 13 Jul 2018 21:22:36 -0500 Subject: [PATCH 3/5] Move PIPENV_VERBOSITY to correct place in alphabet Oops. --- pipenv/environments.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pipenv/environments.py b/pipenv/environments.py index d76426fe..32c5e1c5 100644 --- a/pipenv/environments.py +++ b/pipenv/environments.py @@ -25,12 +25,6 @@ Some people don't like colors in their terminals, for some reason. Default is to show colors. """ -PIPENV_VERBOSITY = int(os.environ.get("PIPENV_VERBOSITY", "0")) -"""Verbosity setting for pipenv. Higher values make pipenv less verbose. - -Default is 0, for maximum verbosity. -""" - # Tells Pipenv which Python to default to, when none is provided. PIPENV_DEFAULT_PYTHON_VERSION = os.environ.get("PIPENV_DEFAULT_PYTHON_VERSION") """Use this Python version when creating new virtual environments by default. @@ -180,6 +174,12 @@ PIPENV_VENV_IN_PROJECT = bool(os.environ.get("PIPENV_VENV_IN_PROJECT")) Default is to create new virtual environments in a global location. """ +PIPENV_VERBOSITY = int(os.environ.get("PIPENV_VERBOSITY", "0")) +"""Verbosity setting for pipenv. Higher values make pipenv less verbose. + +Default is 0, for maximum verbosity. +""" + PIPENV_YES = bool(os.environ.get("PIPENV_YES")) """If set, Pipenv automatically assumes "yes" at all prompts. From cd82cdba632ab18d34acf65b75f3e34c978288bf Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Wed, 25 Jul 2018 15:28:55 +0800 Subject: [PATCH 4/5] Invert the verbosity flag (lower is less verbose) --- pipenv/core.py | 4 ++-- pipenv/environments.py | 7 ++++--- tests/unit/test_core.py | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pipenv/core.py b/pipenv/core.py index f8006eac..25bd0b7e 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -1622,7 +1622,7 @@ def warn_in_virtualenv(): pipenv_active = os.environ.get("PIPENV_ACTIVE") if ( (PIPENV_USE_SYSTEM or PIPENV_VIRTUALENV) - and not (pipenv_active or PIPENV_VERBOSITY > 0) + and not (pipenv_active or PIPENV_VERBOSITY < 0) ): click.echo( "{0}: Pipenv found itself running within a virtual environment, " @@ -1633,7 +1633,7 @@ def warn_in_virtualenv(): "warning.".format( crayons.green("Courtesy Notice"), crayons.normal("PIPENV_IGNORE_VIRTUALENVS=1", bold=True), - crayons.normal("PIPENV_VERBOSITY=1", bold=True), + crayons.normal("PIPENV_VERBOSITY=-1", bold=True), ), err=True, ) diff --git a/pipenv/environments.py b/pipenv/environments.py index 32c5e1c5..afe82988 100644 --- a/pipenv/environments.py +++ b/pipenv/environments.py @@ -174,10 +174,11 @@ PIPENV_VENV_IN_PROJECT = bool(os.environ.get("PIPENV_VENV_IN_PROJECT")) Default is to create new virtual environments in a global location. """ -PIPENV_VERBOSITY = int(os.environ.get("PIPENV_VERBOSITY", "0")) -"""Verbosity setting for pipenv. Higher values make pipenv less verbose. +PIPENV_VERBOSITY = int(os.environ.get("PIPENV_VERBOSITY", 0)) +"""Verbosity setting for pipenv. -Default is 0, for maximum verbosity. +Higher values make pipenv more verbose, lower values less so. Default is 0, +for normal verbosity. """ PIPENV_YES = bool(os.environ.get("PIPENV_YES")) diff --git a/tests/unit/test_core.py b/tests/unit/test_core.py index 7b19c085..e737a59e 100644 --- a/tests/unit/test_core.py +++ b/tests/unit/test_core.py @@ -5,7 +5,7 @@ from pipenv.core import warn_in_virtualenv @mock.patch('pipenv.environments.PIPENV_VIRTUALENV', 'totallyrealenv') -@mock.patch('pipenv.environments.PIPENV_VERBOSITY', 1) +@mock.patch('pipenv.environments.PIPENV_VERBOSITY', -1) @pytest.mark.core def test_suppress_nested_venv_warning(capsys): # Capture the stderr of warn_in_virtualenv to test for the presence of the From 68b1bce5031046f3cb441097fe7324104d2d4169 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Wed, 25 Jul 2018 15:36:03 +0800 Subject: [PATCH 5/5] News --- news/2527.feature | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 news/2527.feature diff --git a/news/2527.feature b/news/2527.feature new file mode 100644 index 00000000..e8148a59 --- /dev/null +++ b/news/2527.feature @@ -0,0 +1,2 @@ +Added environment variable `PIPENV_VERBOSITY` to control output verbosity +without needing to pass options.