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 #1631: Error when PWD=/ #1641

Merged
merged 1 commit into from Aug 27, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -7,6 +7,8 @@
- `Black` now respects `--skip-string-normalization` when normalizing multiline
docstring quotes (#1637)

- fixed a crash when PWD=/ on POSIX (#1631)

### 20.8b1

#### _Packaging_
Expand Down
3 changes: 2 additions & 1 deletion src/black/__init__.py
Expand Up @@ -5831,7 +5831,8 @@ def normalize_path_maybe_ignore(
`report` is where "path ignored" output goes.
"""
try:
normalized_path = path.resolve().relative_to(root).as_posix()
abspath = path if path.is_absolute() else Path.cwd() / path
normalized_path = abspath.resolve().relative_to(root).as_posix()
except OSError as e:
report.path_ignored(path, f"cannot be read because {e}")
return None
Expand Down
18 changes: 18 additions & 0 deletions tests/test_black.py
Expand Up @@ -9,6 +9,7 @@
from io import BytesIO, TextIOWrapper
import os
from pathlib import Path
from platform import system
import regex as re
import sys
from tempfile import TemporaryDirectory
Expand Down Expand Up @@ -1939,6 +1940,23 @@ def test_find_project_root(self) -> None:
self.assertEqual(black.find_project_root((src_dir,)), src_dir.resolve())
self.assertEqual(black.find_project_root((src_python,)), src_dir.resolve())

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

# https://bugs.python.org/issue33660

old_cwd = Path.cwd()
try:
root = Path("/")
os.chdir(str(root))
path = Path("workspace") / "project"
report = black.Report(verbose=True)
normalized_path = black.normalize_path_maybe_ignore(path, root, report)
self.assertEqual(normalized_path, "workspace/project")
finally:
os.chdir(str(old_cwd))


class BlackDTestCase(AioHTTPTestCase):
async def get_application(self) -> web.Application:
Expand Down