Skip to content

Commit

Permalink
Enhance --verbose (#2526)
Browse files Browse the repository at this point in the history
Black would now echo the location that it determined as the root path
for the project if `--verbose` is enabled by the user, according to
which it chooses the SRC paths, i.e. the absolute path of the project
is `{root}/{src}`.

Closes #1880
  • Loading branch information
Shivansh-007 authored Jan 10, 2022
1 parent e401b6b commit 521d1b8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
`values: Tuple[int, ...] = 1, 2, 3`) now implies 3.8+ (#2708)
- For stubs, one blank line between class attributes and methods is now kept if there's
at least one pre-existing blank line (#2736)
- Verbose mode also now describes how a project root was discovered and which paths will
be formatted. (#2526)

### Packaging

Expand Down
42 changes: 35 additions & 7 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
)

import click
from click.core import ParameterSource
from dataclasses import replace
from mypy_extensions import mypyc_attr

Expand Down Expand Up @@ -411,8 +412,37 @@ def main(
config: Optional[str],
) -> None:
"""The uncompromising code formatter."""
if config and verbose:
out(f"Using configuration from {config}.", bold=False, fg="blue")
ctx.ensure_object(dict)
root, method = find_project_root(src) if code is None else (None, None)
ctx.obj["root"] = root

if verbose:
if root:
out(
f"Identified `{root}` as project root containing a {method}.",
fg="blue",
)

normalized = [
(normalize_path_maybe_ignore(Path(source), root), source)
for source in src
]
srcs_string = ", ".join(
[
f'"{_norm}"'
if _norm
else f'\033[31m"{source} (skipping - invalid)"\033[34m'
for _norm, source in normalized
]
)
out(f"Sources to be formatted: {srcs_string}", fg="blue")

if config:
config_source = ctx.get_parameter_source("config")
if config_source in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP):
out("Using configuration from project root.", fg="blue")
else:
out(f"Using configuration in '{config}'.", fg="blue")

error_msg = "Oh no! 💥 💔 💥"
if required_version and required_version != __version__:
Expand Down Expand Up @@ -516,14 +546,12 @@ def get_sources(
stdin_filename: Optional[str],
) -> Set[Path]:
"""Compute the set of files to be formatted."""

root = find_project_root(src)
sources: Set[Path] = set()
path_empty(src, "No Path provided. Nothing to do 😴", quiet, verbose, ctx)

if exclude is None:
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES)
gitignore = get_gitignore(root)
gitignore = get_gitignore(ctx.obj["root"])
else:
gitignore = None

