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 not honouring pyproject.toml when using stdin and calling black from parent directory #3719

Merged
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 @@ -23,6 +23,8 @@
<!-- Changes to how Black can be configured -->

- `.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

Expand Down
5 changes: 4 additions & 1 deletion src/black/__init__.py
Expand Up @@ -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

Expand Down Expand Up @@ -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 "
Expand Down
6 changes: 4 additions & 2 deletions src/black/files.py
Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions tests/test_black.py
Expand Up @@ -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}

Expand Down Expand Up @@ -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:
Expand Down