diff --git a/news/4608.rst b/news/4608.rst new file mode 100644 index 0000000000..94395bff99 --- /dev/null +++ b/news/4608.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* #4608 Expand `'~'` in $VIRTUALENV_HOME when used by vox + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/tests/test_vox.py b/tests/test_vox.py index cf9942bf17..ee4f6998d4 100644 --- a/tests/test_vox.py +++ b/tests/test_vox.py @@ -7,6 +7,7 @@ import sys import types from typing import TYPE_CHECKING +from unittest.mock import ANY, MagicMock import pytest from py.path import local @@ -170,6 +171,31 @@ def test_vox_flow(xession, vox, record_events, venv_home): assert record_events.last == ("vox_on_delete", "spam") +@pytest.mark.parametrize( + "env_venv_home", + [ + pathlib.Path("~", ".custom_home_dir/"), + pathlib.Path("~root", "venv"), + pathlib.Path(".rel_venv"), + pathlib.Path("/tmp", "abs_venv"), + None, + ], +) +def test_venvdir_expand(xession, env_venv_home): + venv_name = "test_xonsh_venv" + + if env_venv_home: + xession.env["VIRTUALENV_HOME"] = str(env_venv_home) + expected_dir = str((env_venv_home / venv_name).expanduser()) + else: + expected_dir = str(pathlib.Path("~", ".virtualenvs", venv_name).expanduser()) + vox = Vox() + vox._create = MagicMock() + vox.create(venv_name) + + vox._create.assert_called_with(expected_dir, ANY, False, False, True, prompt=None) + + def test_activate_non_vox_venv(xession, vox, record_events, venv_proc, venv_home): """ Create a virtual environment using Python's built-in venv module diff --git a/xontrib/voxapi.py b/xontrib/voxapi.py index 01c79bacd4..78237bd616 100644 --- a/xontrib/voxapi.py +++ b/xontrib/voxapi.py @@ -22,6 +22,7 @@ # FIXME: Is there a better way? from xonsh.events import events from xonsh.platform import ON_POSIX, ON_WINDOWS +from xonsh.tools import expand_path events.doc( "vox_on_create", @@ -128,7 +129,7 @@ def __init__(self, force_removals=False): self.venvdir = os.path.join(home_path, ".virtualenvs") XSH.env["VIRTUALENV_HOME"] = self.venvdir else: - self.venvdir = XSH.env["VIRTUALENV_HOME"] + self.venvdir = expand_path(XSH.env["VIRTUALENV_HOME"]) self.force_removals = force_removals self.sub_dirs = _subdir_names()