From f26c9b5235efb22918a99ecb054f0f2f7bad4d0b Mon Sep 17 00:00:00 2001 From: kobayashi Date: Sun, 3 Oct 2021 01:03:04 -0400 Subject: [PATCH] behave as no .venv if .venv is in project root and empty --- pipenv/project.py | 4 ++++ tests/integration/test_dot_venv.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/pipenv/project.py b/pipenv/project.py index abca0ea3..dd6fa7ed 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -270,6 +270,10 @@ class Project: with io.open(dot_venv) as f: name = f.read().strip() + # If .venv file is empty, set location based on config. + if not name: + return str(get_workon_home().joinpath(self.virtualenv_name)) + # If content looks like a path, use it as a relative path. # Otherwise use directory named after content in WORKON_HOME. if looks_like_dir(name): diff --git a/tests/integration/test_dot_venv.py b/tests/integration/test_dot_venv.py index ae51fbcd..85013153 100644 --- a/tests/integration/test_dot_venv.py +++ b/tests/integration/test_dot_venv.py @@ -76,6 +76,36 @@ def test_venv_file(venv_name, PipenvInstance): assert venv_path == normalize_drive(venv_expected_path) +@pytest.mark.dotvenv +def test_empty_venv_file(PipenvInstance): + """Tests virtualenv creation when a empty .venv file exists at the project root + """ + with PipenvInstance(chdir=True) as p: + file_path = os.path.join(p.path, '.venv') + with open(file_path, 'w') as f: + pass + + with temp_environ(), TemporaryDirectory( + prefix='pipenv-', suffix='temp_workon_home' + ) as workon_home: + os.environ['WORKON_HOME'] = workon_home.name + if 'PIPENV_VENV_IN_PROJECT' in os.environ: + del os.environ['PIPENV_VENV_IN_PROJECT'] + + 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() + from pathlib import PurePosixPath + venv_path = normalize_drive(venv_loc.as_posix()) + venv_path_parent = str(PurePosixPath(venv_path).parent) + assert venv_path_parent == Path(workon_home.name).absolute().as_posix() + + @pytest.mark.dotvenv def test_venv_file_with_path(PipenvInstance): """Tests virtualenv creation when a .venv file exists at the project root