Skip to content

Commit

Permalink
Improve handling of root to get_sources
Browse files Browse the repository at this point in the history
This is a little more type safe and a little cleaner
  • Loading branch information
hauntsaninja committed Aug 19, 2023
1 parent c6a031e commit 8a3027c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
14 changes: 8 additions & 6 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,10 @@ def main( # noqa: C901
content=code, fast=fast, write_back=write_back, mode=mode, report=report
)
else:
assert root is not None # root is only None if code is not None
try:
sources = get_sources(
ctx=ctx,
root=root,
src=src,
quiet=quiet,
verbose=verbose,
Expand Down Expand Up @@ -615,7 +616,7 @@ def main( # noqa: C901

def get_sources(
*,
ctx: click.Context,
root: Path,
src: Tuple[str, ...],
quiet: bool,
verbose: bool,
Expand All @@ -628,7 +629,6 @@ def get_sources(
) -> Set[Path]:
"""Compute the set of files to be formatted."""
sources: Set[Path] = set()
root = ctx.obj["root"]

using_default_exclude = exclude is None
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES) if exclude is None else exclude
Expand All @@ -645,7 +645,7 @@ def get_sources(

if is_stdin or p.is_file():
normalized_path: Optional[str] = normalize_path_maybe_ignore(
p, ctx.obj["root"], report
p, root, report
)
if normalized_path is None:
if verbose:
Expand Down Expand Up @@ -674,7 +674,9 @@ def get_sources(

sources.add(p)
elif p.is_dir():
p = root / normalize_path_maybe_ignore(p, ctx.obj["root"], report)
p_relative = normalize_path_maybe_ignore(p, root, report)
assert p_relative is not None
p = root / p_relative
if verbose:
out(f'Found input source directory: "{p}"', fg="blue")

Expand All @@ -686,7 +688,7 @@ def get_sources(
sources.update(
gen_python_files(
p.iterdir(),
ctx.obj["root"],
root,
include,
exclude,
extend_exclude,
Expand Down
28 changes: 8 additions & 20 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,11 @@ def _mocked_calls() -> bool:
with patch("pathlib.Path.iterdir", return_value=target_contents), patch(
"pathlib.Path.cwd", return_value=working_directory
), patch("pathlib.Path.is_dir", side_effect=mock_n_calls([True])):
ctx = FakeContext()
# Note that the root folder (project_root) isn't the folder
# named "root" (aka working_directory)
ctx.obj["root"] = project_root
report = MagicMock(verbose=True)
black.get_sources(
ctx=ctx,
root=project_root,
src=("./child",),
quiet=False,
verbose=True,
Expand Down Expand Up @@ -2163,7 +2161,7 @@ def assert_collected_sources(
src: Sequence[Union[str, Path]],
expected: Sequence[Union[str, Path]],
*,
ctx: Optional[FakeContext] = None,
root: Optional[Path] = None,
exclude: Optional[str] = None,
include: Optional[str] = None,
extend_exclude: Optional[str] = None,
Expand All @@ -2179,7 +2177,7 @@ def assert_collected_sources(
)
gs_force_exclude = None if force_exclude is None else compile_pattern(force_exclude)
collected = black.get_sources(
ctx=ctx or FakeContext(),
root=root or THIS_DIR,
src=gs_src,
quiet=False,
verbose=False,
Expand Down Expand Up @@ -2215,20 +2213,16 @@ def test_gitignore_used_as_default(self) -> None:
base / "b/.definitely_exclude/a.pyi",
]
src = [base / "b/"]
ctx = FakeContext()
ctx.obj["root"] = base
assert_collected_sources(src, expected, ctx=ctx, extend_exclude=r"/exclude/")
assert_collected_sources(src, expected, root=base, extend_exclude=r"/exclude/")

def test_gitignore_used_on_multiple_sources(self) -> None:
root = Path(DATA_DIR / "gitignore_used_on_multiple_sources")
expected = [
root / "dir1" / "b.py",
root / "dir2" / "b.py",
]
ctx = FakeContext()
ctx.obj["root"] = root
src = [root / "dir1", root / "dir2"]
assert_collected_sources(src, expected, ctx=ctx)
assert_collected_sources(src, expected, root=root)

@patch("black.find_project_root", lambda *args: (THIS_DIR.resolve(), None))
def test_exclude_for_issue_1572(self) -> None:
Expand Down Expand Up @@ -2334,27 +2328,21 @@ def test_gitignore_that_ignores_subfolders(self) -> None:
# If gitignore with */* is in root
root = Path(DATA_DIR / "ignore_subfolders_gitignore_tests" / "subdir")
expected = [root / "b.py"]
ctx = FakeContext()
ctx.obj["root"] = root
assert_collected_sources([root], expected, ctx=ctx)
assert_collected_sources([root], expected, root=root)

# If .gitignore with */* is nested
root = Path(DATA_DIR / "ignore_subfolders_gitignore_tests")
expected = [
root / "a.py",
root / "subdir" / "b.py",
]
ctx = FakeContext()
ctx.obj["root"] = root
assert_collected_sources([root], expected, ctx=ctx)
assert_collected_sources([root], expected, root=root)

# If command is executed from outer dir
root = Path(DATA_DIR / "ignore_subfolders_gitignore_tests")
target = root / "subdir"
expected = [target / "b.py"]
ctx = FakeContext()
ctx.obj["root"] = root
assert_collected_sources([target], expected, ctx=ctx)
assert_collected_sources([target], expected, root=root)

def test_empty_include(self) -> None:
path = DATA_DIR / "include_exclude_tests"
Expand Down

0 comments on commit 8a3027c

Please sign in to comment.