From dfe7f6c3f7524018229b2782416138d6ee2028cf Mon Sep 17 00:00:00 2001 From: Renan Rodrigues Date: Mon, 5 Jun 2023 10:24:11 -0300 Subject: [PATCH 1/4] add test that finds and reads pyproject.toml based on stdin --- tests/test_black.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/test_black.py b/tests/test_black.py index 42b0161d156..453882262bd 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 @@ def test_read_pyproject_toml(self) -> None: 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: From c4615061699a6a1d691bff254e8db9e9abee9619 Mon Sep 17 00:00:00 2001 From: Renan Rodrigues Date: Mon, 5 Jun 2023 10:25:47 -0300 Subject: [PATCH 2/4] Fix behavior when running black with stdin and pyproject.toml isn't in cwd --- src/black/__init__.py | 5 ++++- src/black/files.py | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 8a759aa493a..a8a09deded7 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 8c0131126b7..65b2d0a8402 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) From b34c399fc9a6b4c4e631169fb001bbe3b045b15b Mon Sep 17 00:00:00 2001 From: Renan Rodrigues Date: Mon, 5 Jun 2023 10:38:12 -0300 Subject: [PATCH 3/4] Update CHANGES.md --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index fb3dea8c348..b659547dc66 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -23,6 +23,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 From fd6b4d673d548e6553c34ff7bff3a8c9d7beb4fd Mon Sep 17 00:00:00 2001 From: Renan Rodrigues Date: Mon, 5 Jun 2023 11:55:42 -0300 Subject: [PATCH 4/4] fix linting --- tests/test_black.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_black.py b/tests/test_black.py index 453882262bd..a04dbab88e2 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -1638,7 +1638,7 @@ def test_read_pyproject_toml_from_stdin(self) -> None: src_python.touch() fake_ctx = FakeContext() - fake_ctx.params["src"] = ("-", ) + fake_ctx.params["src"] = ("-",) fake_ctx.params["stdin_filename"] = str(src_python) with change_directory(root):