Skip to content

Commit

Permalink
Resolve home path in dir env vars (#1241)
Browse files Browse the repository at this point in the history
* correctly resolve home directory in env var directories

* add test case for env dir not set

* fix news fragment file names
  • Loading branch information
Gitznik authored Feb 11, 2024
1 parent 407b797 commit 7136b3d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions changelog.d/94.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correctly resolve home directory in pipx directory environment variables.
14 changes: 10 additions & 4 deletions src/pipx/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@

from platformdirs import user_cache_path, user_data_path, user_log_path


def load_dir_from_environ(dir_name: str, default: Path) -> Path:
env = os.environ.get(dir_name, default)
return Path(os.path.expanduser(env)).resolve()


DEFAULT_PIPX_HOME = user_data_path("pipx")
FALLBACK_PIPX_HOME = Path.home() / ".local/pipx"
DEFAULT_PIPX_BIN_DIR = Path.home() / ".local/bin"
DEFAULT_PIPX_MAN_DIR = Path.home() / ".local/share/man"
MAN_SECTIONS = ["man%d" % i for i in range(1, 10)]

if FALLBACK_PIPX_HOME.exists() or os.environ.get("PIPX_HOME") is not None:
PIPX_HOME = Path(os.environ.get("PIPX_HOME", FALLBACK_PIPX_HOME)).resolve()
PIPX_HOME = load_dir_from_environ("PIPX_HOME", FALLBACK_PIPX_HOME)
PIPX_LOCAL_VENVS = PIPX_HOME / "venvs"
PIPX_STANDALONE_PYTHON_CACHEDIR = PIPX_HOME / "py"
PIPX_LOG_DIR = PIPX_HOME / "logs"
Expand All @@ -30,10 +36,10 @@
PIPX_TRASH_DIR = PIPX_HOME / "trash"
PIPX_VENV_CACHEDIR = user_cache_path("pipx")

PIPX_SHARED_LIBS = Path(os.environ.get("PIPX_SHARED_LIBS", DEFAULT_PIPX_SHARED_LIBS)).resolve()
PIPX_SHARED_LIBS = load_dir_from_environ("PIPX_SHARED_LIBS", DEFAULT_PIPX_SHARED_LIBS)
PIPX_SHARED_PTH = "pipx_shared.pth"
LOCAL_BIN_DIR = Path(os.environ.get("PIPX_BIN_DIR", DEFAULT_PIPX_BIN_DIR)).resolve()
LOCAL_MAN_DIR = Path(os.environ.get("PIPX_MAN_DIR", DEFAULT_PIPX_MAN_DIR)).resolve()
LOCAL_BIN_DIR = load_dir_from_environ("PIPX_BIN_DIR", DEFAULT_PIPX_BIN_DIR)
LOCAL_MAN_DIR = load_dir_from_environ("PIPX_MAN_DIR", DEFAULT_PIPX_MAN_DIR)
FETCH_MISSING_PYTHON = os.environ.get("PIPX_FETCH_MISSING_PYTHON", False)
TEMP_VENV_EXPIRATION_THRESHOLD_DAYS = 14
MINIMUM_PYTHON_VERSION = "3.8"
Expand Down
17 changes: 17 additions & 0 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from pathlib import Path

from helpers import run_pipx_cli
from pipx.constants import load_dir_from_environ


def test_cli(monkeypatch, capsys):
Expand Down Expand Up @@ -32,3 +35,17 @@ def test_cli_with_args(monkeypatch, capsys):
assert run_pipx_cli(["environment", "--value", "SSS"])
captured = capsys.readouterr()
assert "Variable not found." in captured.err


def test_resolve_user_dir_in_env_paths(monkeypatch):
monkeypatch.setenv("TEST_DIR", "~/test")
home = Path.home()
env_dir = load_dir_from_environ("TEST_DIR", Path.home())
assert "~" not in str(env_dir)
assert env_dir == home / "test"


def test_resolve_user_dir_in_env_paths_env_not_set(monkeypatch):
home = Path.home()
env_dir = load_dir_from_environ("TEST_DIR", Path.home())
assert env_dir == home

0 comments on commit 7136b3d

Please sign in to comment.