Skip to content

Commit 6453abf

Browse files
committed
add a test
1 parent 679207e commit 6453abf

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

src/black/__init__.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -469,12 +469,8 @@ def main( # noqa: C901
469469
out(main.get_usage(ctx) + "\n\nOne of 'SRC' or 'code' is required.")
470470
ctx.exit(1)
471471

472-
if stdin_filename is not None:
473-
src_with_stdin_filename = tuple(stdin_filename if s == "-" else s for s in src)
474-
else:
475-
src_with_stdin_filename = src
476472
root, method = (
477-
find_project_root(src_with_stdin_filename) if code is None else (None, None)
473+
find_project_root(src, stdin_filename) if code is None else (None, None)
478474
)
479475
ctx.obj["root"] = root
480476

src/black/files.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939

4040

4141
@lru_cache()
42-
def find_project_root(srcs: Sequence[str]) -> Tuple[Path, str]:
42+
def find_project_root(
43+
srcs: Sequence[str], stdin_filename: Optional[str] = None
44+
) -> Tuple[Path, str]:
4345
"""Return a directory containing .git, .hg, or pyproject.toml.
4446
4547
That directory will be a common parent of all files and directories
@@ -52,6 +54,8 @@ def find_project_root(srcs: Sequence[str]) -> Tuple[Path, str]:
5254
the second element as a string describing the method by which the
5355
project root was discovered.
5456
"""
57+
if stdin_filename is not None:
58+
srcs = tuple(stdin_filename if s == "-" else s for s in srcs)
5559
if not srcs:
5660
srcs = [str(Path.cwd().resolve())]
5761

tests/test_black.py

+6
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,12 @@ def test_find_project_root(self) -> None:
13961396
(src_dir.resolve(), "pyproject.toml"),
13971397
)
13981398

1399+
with change_directory(test_dir):
1400+
self.assertEqual(
1401+
black.find_project_root(("-",), stdin_filename="../whatever.py"),
1402+
(root.resolve(), "pyproject.toml"),
1403+
)
1404+
13991405
@patch(
14001406
"black.files.find_user_pyproject_toml",
14011407
)

0 commit comments

Comments
 (0)