Skip to content

Commit

Permalink
Minor refactoring in get_sources and gen_python_files (#4013)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja authored Nov 1, 2023
1 parent 5758da6 commit e2f2bd0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
39 changes: 18 additions & 21 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
get_gitignore,
normalize_path_maybe_ignore,
parse_pyproject_toml,
path_is_excluded,
wrap_stream_for_windows,
)
from black.handle_ipynb_magics import (
Expand Down Expand Up @@ -632,15 +633,15 @@ def get_sources(

for s in src:
if s == "-" and stdin_filename:
p = Path(stdin_filename)
path = Path(stdin_filename)
is_stdin = True
else:
p = Path(s)
path = Path(s)
is_stdin = False

if is_stdin or p.is_file():
if is_stdin or path.is_file():
normalized_path: Optional[str] = normalize_path_maybe_ignore(
p, root, report
path, root, report
)
if normalized_path is None:
if verbose:
Expand All @@ -651,38 +652,34 @@ def get_sources(

normalized_path = "/" + normalized_path
# Hard-exclude any files that matches the `--force-exclude` regex.
if force_exclude:
force_exclude_match = force_exclude.search(normalized_path)
else:
force_exclude_match = None
if force_exclude_match and force_exclude_match.group(0):
report.path_ignored(p, "matches the --force-exclude regular expression")
if path_is_excluded(normalized_path, force_exclude):
report.path_ignored(
path, "matches the --force-exclude regular expression"
)
continue

if is_stdin:
p = Path(f"{STDIN_PLACEHOLDER}{str(p)}")
path = Path(f"{STDIN_PLACEHOLDER}{str(path)}")

if p.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
if path.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
warn=verbose or not quiet
):
continue

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

if using_default_exclude:
gitignore = {
root: root_gitignore,
p: get_gitignore(p),
path: get_gitignore(path),
}
sources.update(
gen_python_files(
p.iterdir(),
path.iterdir(),
root,
include,
exclude,
Expand All @@ -697,7 +694,7 @@ def get_sources(
elif s == "-":
if verbose:
out("Found input source stdin", fg="blue")
sources.add(p)
sources.add(path)
else:
err(f"invalid path: {s}")

Expand Down
7 changes: 2 additions & 5 deletions src/black/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ def _path_is_ignored(
root_relative_path: str,
root: Path,
gitignore_dict: Dict[Path, PathSpec],
report: Report,
) -> bool:
path = root / root_relative_path
# Note that this logic is sensitive to the ordering of gitignore_dict. Callers must
Expand All @@ -291,9 +290,6 @@ def _path_is_ignored(
except ValueError:
break
if pattern.match_file(relative_path):
report.path_ignored(
path.relative_to(root), "matches a .gitignore file content"
)
return True
return False

Expand Down Expand Up @@ -334,8 +330,9 @@ def gen_python_files(

# First ignore files matching .gitignore, if passed
if gitignore_dict and _path_is_ignored(
root_relative_path, root, gitignore_dict, report
root_relative_path, root, gitignore_dict
):
report.path_ignored(child, "matches a .gitignore file content")
continue

# Then ignore with `--exclude` `--extend-exclude` and `--force-exclude` options.
Expand Down
5 changes: 3 additions & 2 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def _mocked_calls() -> bool:
return _mocked_calls

with patch("pathlib.Path.iterdir", return_value=target_contents), patch(
"pathlib.Path.cwd", return_value=working_directory
"pathlib.Path.resolve", return_value=target_abspath
), patch("pathlib.Path.is_dir", side_effect=mock_n_calls([True])):
# Note that the root folder (project_root) isn't the folder
# named "root" (aka working_directory)
Expand All @@ -526,7 +526,8 @@ def _mocked_calls() -> bool:
for _, mock_args, _ in report.path_ignored.mock_calls
), "A symbolic link was reported."
report.path_ignored.assert_called_once_with(
Path("root", "child", "b.py"), "matches a .gitignore file content"
Path(working_directory, "child", "b.py"),
"matches a .gitignore file content",
)

def test_report_verbose(self) -> None:
Expand Down

0 comments on commit e2f2bd0

Please sign in to comment.