Skip to content
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

fix unc path bug as stdin-filename in Windows python 3.8 #4210

Closed
wants to merge 5 commits into from
Closed
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 CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
for users in a monorepo setup (desirably). If you wish to preserve previous behavior,
simply add an empty `[tool.black]` to the previously discovered `pyproject.toml`
(#4204)
- Fix UNC path as stdin-filename in Windows Python 3.8 (#4210)

### Packaging

Expand Down
9 changes: 7 additions & 2 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,12 @@ def get_sources(

# Compare the logic here to the logic in `gen_python_files`.
if is_stdin or path.is_file():
root_relative_path = get_root_relative_path(path, root, report)
# From https://stackoverflow.com/questions/42513056/how-to-get-absolute-path-of-a-pathlib-path-object # noqa: B950
# suggest to use resolve instead of absolute prior to Python 3.11
# to get the absolute path.
# This change is to fix bug:
# https://github.com/psf/black/issues/4209
root_relative_path = get_root_relative_path(path.resolve(), root, report)
guotuofeng marked this conversation as resolved.
Show resolved Hide resolved

if root_relative_path is None:
continue
Expand All @@ -772,7 +777,7 @@ def get_sources(
continue

if is_stdin:
path = Path(f"{STDIN_PLACEHOLDER}{str(path)}")
path = Path(f"{STDIN_PLACEHOLDER}{str(path.resolve())}")

if path.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
warn=verbose or not quiet
Expand Down
26 changes: 26 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from black import Feature, TargetVersion
from black import re_compile_maybe_verbose as compile_pattern
from black.cache import FileData, get_cache_dir, get_cache_file
from black.const import STDIN_PLACEHOLDER
from black.debug import DebugVisitor
from black.mode import Mode, Preview
from black.output import color_diff, diff
Expand Down Expand Up @@ -2623,6 +2624,31 @@ def test_symlinks(self) -> None:
outside_root_symlink.resolve.assert_called_once()
ignored_symlink.resolve.assert_not_called()

def test_unc_path(self) -> None:
if system() != "Windows":
return

stdin_filename = "\\\\?\\D:\\git\\OLive\\setup.py"
root = Path("D:\\git\\OLive")
collected = black.get_sources(
root=root,
src=("-",),
quiet=False,
verbose=False,
include=DEFAULT_INCLUDE,
exclude=DEFAULT_EXCLUDE,
extend_exclude=None,
force_exclude=None,
report=black.Report(),
stdin_filename=stdin_filename,
)
assert len(collected) == 1
expected_path = Path(
f"{STDIN_PLACEHOLDER}{str(Path(stdin_filename).resolve())}"
)
(collected_item,) = collected
assert collected_item == expected_path

def test_get_sources_with_stdin_symlink_outside_root(
self,
) -> None:
Expand Down
Loading