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:
parent
d1248ca9be
commit
453828d17d
@ -26,6 +26,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
|
||||
|
||||
|
@ -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 "
|
||||
|
@ -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)
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user