Expand All @@ -536,7 +564,7 @@ def get_sources(
is_stdin = False

if is_stdin or p.is_file():
normalized_path = normalize_path_maybe_ignore(p, root, report)
normalized_path = normalize_path_maybe_ignore(p, ctx.obj["root"], report)
if normalized_path is None:
continue

Expand All @@ -563,7 +591,7 @@ def get_sources(
sources.update(
gen_python_files(
p.iterdir(),
root,
ctx.obj["root"],
include,
exclude,
extend_exclude,
Expand Down
28 changes: 19 additions & 9 deletions src/black/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@


@lru_cache()
def find_project_root(srcs: Sequence[str]) -> Path:
def find_project_root(srcs: Sequence[str]) -> Tuple[Path, str]:
"""Return a directory containing .git, .hg, or pyproject.toml.
That directory will be a common parent of all files and directories
passed in `srcs`.
If no directory in the tree contains a marker that would specify it's the
project root, the root of the file system is returned.
Returns a two-tuple with the first element as the project root path and
the second element as a string describing the method by which the
project root was discovered.
"""
if not srcs:
srcs = [str(Path.cwd().resolve())]
Expand All @@ -58,20 +62,20 @@ def find_project_root(srcs: Sequence[str]) -> Path:

for directory in (common_base, *common_base.parents):
if (directory / ".git").exists():
return directory
return directory, ".git directory"

if (directory / ".hg").is_dir():
return directory
return directory, ".hg directory"

if (directory / "pyproject.toml").is_file():
return directory
return directory, "pyproject.toml"

return directory
return directory, "file system root"


def find_pyproject_toml(path_search_start: Tuple[str, ...]) -> 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)
path_pyproject_toml = path_project_root / "pyproject.toml"
if path_pyproject_toml.is_file():
return str(path_pyproject_toml)
Expand Down Expand Up @@ -133,7 +137,9 @@ def get_gitignore(root: Path) -> PathSpec:


def normalize_path_maybe_ignore(
path: Path, root: Path, report: Report
path: Path,
root: Path,
report: Optional[Report] = None,
) -> Optional[str]:
"""Normalize `path`. May return `None` if `path` was ignored.
Expand All @@ -143,12 +149,16 @@ def normalize_path_maybe_ignore(
abspath = path if path.is_absolute() else Path.cwd() / path
normalized_path = abspath.resolve().relative_to(root).as_posix()
except OSError as e:
report.path_ignored(path, f"cannot be read because {e}")
if report:
report.path_ignored(path, f"cannot be read because {e}")
return None

except ValueError:
if path.is_symlink():
report.path_ignored(path, f"is a symbolic link that points outside {root}")
if report:
report.path_ignored(
path, f"is a symbolic link that points outside {root}"
)
return None

raise
Expand Down
34 changes: 23 additions & 11 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class FakeContext(click.Context):

def __init__(self) -> None:
self.default_map: Dict[str, Any] = {}
# Dummy root, since most of the tests don't care about it
self.obj: Dict[str, Any] = {"root": PROJECT_ROOT}


class FakeParameter(click.Parameter):
Expand Down Expand Up @@ -1350,10 +1352,17 @@ def test_find_project_root(self) -> None:
src_python.touch()

self.assertEqual(
black.find_project_root((src_dir, test_dir)), root.resolve()
black.find_project_root((src_dir, test_dir)),
(root.resolve(), "pyproject.toml"),
)
self.assertEqual(
black.find_project_root((src_dir,)),
(src_dir.resolve(), "pyproject.toml"),
)
self.assertEqual(
black.find_project_root((src_python,)),
(src_dir.resolve(), "pyproject.toml"),
)
self.assertEqual(black.find_project_root((src_dir,)), src_dir.resolve())
self.assertEqual(black.find_project_root((src_python,)), src_dir.resolve())

@patch(
"black.files.find_user_pyproject_toml",
Expand Down Expand Up @@ -1756,6 +1765,7 @@ def assert_collected_sources(
src: Sequence[Union[str, Path]],
expected: Sequence[Union[str, Path]],
*,
ctx: Optional[FakeContext] = None,
exclude: Optional[str] = None,
include: Optional[str] = None,
extend_exclude: Optional[str] = None,
Expand All @@ -1771,7 +1781,7 @@ def assert_collected_sources(
)
gs_force_exclude = None if force_exclude is None else compile_pattern(force_exclude)
collected = black.get_sources(
ctx=FakeContext(),
ctx=ctx or FakeContext(),
src=gs_src,
quiet=False,
verbose=False,
Expand Down Expand Up @@ -1807,9 +1817,11 @@ def test_gitignore_used_as_default(self) -> None:
base / "b/.definitely_exclude/a.pyi",
]
src = [base / "b/"]
assert_collected_sources(src, expected, extend_exclude=r"/exclude/")
ctx = FakeContext()
ctx.obj["root"] = base
assert_collected_sources(src, expected, ctx=ctx, extend_exclude=r"/exclude/")

@patch("black.find_project_root", lambda *args: THIS_DIR.resolve())
@patch("black.find_project_root", lambda *args: (THIS_DIR.resolve(), None))
def test_exclude_for_issue_1572(self) -> None:
# Exclude shouldn't touch files that were explicitly given to Black through the
# CLI. Exclude is supposed to only apply to the recursive discovery of files.
Expand Down Expand Up @@ -1992,13 +2004,13 @@ def test_symlink_out_of_root_directory(self) -> None:
child.is_symlink.assert_called()
assert child.is_symlink.call_count == 2

@patch("black.find_project_root", lambda *args: THIS_DIR.resolve())
@patch("black.find_project_root", lambda *args: (THIS_DIR.resolve(), None))
def test_get_sources_with_stdin(self) -> None:
src = ["-"]
expected = ["-"]
assert_collected_sources(src, expected, include="", exclude=r"/exclude/|a\.py")

@patch("black.find_project_root", lambda *args: THIS_DIR.resolve())
@patch("black.find_project_root", lambda *args: (THIS_DIR.resolve(), None))
def test_get_sources_with_stdin_filename(self) -> None:
src = ["-"]
stdin_filename = str(THIS_DIR / "data/collections.py")
Expand All @@ -2010,7 +2022,7 @@ def test_get_sources_with_stdin_filename(self) -> None:
stdin_filename=stdin_filename,
)

@patch("black.find_project_root", lambda *args: THIS_DIR.resolve())
@patch("black.find_project_root", lambda *args: (THIS_DIR.resolve(), None))
def test_get_sources_with_stdin_filename_and_exclude(self) -> None:
# Exclude shouldn't exclude stdin_filename since it is mimicking the
# file being passed directly. This is the same as
Expand All @@ -2026,7 +2038,7 @@ def test_get_sources_with_stdin_filename_and_exclude(self) -> None:
stdin_filename=stdin_filename,
)

@patch("black.find_project_root", lambda *args: THIS_DIR.resolve())
@patch("black.find_project_root", lambda *args: (THIS_DIR.resolve(), None))
def test_get_sources_with_stdin_filename_and_extend_exclude(self) -> None:
# Extend exclude shouldn't exclude stdin_filename since it is mimicking the
# file being passed directly. This is the same as
Expand All @@ -2042,7 +2054,7 @@ def test_get_sources_with_stdin_filename_and_extend_exclude(self) -> None:
stdin_filename=stdin_filename,
)

@patch("black.find_project_root", lambda *args: THIS_DIR.resolve())
@patch("black.find_project_root", lambda *args: (THIS_DIR.resolve(), None))
def test_get_sources_with_stdin_filename_and_force_exclude(self) -> None:
# Force exclude should exclude the file when passing it through
# stdin_filename
Expand Down

0 comments on commit 521d1b8

Please sign in to comment.