From: Renan Santos Date: Fri, 23 Jun 2023 04:21:49 +0000 (-0300) Subject: Fix not honouring pyproject.toml when using stdin and calling black from parent direc... X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/453828d17d50e4bf8bdef972fa6815258e380034?ds=sidebyside;hp=d1248ca9beaf0ba526d265f4108836d89cf551b7 Fix not honouring pyproject.toml when using stdin and calling black from parent directory (#3719) Co-authored-by: Renan Rodrigues --- diff --git a/CHANGES.md b/CHANGES.md index fd4d911..e1028c1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -26,6 +26,8 @@ - `.pytest_cache`, `.ruff_cache` and `.vscode` are now excluded by default (#3691) +- Fix black not honouring `pyproject.toml` settings when running `--stdin-filename` and + the `pyproject.toml` found isn't in the current working directory (#3719) ### Packaging diff --git a/src/black/__init__.py b/src/black/__init__.py index dbcb559..60a339c 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -127,7 +127,9 @@ def read_pyproject_toml( otherwise. """ if not value: - value = find_pyproject_toml(ctx.params.get("src", ())) + value = find_pyproject_toml( + ctx.params.get("src", ()), ctx.params.get("stdin_filename", None) + ) if value is None: return None @@ -362,6 +364,7 @@ def validate_regex( @click.option( "--stdin-filename", type=str, + is_eager=True, help=( "The name of the file when passing it through stdin. Useful to make " "sure Black will respect --force-exclude option on some " diff --git a/src/black/files.py b/src/black/files.py index 8c01311..65b2d0a 100644 --- a/src/black/files.py +++ b/src/black/files.py @@ -89,9 +89,11 @@ def find_project_root( return directory, "file system root" -def find_pyproject_toml(path_search_start: Tuple[str, ...]) -> Optional[str]: +def find_pyproject_toml( + path_search_start: Tuple[str, ...], stdin_filename: Optional[str] = None +) -> Optional[str]: """Find the absolute filepath to a pyproject.toml if it exists""" - path_project_root, _ = find_project_root(path_search_start) + path_project_root, _ = find_project_root(path_search_start, stdin_filename) path_pyproject_toml = path_project_root / "pyproject.toml" if path_pyproject_toml.is_file(): return str(path_pyproject_toml) diff --git a/tests/test_black.py b/tests/test_black.py index 5f2e6f5..abb304a 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -104,6 +104,7 @@ class FakeContext(click.Context): def __init__(self) -> None: self.default_map: Dict[str, Any] = {} + self.params: Dict[str, Any] = {} # Dummy root, since most of the tests don't care about it self.obj: Dict[str, Any] = {"root": PROJECT_ROOT} @@ -1620,6 +1621,39 @@ class BlackTestCase(BlackBaseTestCase): self.assertEqual(config["exclude"], r"\.pyi?$") self.assertEqual(config["include"], r"\.py?$") + def test_read_pyproject_toml_from_stdin(self) -> None: + with TemporaryDirectory() as workspace: + root = Path(workspace) + + src_dir = root / "src" + src_dir.mkdir() + + src_pyproject = src_dir / "pyproject.toml" + src_pyproject.touch() + + test_toml_file = THIS_DIR / "test.toml" + src_pyproject.write_text(test_toml_file.read_text()) + + src_python = src_dir / "foo.py" + src_python.touch() + + fake_ctx = FakeContext() + fake_ctx.params["src"] = ("-",) + fake_ctx.params["stdin_filename"] = str(src_python) + + with change_directory(root): + black.read_pyproject_toml(fake_ctx, FakeParameter(), None) + + config = fake_ctx.default_map + self.assertEqual(config["verbose"], "1") + self.assertEqual(config["check"], "no") + self.assertEqual(config["diff"], "y") + self.assertEqual(config["color"], "True") + self.assertEqual(config["line_length"], "79") + self.assertEqual(config["target_version"], ["py36", "py37", "py38"]) + self.assertEqual(config["exclude"], r"\.pyi?$") + self.assertEqual(config["include"], r"\.py?$") + @pytest.mark.incompatible_with_mypyc def test_find_project_root(self) -> None: with TemporaryDirectory() as workspace: