From 748d22b2f23dd8e6ca5bb6d3462cb279a6d0b3a2 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Sat, 17 Nov 2018 13:08:12 +0800 Subject: [PATCH] virtualenv fixture and a test case Signed-off-by: Frost Ming --- news/3231.bugfix.rst | 1 + pipenv/environments.py | 3 ++- pipenv/project.py | 6 +++--- tests/integration/conftest.py | 21 +++++++++++++++++++++ tests/integration/test_project.py | 16 ++++++++++++++++ 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 news/3231.bugfix.rst diff --git a/news/3231.bugfix.rst b/news/3231.bugfix.rst new file mode 100644 index 00000000..036b2c12 --- /dev/null +++ b/news/3231.bugfix.rst @@ -0,0 +1 @@ +Correctly detect the virtualenv location inside an activated virtualenv. diff --git a/pipenv/environments.py b/pipenv/environments.py index 2d9bfaa8..45a4f6b8 100644 --- a/pipenv/environments.py +++ b/pipenv/environments.py @@ -278,7 +278,8 @@ def is_quiet(threshold=-1): def is_in_virtualenv(): pipenv_active = os.environ.get("PIPENV_ACTIVE") - return (PIPENV_USE_SYSTEM or PIPENV_VIRTUALENV) and not pipenv_active + virtual_env = os.environ.get("VIRTUAL_ENV") + return (PIPENV_USE_SYSTEM or virtual_env) and not pipenv_active PIPENV_SPINNER_FAIL_TEXT = fix_utf8(u"✘ {0}") if not PIPENV_HIDE_EMOJIS else ("{0}") diff --git a/pipenv/project.py b/pipenv/project.py index fa7adb3e..334782d9 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -43,7 +43,6 @@ from .environments import ( PIPENV_MAX_DEPTH, PIPENV_PIPFILE, PIPENV_VENV_IN_PROJECT, - PIPENV_VIRTUALENV, PIPENV_TEST_INDEX, PIPENV_PYTHON, PIPENV_DEFAULT_PYTHON_VERSION, @@ -427,8 +426,9 @@ class Project(object): @property def virtualenv_location(self): # if VIRTUAL_ENV is set, use that. - if PIPENV_VIRTUALENV: - return PIPENV_VIRTUALENV + virtualenv_env = os.getenv("VIRTUAL_ENV") + if virtualenv_env: + return virtualenv_env if not self._virtualenv_location: # Use cached version, if available. assert self.project_directory, "project not created" diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 6ed95b3e..0b27efe8 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -6,6 +6,8 @@ import warnings import pytest from pipenv._compat import TemporaryDirectory, Path +from pipenv.exceptions import VirtualenvActivationException +from pipenv.utils import temp_environ from pipenv.vendor import delegator from pipenv.vendor import requests from pipenv.vendor import toml @@ -239,3 +241,22 @@ def pip_src_dir(request, pathlib_tmpdir): @pytest.fixture() def testsroot(): return TESTS_ROOT + + +@pytest.fixture() +def virtualenv(pathlib_tmpdir): + virtualenv_path = pathlib_tmpdir / "venv" + with temp_environ(): + c = delegator.run("virtualenv {}".format(virtualenv_path), block=True) + assert c.return_code == 0 + for name in ("bin", "Scripts"): + activate_this = virtualenv_path / name / "activate_this.py" + if activate_this.exists(): + with open(str(activate_this)) as f: + code = compile(f.read(), str(activate_this), "exec") + exec(code, dict(__file__=str(activate_this))) + break + else: + raise VirtualenvActivationException("Can't find the activate_this.py script.") + os.environ["VIRTUAL_ENV"] = str(virtualenv_path) + yield virtualenv_path diff --git a/tests/integration/test_project.py b/tests/integration/test_project.py index fce8ea61..f4951886 100644 --- a/tests/integration/test_project.py +++ b/tests/integration/test_project.py @@ -180,3 +180,19 @@ def test_include_editable_packages(PipenvInstance, pypi, testsroot, pathlib_tmpd package.project_name for package in project.environment.get_installed_packages() ] + + +@pytest.mark.project +def test_run_in_virtualenv(PipenvInstance, pypi, virtualenv): + with PipenvInstance(chdir=True, pypi=pypi) as p: + project = Project() + assert project.virtualenv_location == str(virtualenv) + c = p.pipenv("run pip install click") + assert c.return_code == 0 + assert "Courtesy Notice" in c.err + c = p.pipenv('run python -c "import click;print(click.__file__)"') + assert c.return_code == 0 + assert c.out.strip().startswith(str(virtualenv)) + c = p.pipenv("clean --dry-run") + assert c.return_code == 0 + assert "click" in c.out