-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Preserve original letter-casing for displaying #13205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
bb3f5ba
to
c0c9596
Compare
Modified according to review feedbacks. Since that (Edited: synchronize the script content to latest version of this PR) from typing import NamedTuple
import os
def display_path(path: str) -> str:
"""Gives the display value for a given path, making it relative to cwd
if possible."""
rel = os.path.relpath(path)
if rel[:2] == "..":
return path
return os.path.join(".", rel)
class Case(NamedTuple):
input: str
expected: str
testCases = [
Case(
input=r"C:\Users\weida\Projects\pip\main.py",
expected=r".\main.py",
),
Case(
input=r"C:\Users\weida\Projects\pip\main.PY",
expected=r".\main.PY",
),
Case(
input=r"C:\Users\weida\Projects\PIP\main.py",
expected=r".\main.py",
),
Case(
input=r"C:\Users\WeiDa\Projects\ABC\main.PY",
expected=r"C:\Users\WeiDa\Projects\ABC\main.PY",
),
]
# modify this path if needed
assert os.getcwd() == r"C:\Users\weida\Projects\pip"
for idx, test in enumerate(testCases):
result = display_path(test.input)
if result == test.expected:
print(f"Test case {idx} passed.")
else:
print(f"Test case {idx} failed: got {result}, expected {test.expected}.") |
Is there a reason why we can't use cc @pfmoore given this primarily impacts Windows. |
No such reason. |
Simplify input path as a relative path to current working directory, while preserving the original letter-casing at best effort.
This change will preserve path in it's original letter-case during displaying.
Related to: #6823
Becauseos.getcwd
andos.path.abspath
both honors original letter-case,we should not normalize case again when calculating the relative path.
(Edited) As suggested, we match input path with cwd in normalized-casing,
while returning the relative part in original-casing.
Original path is returned untouched if the match fails.
And this change actually fix the (harmless) bug in Windows that relative path is not shown correctly,
as a side effect.
Before:
After:
All the testcase in unit test still passed. (And it should, because it's just a cosmetic change.)
I tried to locate the first commit that use
os.path.normcase
, but it turns out that it's already therewhen this project migrated from svn to git in 2018.
Maybe there are some good reason to normalize case for the path in the past, but it should be ok to remove it now. :D