Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_empty_include(self) -> None:
path = THIS_DIR / "data" / "include_exclude_tests"
report = black.Report()
gitignore = PathSpec.from_lines("gitwildmatch", [])
empty = re.compile(r"")
sources: List[Path] = []
expected = [
Path(path / "b/exclude/a.pie"),
Path(path / "b/exclude/a.py"),
Path(path / "b/exclude/a.pyi"),
Path(path / "b/dont_exclude/a.pie"),
Path(path / "b/dont_exclude/a.py"),
Path(path / "b/dont_exclude/a.pyi"),
Path(path / "b/.definitely_exclude/a.pie"),
Path(path / "b/.definitely_exclude/a.py"),
Path(path / "b/.definitely_exclude/a.pyi"),
]
this_abs = THIS_DIR.resolve()
sources.extend(
def test_empty_exclude(self) -> None:
path = THIS_DIR / "data" / "include_exclude_tests"
report = black.Report()
gitignore = PathSpec.from_lines("gitwildmatch", [])
empty = re.compile(r"")
sources: List[Path] = []
expected = [
Path(path / "b/dont_exclude/a.py"),
Path(path / "b/dont_exclude/a.pyi"),
Path(path / "b/exclude/a.py"),
Path(path / "b/exclude/a.pyi"),
Path(path / "b/.definitely_exclude/a.py"),
Path(path / "b/.definitely_exclude/a.pyi"),
]
this_abs = THIS_DIR.resolve()
sources.extend(
black.gen_python_files_in_dir(
path,
this_abs,
def test_report_normal(self) -> None:
report = black.Report()
out_lines = []
err_lines = []
def out(msg: str, **kwargs: Any) -> None:
out_lines.append(msg)
def err(msg: str, **kwargs: Any) -> None:
err_lines.append(msg)
with patch("black.out", out), patch("black.err", err):
report.done(Path("f1"), black.Changed.NO)
self.assertEqual(len(out_lines), 0)
self.assertEqual(len(err_lines), 0)
self.assertEqual(unstyle(str(report)), "1 file left unchanged.")
self.assertEqual(report.return_code, 0)
report.done(Path("f2"), black.Changed.YES)
if config and verbose:
black.out(f"Using configuration from {config}.", bold=False, fg="blue")
try:
include_regex = black.re_compile_maybe_verbose(include)
except re.error:
black.err(f"Invalid regular expression for include given: {include!r}")
ctx.exit(2)
try:
exclude_regex = black.re_compile_maybe_verbose(exclude)
except re.error:
black.err(f"Invalid regular expression for exclude given: {exclude!r}")
ctx.exit(2)
report = black.Report(check=check, quiet=quiet, verbose=verbose)
root = black.find_project_root(src)
sources: Set[Path] = set()
for s in src:
p = Path(s)
if p.is_dir():
sources.update(
black.gen_python_files_in_dir(
p,
root,
include_regex,
exclude_regex,
report,
black.get_gitignore(root),
)
)
elif p.is_file() or s == "-":
if config and verbose:
out(f"Using configuration from {config}.", bold=False, fg="blue")
if code is not None:
print(format_str(code, mode=mode))
ctx.exit(0)
try:
include_regex = re_compile_maybe_verbose(include)
except re.error:
err(f"Invalid regular expression for include given: {include!r}")
ctx.exit(2)
try:
exclude_regex = re_compile_maybe_verbose(exclude)
except re.error:
err(f"Invalid regular expression for exclude given: {exclude!r}")
ctx.exit(2)
report = Report(check=check, quiet=quiet, verbose=verbose)
root = find_project_root(src)
sources: Set[Path] = set()
for s in src:
p = Path(s)
if p.is_dir():
sources.update(
gen_python_files_in_dir(p, root, include_regex, exclude_regex, report)
)
elif p.is_file() or s == "-":
# if a file was explicitly given, we don't care about its extension
sources.add(p)
else:
err(f"invalid path: {s}")
if len(sources) == 0:
if verbose or not quiet:
out("No paths given. Nothing to do 😴")
def reformat_code(filename):
SortImports(filename)
src = Path(filename)
report = black.Report()
black.reformat_one(
src=src,
line_length=88,
fast=False,
write_back=black.WriteBack.YES,
mode=black.FileMode.AUTO_DETECT,
report=report,
)