From 2b75f8870eac943c93e93c740c9d2ef74efeeb41 Mon Sep 17 00:00:00 2001 From: mbarkhau Date: Thu, 27 Aug 2020 11:47:59 +0000 Subject: [PATCH] fix 1631 and add test (#1641) --- CHANGES.md | 2 ++ src/black/__init__.py | 3 ++- tests/test_black.py | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 7352b85..1c53604 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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_ diff --git a/src/black/__init__.py b/src/black/__init__.py index 34d8145..048e771 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -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 diff --git a/tests/test_black.py b/tests/test_black.py index 629afc5..bc80c8f 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -9,6 +9,7 @@ import inspect 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 @@ -1939,6 +1940,23 @@ class BlackTestCase(unittest.TestCase): 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: -- 2.39.5