Minor refactoring in get_sources and gen_python_files (#4013)

This commit is contained in:
Shantanu 2023-11-01 06:20:14 -07:00 committed by GitHub
parent 5758da6e3c
commit e2f2bd076f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 28 deletions

View File

@ -50,6 +50,7 @@
get_gitignore, get_gitignore,
normalize_path_maybe_ignore, normalize_path_maybe_ignore,
parse_pyproject_toml, parse_pyproject_toml,
path_is_excluded,
wrap_stream_for_windows, wrap_stream_for_windows,
) )
from black.handle_ipynb_magics import ( from black.handle_ipynb_magics import (
@ -632,15 +633,15 @@ def get_sources(
for s in src: for s in src:
if s == "-" and stdin_filename: if s == "-" and stdin_filename:
p = Path(stdin_filename) path = Path(stdin_filename)
is_stdin = True is_stdin = True
else: else:
p = Path(s) path = Path(s)
is_stdin = False 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( normalized_path: Optional[str] = normalize_path_maybe_ignore(
p, root, report path, root, report
) )
if normalized_path is None: if normalized_path is None:
if verbose: if verbose:
@ -651,38 +652,34 @@ def get_sources(
normalized_path = "/" + normalized_path normalized_path = "/" + normalized_path
# Hard-exclude any files that matches the `--force-exclude` regex. # Hard-exclude any files that matches the `--force-exclude` regex.
if force_exclude: if path_is_excluded(normalized_path, force_exclude):
force_exclude_match = force_exclude.search(normalized_path) report.path_ignored(
else: path, "matches the --force-exclude regular expression"
force_exclude_match = None )
if force_exclude_match and force_exclude_match.group(0):
report.path_ignored(p, "matches the --force-exclude regular expression")
continue continue
if is_stdin: 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 warn=verbose or not quiet
): ):
continue continue
sources.add(p) sources.add(path)
elif p.is_dir(): elif path.is_dir():
p_relative = normalize_path_maybe_ignore(p, root, report) path = root / (path.resolve().relative_to(root))
assert p_relative is not None
p = root / p_relative
if verbose: if verbose:
out(f'Found input source directory: "{p}"', fg="blue") out(f'Found input source directory: "{path}"', fg="blue")
if using_default_exclude: if using_default_exclude:
gitignore = { gitignore = {
root: root_gitignore, root: root_gitignore,
p: get_gitignore(p), path: get_gitignore(path),
} }
sources.update( sources.update(
gen_python_files( gen_python_files(
p.iterdir(), path.iterdir(),
root, root,
include, include,
exclude, exclude,
@ -697,7 +694,7 @@ def get_sources(
elif s == "-": elif s == "-":
if verbose: if verbose:
out("Found input source stdin", fg="blue") out("Found input source stdin", fg="blue")
sources.add(p) sources.add(path)
else: else:
err(f"invalid path: {s}") err(f"invalid path: {s}")

View File

@ -280,7 +280,6 @@ def _path_is_ignored(
root_relative_path: str, root_relative_path: str,
root: Path, root: Path,
gitignore_dict: Dict[Path, PathSpec], gitignore_dict: Dict[Path, PathSpec],
report: Report,
) -> bool: ) -> bool:
path = root / root_relative_path path = root / root_relative_path
# Note that this logic is sensitive to the ordering of gitignore_dict. Callers must # Note that this logic is sensitive to the ordering of gitignore_dict. Callers must
@ -291,9 +290,6 @@ def _path_is_ignored(
except ValueError: except ValueError:
break break
if pattern.match_file(relative_path): if pattern.match_file(relative_path):
report.path_ignored(
path.relative_to(root), "matches a .gitignore file content"
)
return True return True
return False return False
@ -334,8 +330,9 @@ def gen_python_files(
# First ignore files matching .gitignore, if passed # First ignore files matching .gitignore, if passed
if gitignore_dict and _path_is_ignored( 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 continue
# Then ignore with `--exclude` `--extend-exclude` and `--force-exclude` options. # Then ignore with `--exclude` `--extend-exclude` and `--force-exclude` options.

View File

@ -504,7 +504,7 @@ def _mocked_calls() -> bool:
return _mocked_calls return _mocked_calls
with patch("pathlib.Path.iterdir", return_value=target_contents), patch( 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])): ), patch("pathlib.Path.is_dir", side_effect=mock_n_calls([True])):
# Note that the root folder (project_root) isn't the folder # Note that the root folder (project_root) isn't the folder
# named "root" (aka working_directory) # named "root" (aka working_directory)
@ -526,7 +526,8 @@ def _mocked_calls() -> bool:
for _, mock_args, _ in report.path_ignored.mock_calls for _, mock_args, _ in report.path_ignored.mock_calls
), "A symbolic link was reported." ), "A symbolic link was reported."
report.path_ignored.assert_called_once_with( 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: def test_report_verbose(self) -> None: