Skip to content

Commit

Permalink
Ignore existing venv dir when PIPENV_VENV_IN_PROJECT is false (#6009)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-dezandee committed Nov 14, 2023
1 parent cd961a4 commit bb0ca90
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
8 changes: 2 additions & 6 deletions pipenv/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
23 changes: 23 additions & 0 deletions tests/integration/test_dot_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit bb0ca90

Please sign in to comment.