Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore existing venv dir when PIPENV_VENV_IN_PROJECT is false #6009

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions pipenv/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,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