Fix not honouring pyproject.toml when using stdin and calling black from parent directory (#3719)

Co-authored-by: Renan Rodrigues <renan.rodrigues@appliedbiomath.com>
This commit is contained in:
Renan Santos 2023-06-23 01:21:49 -03:00 committed by GitHub
parent d1248ca9be
commit 453828d17d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 3 deletions

View File

@ -26,6 +26,8 @@
<!-- Changes to how Black can be configured --> <!-- Changes to how Black can be configured -->
- `.pytest_cache`, `.ruff_cache` and `.vscode` are now excluded by default (#3691) - `.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 ### Packaging

View File

@ -127,7 +127,9 @@ def read_pyproject_toml(
otherwise. otherwise.
""" """
if not value: 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: if value is None:
return None return None
@ -362,6 +364,7 @@ def validate_regex(
@click.option( @click.option(
"--stdin-filename", "--stdin-filename",
type=str, type=str,
is_eager=True,
help=( help=(
"The name of the file when passing it through stdin. Useful to make " "The name of the file when passing it through stdin. Useful to make "
"sure Black will respect --force-exclude option on some " "sure Black will respect --force-exclude option on some "

View File

@ -89,9 +89,11 @@ def find_project_root(
return directory, "file system 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""" """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" path_pyproject_toml = path_project_root / "pyproject.toml"
if path_pyproject_toml.is_file(): if path_pyproject_toml.is_file():
return str(path_pyproject_toml) return str(path_pyproject_toml)

View File

@ -104,6 +104,7 @@ class FakeContext(click.Context):
def __init__(self) -> None: def __init__(self) -> None:
self.default_map: Dict[str, Any] = {} self.default_map: Dict[str, Any] = {}
self.params: Dict[str, Any] = {}
# Dummy root, since most of the tests don't care about it # Dummy root, since most of the tests don't care about it
self.obj: Dict[str, Any] = {"root": PROJECT_ROOT} 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["exclude"], r"\.pyi?$")
self.assertEqual(config["include"], r"\.py?$") 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 @pytest.mark.incompatible_with_mypyc
def test_find_project_root(self) -> None: def test_find_project_root(self) -> None:
with TemporaryDirectory() as workspace: with TemporaryDirectory() as workspace: