Skip to content

Commit e351bbb

Browse files
authored
Merge pull request #13205 from wdhongtw/fix-path-case
Preserve original letter-casing for displaying
2 parents fe8851a + 845e981 commit e351bbb

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

news/6823.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Better preserve original casing when a path is displayed.

src/pip/_internal/utils/misc.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,12 @@ def rmtree_errorhandler(
182182
def display_path(path: str) -> str:
183183
"""Gives the display value for a given path, making it relative to cwd
184184
if possible."""
185-
path = os.path.normcase(os.path.abspath(path))
186-
if path.startswith(os.getcwd() + os.path.sep):
187-
path = "." + path[len(os.getcwd()) :]
188-
return path
185+
try:
186+
relative = Path(path).relative_to(Path.cwd())
187+
except ValueError:
188+
# If the path isn't relative to the CWD, leave it alone
189+
return path
190+
return os.path.join(".", relative)
189191

190192

191193
def backup_dir(dir: str, ext: str = ".bak") -> str:

tests/unit/test_utils.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from io import BytesIO
1515
from pathlib import Path
1616
from typing import Any, Callable, NoReturn
17-
from unittest.mock import Mock
17+
from unittest.mock import Mock, patch
1818

1919
import pytest
2020

@@ -33,6 +33,7 @@
3333
HiddenText,
3434
build_netloc,
3535
build_url_from_netloc,
36+
display_path,
3637
format_size,
3738
get_prog,
3839
hide_url,
@@ -320,6 +321,39 @@ def test_rmtree_retries_for_3sec(monkeypatch: pytest.MonkeyPatch) -> None:
320321
)
321322

322323

324+
class Test_display_path:
325+
on_unix = pytest.mark.skipif("sys.platform == 'win32'")
326+
on_win32 = pytest.mark.skipif("sys.platform != 'win32'")
327+
328+
@pytest.mark.parametrize(
329+
"path, fake_cwd, expected",
330+
[
331+
pytest.param(
332+
*("/home/name/project", Path("/home/name"), "./project"),
333+
marks=on_unix,
334+
),
335+
pytest.param(
336+
*("/home", Path("/home/name"), "/home"),
337+
marks=on_unix,
338+
id="not-go-up",
339+
),
340+
pytest.param(
341+
*("C:\\Name\\Project", Path("C:\\Name"), ".\\Project"),
342+
marks=on_win32,
343+
),
344+
pytest.param(
345+
*("D:\\Data", Path("C:\\Name"), "D:\\Data"),
346+
marks=on_win32,
347+
),
348+
],
349+
)
350+
def test_display(self, path: str, fake_cwd: Path, expected: str) -> None:
351+
with patch("pathlib.Path.cwd") as cwd_func:
352+
cwd_func.return_value = fake_cwd
353+
got = display_path(path)
354+
assert got == expected
355+
356+
323357
class Test_normalize_path:
324358
# Technically, symlinks are possible on Windows, but you need a special
325359
# permission bit to create them, and Python 2 doesn't support it anyway, so

0 commit comments

Comments
 (0)