All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
3 from pathlib import Path
4 from typing import Iterator, List, Tuple, Any
5 from contextlib import contextmanager
6 from functools import partial
9 from black.output import out, err
10 from black.debug import DebugVisitor
12 THIS_DIR = Path(__file__).parent
13 PROJECT_ROOT = THIS_DIR.parent
14 EMPTY_LINE = "# EMPTY LINE WITH WHITESPACE" + " (this comment will be removed)"
15 DETERMINISTIC_HEADER = "[Deterministic header]"
18 DEFAULT_MODE = black.Mode()
19 ff = partial(black.format_file_in_place, mode=DEFAULT_MODE, fast=True)
20 fs = partial(black.format_str, mode=DEFAULT_MODE)
23 def dump_to_stderr(*output: str) -> str:
24 return "\n" + "\n".join(output) + "\n"
27 class BlackBaseTestCase(unittest.TestCase):
29 _diffThreshold = 2 ** 20
31 def assertFormatEqual(self, expected: str, actual: str) -> None:
32 if actual != expected and not os.environ.get("SKIP_AST_PRINT"):
33 bdv: DebugVisitor[Any]
34 out("Expected tree:", fg="green")
36 exp_node = black.lib2to3_parse(expected)
38 list(bdv.visit(exp_node))
39 except Exception as ve:
41 out("Actual tree:", fg="red")
43 exp_node = black.lib2to3_parse(actual)
45 list(bdv.visit(exp_node))
46 except Exception as ve:
48 self.assertMultiLineEqual(expected, actual)
51 def read_data(name: str, data: bool = True) -> Tuple[str, str]:
52 """read_data('test_name') -> 'input', 'output'"""
53 if not name.endswith((".py", ".pyi", ".out", ".diff")):
55 base_dir = THIS_DIR / "data" if data else PROJECT_ROOT
56 return read_data_from_file(base_dir / name)
59 def read_data_from_file(file_name: Path) -> Tuple[str, str]:
60 with open(file_name, "r", encoding="utf8") as test:
61 lines = test.readlines()
62 _input: List[str] = []
63 _output: List[str] = []
66 line = line.replace(EMPTY_LINE, "")
67 if line.rstrip() == "# output":
72 if _input and not _output:
73 # If there's no output marker, treat the entire file as already pre-formatted.
75 return "".join(_input).strip() + "\n", "".join(_output).strip() + "\n"
79 def change_directory(path: Path) -> Iterator[None]:
80 """Context manager to temporarily chdir to a different directory."""
81 previous_dir = os.getcwd()
86 os.chdir(previous_dir)