diff --git a/pipenv/project.py b/pipenv/project.py index 7e96db129c..9495370e97 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -427,16 +427,12 @@ def get_location_for_virtualenv(self) -> str: dot_venv = os.path.join(self.project_directory, ".venv") - # If there's no .venv in project root, set location based on config. - if not os.path.exists(dot_venv): + # If there's no .venv in project root or it is a folder, set location based on config. + if not os.path.exists(dot_venv) or os.path.isdir(dot_venv): if self.is_venv_in_project(): return dot_venv return str(get_workon_home().joinpath(self.virtualenv_name)) - # If .venv in project root is a directory, use it. - if os.path.isdir(dot_venv): - return dot_venv - # Now we assume .venv in project root is a file. Use its content. with open(dot_venv) as f: name = f.read().strip() diff --git a/tests/integration/test_dot_venv.py b/tests/integration/test_dot_venv.py index ce920e63da..b4ac5486fd 100644 --- a/tests/integration/test_dot_venv.py +++ b/tests/integration/test_dot_venv.py @@ -62,6 +62,29 @@ def test_venv_at_project_root(true_value, pipenv_instance_pypi): assert normalize_drive(p.path) in p.pipenv('--venv').stdout +@pytest.mark.dotvenv +@pytest.mark.parametrize("false_value", FALSE_VALUES) +def test_venv_in_project_disabled_with_existing_venv_dir(false_value, pipenv_instance_pypi): + venv_name = "my_project" + with temp_environ(), pipenv_instance_pypi() as p, TemporaryDirectory( + prefix="pipenv-", suffix="temp_workon_home" + ) as workon_home: + os.environ['PIPENV_VENV_IN_PROJECT'] = false_value + os.environ['PIPENV_CUSTOM_VENV_NAME'] = venv_name + os.environ["WORKON_HOME"] = workon_home + os.mkdir('.venv') + c = p.pipenv('install') + assert c.returncode == 0 + c = p.pipenv('--venv') + assert c.returncode == 0 + venv_loc = Path(c.stdout.strip()).absolute() + assert venv_loc.exists() + assert venv_loc.joinpath('.project').exists() + venv_path = normalize_drive(venv_loc.as_posix()) + venv_expected_path = Path(workon_home).joinpath(venv_name).absolute().as_posix() + assert venv_path == normalize_drive(venv_expected_path) + + @pytest.mark.dotvenv def test_reuse_previous_venv(pipenv_instance_pypi): with pipenv_instance_pypi() as p: