Skip to content
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
1 change: 1 addition & 0 deletions news/6823.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Better preserve original casing when a path is displayed.
10 changes: 6 additions & 4 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,12 @@ def rmtree_errorhandler(
def display_path(path: str) -> str:
"""Gives the display value for a given path, making it relative to cwd
if possible."""
path = os.path.normcase(os.path.abspath(path))
if path.startswith(os.getcwd() + os.path.sep):
path = "." + path[len(os.getcwd()) :]
return path
try:
relative = Path(path).relative_to(Path.cwd())
except ValueError:
# If the path isn't relative to the CWD, leave it alone
return path
return os.path.join(".", relative)


def backup_dir(dir: str, ext: str = ".bak") -> str:
Expand Down
36 changes: 35 additions & 1 deletion tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from io import BytesIO
from pathlib import Path
from typing import Any, Callable, NoReturn
from unittest.mock import Mock
from unittest.mock import Mock, patch

import pytest

Expand All @@ -33,6 +33,7 @@
HiddenText,
build_netloc,
build_url_from_netloc,
display_path,
format_size,
get_prog,
hide_url,
Expand Down Expand Up @@ -320,6 +321,39 @@ def test_rmtree_retries_for_3sec(monkeypatch: pytest.MonkeyPatch) -> None:
)


class Test_display_path:
on_unix = pytest.mark.skipif("sys.platform == 'win32'")
on_win32 = pytest.mark.skipif("sys.platform != 'win32'")

@pytest.mark.parametrize(
"path, fake_cwd, expected",
[
pytest.param(
*("/home/name/project", Path("/home/name"), "./project"),
marks=on_unix,
),
pytest.param(
*("/home", Path("/home/name"), "/home"),
marks=on_unix,
id="not-go-up",
),
pytest.param(
*("C:\\Name\\Project", Path("C:\\Name"), ".\\Project"),
marks=on_win32,
),
pytest.param(
*("D:\\Data", Path("C:\\Name"), "D:\\Data"),
marks=on_win32,
),
],
)
def test_display(self, path: str, fake_cwd: Path, expected: str) -> None:
with patch("pathlib.Path.cwd") as cwd_func:
cwd_func.return_value = fake_cwd
got = display_path(path)
assert got == expected


class Test_normalize_path:
# Technically, symlinks are possible on Windows, but you need a special
# permission bit to create them, and Python 2 doesn't support it anyway, so
Expand Down