from contextlib import contextmanager
from dataclasses import replace
import inspect
-from io import BytesIO, TextIOWrapper
+from io import BytesIO
import os
from pathlib import Path
from platform import system
import types
from typing import (
Any,
- BinaryIO,
Callable,
Dict,
- Generator,
List,
Iterator,
TypeVar,
ff,
dump_to_stderr,
)
-from .test_primer import PrimerCLITests # noqa: F401
THIS_FILE = Path(__file__)
class BlackRunner(CliRunner):
- """Modify CliRunner so that stderr is not merged with stdout.
-
- This is a hack that can be removed once we depend on Click 7.x"""
+ """Make sure STDOUT and STDERR are kept separate when testing Black via its CLI."""
def __init__(self) -> None:
- self.stderrbuf = BytesIO()
- self.stdoutbuf = BytesIO()
- self.stdout_bytes = b""
- self.stderr_bytes = b""
- super().__init__()
-
- @contextmanager
- def isolation(self, *args: Any, **kwargs: Any) -> Generator[BinaryIO, None, None]:
- with super().isolation(*args, **kwargs) as output:
- try:
- hold_stderr = sys.stderr
- sys.stderr = TextIOWrapper(self.stderrbuf, encoding=self.charset)
- yield output
- finally:
- self.stdout_bytes = sys.stdout.buffer.getvalue() # type: ignore
- self.stderr_bytes = sys.stderr.buffer.getvalue() # type: ignore
- sys.stderr = hold_stderr
+ super().__init__(mix_stderr=False)
class BlackTestCase(BlackBaseTestCase):
exit_code,
msg=(
f"Failed with args: {args}\n"
- f"stdout: {runner.stdout_bytes.decode()!r}\n"
- f"stderr: {runner.stderr_bytes.decode()!r}\n"
+ f"stdout: {result.stdout_bytes.decode()!r}\n"
+ f"stderr: {result.stderr_bytes.decode()!r}\n"
f"exception: {result.exception}"
),
)
finally:
os.unlink(tmp_file)
actual = (
- runner.stderr_bytes.decode()
+ result.stderr_bytes.decode()
.replace("\n", "")
.replace("\\n", "")
.replace("\\r", "")
)
self.assertEqual(sorted(expected), sorted(sources))
- def test_gitingore_used_as_default(self) -> None:
+ def test_gitignore_used_as_default(self) -> None:
path = Path(THIS_DIR / "data" / "include_exclude_tests")
include = re.compile(r"\.pyi?$")
extend_exclude = re.compile(r"/exclude/")
@patch("black.find_project_root", lambda *args: THIS_DIR.resolve())
def test_get_sources_with_stdin_filename_and_exclude(self) -> None:
- # Exclude shouldn't exclude stdin_filename since it is mimicing the
+ # Exclude shouldn't exclude stdin_filename since it is mimicking the
# file being passed directly. This is the same as
# test_exclude_for_issue_1572
path = THIS_DIR / "data" / "include_exclude_tests"
)
self.assertEqual(sorted(expected), sorted(sources))
+ def test_nested_gitignore(self) -> None:
+ path = Path(THIS_DIR / "data" / "nested_gitignore_tests")
+ include = re.compile(r"\.pyi?$")
+ exclude = re.compile(r"")
+ root_gitignore = black.files.get_gitignore(path)
+ report = black.Report()
+ expected: List[Path] = [
+ Path(path / "x.py"),
+ Path(path / "root/b.py"),
+ Path(path / "root/c.py"),
+ Path(path / "root/child/c.py"),
+ ]
+ this_abs = THIS_DIR.resolve()
+ sources = list(
+ black.gen_python_files(
+ path.iterdir(),
+ this_abs,
+ include,
+ exclude,
+ None,
+ None,
+ report,
+ root_gitignore,
+ )
+ )
+ self.assertEqual(sorted(expected), sorted(sources))
+
def test_empty_include(self) -> None:
path = THIS_DIR / "data" / "include_exclude_tests"
report = black.Report()
black.main, ["-", "--fast"], input=BytesIO(contents.encode("utf8"))
)
self.assertEqual(result.exit_code, 0)
- output = runner.stdout_bytes
+ output = result.stdout_bytes
self.assertIn(nl.encode("utf8"), output)
if nl == "\n":
self.assertNotIn(b"\r\n", output)