From ea8e444367e411d64f79dab459ba559c4a026375 Mon Sep 17 00:00:00 2001 From: wpk Date: Tue, 27 Feb 2024 15:05:33 -0500 Subject: [PATCH] 'chore: wrap venv_location with os.path.expanuser 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. --- nox/sessions.py | 7 +++++-- tests/test_sessions.py | 30 +++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/nox/sessions.py b/nox/sessions.py index c513d641..f87cca4a 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -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: diff --git a/tests/test_sessions.py b/tests/test_sessions.py index 49bd609a..29f3cd8f 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -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: @@ -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", [ @@ -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"