Skip to content

Commit

Permalink
'chore: wrap venv_location with os.path.expanuser
Browse files Browse the repository at this point in the history
There was an edge case where the user could pass something like
`venv_locaiton="~/tmp/venv"`, but this would be place in a directory
`$PWD/~/tmp/venv`.  So now use `os.path.expanduser(venv_location)` to
fix this.
  • Loading branch information
wpk committed Feb 27, 2024
1 parent 8636ce3 commit ea8e444
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
7 changes: 5 additions & 2 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,11 @@ def tags(self) -> list[str]:

@property
def envdir(self) -> str:
return self.func.venv_location or _normalize_path(
self.global_config.envdir, self.friendly_name
if self.func.venv_location:
return os.path.expanduser(self.func.venv_location)
return _normalize_path(
self.global_config.envdir,
self.friendly_name,
)

def _create_venv(self) -> None:
Expand Down
30 changes: 27 additions & 3 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ def test_create_tmp_with_venv_location(self, change_to_tmp_path, pre_run):
)
assert session.env["TMPDIR"] == os.path.abspath(tmpdir)

@pytest.mark.parametrize("pre_run", [0, 1])
def test_create_tmp_with_venv_location_home_path(self, change_to_tmp_path, pre_run):
"""Test that this works with absolute path..."""
session, runner = self.make_session_and_runner(venv_location="~/my-location")
# for testing, also set envdir
with tempfile.TemporaryDirectory() as root:
runner.global_config.envdir = root
for _ in range(pre_run):
session.create_tmp()

tmpdir = session.create_tmp()

assert tmpdir == os.path.join(os.path.expanduser("~/my-location"), "tmp")
assert tmpdir == os.path.abspath(tmpdir)
assert session.env["TMPDIR"] == os.path.abspath(tmpdir)

def test_properties(self):
session, runner = self.make_session_and_runner()
with tempfile.TemporaryDirectory() as root:
Expand Down Expand Up @@ -957,7 +973,7 @@ def test__create_venv(self, create):
assert runner.venv.interpreter is None
assert runner.venv.reuse_existing is False

@pytest.mark.parametrize("venv_location", [None, "my-location"])
@pytest.mark.parametrize("venv_location", [None, "my-location", "~/my-location"])
@pytest.mark.parametrize(
"create_method,venv_backend,expected_backend",
[
Expand Down Expand Up @@ -988,13 +1004,21 @@ def test__create_venv_options(
assert runner.venv.interpreter == "coolpython"
assert runner.venv.reuse_existing is True

location_name = venv_location or nox.sessions._normalize_path(
runner.global_config.envdir, runner.friendly_name
location_name = (
os.path.expanduser(venv_location)
if venv_location
else nox.sessions._normalize_path(
runner.global_config.envdir, runner.friendly_name
)
)

assert runner.venv.location_name == location_name
assert runner.venv.location == os.path.abspath(location_name)
assert runner.envdir == location_name

if venv_location and "~/" in venv_location:
assert runner.venv.location_name == runner.venv.location

def test__create_venv_unexpected_venv_backend(self):
runner = self.make_runner()
runner.func.venv_backend = "somenewenvtool"
Expand Down

0 comments on commit ea8e444

Please sign in to comment.