raise
+class FakeContext(click.Context):
+ """A fake click Context for when calling functions that need it."""
+
+ def __init__(self) -> None:
+ self.default_map: Dict[str, Any] = {}
+
+
+class FakeParameter(click.Parameter):
+ """A fake click Parameter for when calling functions that need it."""
+
+ def __init__(self) -> None:
+ pass
+
+
class BlackRunner(CliRunner):
"""Modify CliRunner so that stderr is not merged with stdout.
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, black.FileMode())
+ @patch("black.dump_to_file", dump_to_stderr)
+ def test_percent_precedence(self) -> None:
+ source, expected = read_data("percent_precedence")
+ actual = fs(source)
+ self.assertFormatEqual(expected, actual)
+ black.assert_equivalent(source, actual)
+ black.assert_stable(source, actual, black.FileMode())
+
@patch("black.dump_to_file", dump_to_stderr)
def test_comments(self) -> None:
source, expected = read_data("comments")
this_abs = THIS_DIR.resolve()
sources.extend(
black.gen_python_files(
- path.iterdir(), this_abs, include, [exclude], report, gitignore
+ path.iterdir(), this_abs, include, exclude, None, report, gitignore
+ )
+ )
+ self.assertEqual(sorted(expected), sorted(sources))
+
+ @patch("black.find_project_root", lambda *args: THIS_DIR.resolve())
+ 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.
+ # https://github.com/psf/black/issues/1572
+ path = THIS_DIR / "data" / "include_exclude_tests"
+ include = ""
+ exclude = r"/exclude/|a\.py"
+ src = str(path / "b/exclude/a.py")
+ report = black.Report()
+ expected = [Path(path / "b/exclude/a.py")]
+ sources = list(
+ black.get_sources(
+ ctx=FakeContext(),
+ src=(src,),
+ quiet=True,
+ verbose=False,
+ include=include,
+ exclude=exclude,
+ force_exclude=None,
+ report=report,
)
)
self.assertEqual(sorted(expected), sorted(sources))
this_abs = THIS_DIR.resolve()
sources.extend(
black.gen_python_files(
- path.iterdir(), this_abs, include, [exclude], report, gitignore
+ path.iterdir(), this_abs, include, exclude, None, report, gitignore
)
)
self.assertEqual(sorted(expected), sorted(sources))
path.iterdir(),
this_abs,
empty,
- [re.compile(black.DEFAULT_EXCLUDES)],
+ re.compile(black.DEFAULT_EXCLUDES),
+ None,
report,
gitignore,
)
path.iterdir(),
this_abs,
re.compile(black.DEFAULT_INCLUDES),
- [empty],
+ empty,
+ None,
report,
gitignore,
)
try:
list(
black.gen_python_files(
- path.iterdir(), root, include, exclude, report, gitignore
+ path.iterdir(), root, include, exclude, None, report, gitignore
)
)
except ValueError as ve:
with self.assertRaises(ValueError):
list(
black.gen_python_files(
- path.iterdir(), root, include, exclude, report, gitignore
+ path.iterdir(), root, include, exclude, None, report, gitignore
)
)
path.iterdir.assert_called()
def test_read_pyproject_toml(self) -> None:
test_toml_file = THIS_DIR / "test.toml"
-
- # Fake a click context and parameter so mypy stays happy
- class FakeContext(click.Context):
- def __init__(self) -> None:
- self.default_map: Dict[str, Any] = {}
-
- class FakeParameter(click.Parameter):
- def __init__(self) -> None:
- pass
-
fake_ctx = FakeContext()
black.read_pyproject_toml(
fake_ctx, FakeParameter(), str(test_toml_file),