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.
2 from functools import partial
3 from io import StringIO
5 from pathlib import Path
7 from typing import Any, List, Tuple
9 from unittest.mock import patch
11 from click import unstyle
16 ff = partial(black.format_file_in_place, line_length=ll, fast=True)
17 fs = partial(black.format_str, line_length=ll)
18 THIS_FILE = Path(__file__)
19 THIS_DIR = THIS_FILE.parent
20 EMPTY_LINE = "# EMPTY LINE WITH WHITESPACE" + " (this comment will be removed)"
23 def dump_to_stderr(*output: str) -> str:
24 return "\n" + "\n".join(output) + "\n"
27 def read_data(name: str) -> Tuple[str, str]:
28 """read_data('test_name') -> 'input', 'output'"""
29 if not name.endswith((".py", ".out", ".diff")):
31 _input: List[str] = []
32 _output: List[str] = []
33 with open(THIS_DIR / name, "r", encoding="utf8") as test:
34 lines = test.readlines()
37 line = line.replace(EMPTY_LINE, "")
38 if line.rstrip() == "# output":
43 if _input and not _output:
44 # If there's no output marker, treat the entire file as already pre-formatted.
46 return "".join(_input).strip() + "\n", "".join(_output).strip() + "\n"
49 class BlackTestCase(unittest.TestCase):
52 def assertFormatEqual(self, expected: str, actual: str) -> None:
53 if actual != expected and not os.environ.get("SKIP_AST_PRINT"):
54 bdv: black.DebugVisitor[Any]
55 black.out("Expected tree:", fg="green")
57 exp_node = black.lib2to3_parse(expected)
58 bdv = black.DebugVisitor()
59 list(bdv.visit(exp_node))
60 except Exception as ve:
62 black.out("Actual tree:", fg="red")
64 exp_node = black.lib2to3_parse(actual)
65 bdv = black.DebugVisitor()
66 list(bdv.visit(exp_node))
67 except Exception as ve:
69 self.assertEqual(expected, actual)
71 @patch("black.dump_to_file", dump_to_stderr)
72 def test_self(self) -> None:
73 source, expected = read_data("test_black")
75 self.assertFormatEqual(expected, actual)
76 black.assert_equivalent(source, actual)
77 black.assert_stable(source, actual, line_length=ll)
78 self.assertFalse(ff(THIS_FILE))
80 @patch("black.dump_to_file", dump_to_stderr)
81 def test_black(self) -> None:
82 source, expected = read_data("../black")
84 self.assertFormatEqual(expected, actual)
85 black.assert_equivalent(source, actual)
86 black.assert_stable(source, actual, line_length=ll)
87 self.assertFalse(ff(THIS_DIR / ".." / "black.py"))
89 def test_piping(self) -> None:
90 source, expected = read_data("../black")
91 hold_stdin, hold_stdout = sys.stdin, sys.stdout
93 sys.stdin, sys.stdout = StringIO(source), StringIO()
94 sys.stdin.name = "<stdin>"
95 black.format_stdin_to_stdout(
96 line_length=ll, fast=True, write_back=black.WriteBack.YES
99 actual = sys.stdout.read()
101 sys.stdin, sys.stdout = hold_stdin, hold_stdout
102 self.assertFormatEqual(expected, actual)
103 black.assert_equivalent(source, actual)
104 black.assert_stable(source, actual, line_length=ll)
106 def test_piping_diff(self) -> None:
107 source, _ = read_data("expression.py")
108 expected, _ = read_data("expression.diff")
109 hold_stdin, hold_stdout = sys.stdin, sys.stdout
111 sys.stdin, sys.stdout = StringIO(source), StringIO()
112 sys.stdin.name = "<stdin>"
113 black.format_stdin_to_stdout(
114 line_length=ll, fast=True, write_back=black.WriteBack.DIFF
117 actual = sys.stdout.read()
119 sys.stdin, sys.stdout = hold_stdin, hold_stdout
120 actual = actual.rstrip() + "\n" # the diff output has a trailing space
121 self.assertEqual(expected, actual)
123 @patch("black.dump_to_file", dump_to_stderr)
124 def test_setup(self) -> None:
125 source, expected = read_data("../setup")
127 self.assertFormatEqual(expected, actual)
128 black.assert_equivalent(source, actual)
129 black.assert_stable(source, actual, line_length=ll)
130 self.assertFalse(ff(THIS_DIR / ".." / "setup.py"))
132 @patch("black.dump_to_file", dump_to_stderr)
133 def test_function(self) -> None:
134 source, expected = read_data("function")
136 self.assertFormatEqual(expected, actual)
137 black.assert_equivalent(source, actual)
138 black.assert_stable(source, actual, line_length=ll)
140 @patch("black.dump_to_file", dump_to_stderr)
141 def test_expression(self) -> None:
142 source, expected = read_data("expression")
144 self.assertFormatEqual(expected, actual)
145 black.assert_equivalent(source, actual)
146 black.assert_stable(source, actual, line_length=ll)
148 def test_expression_ff(self) -> None:
149 source, expected = read_data("expression")
150 tmp_file = Path(black.dump_to_file(source))
152 self.assertTrue(ff(tmp_file, write_back=black.WriteBack.YES))
153 with open(tmp_file) as f:
157 self.assertFormatEqual(expected, actual)
158 with patch("black.dump_to_file", dump_to_stderr):
159 black.assert_equivalent(source, actual)
160 black.assert_stable(source, actual, line_length=ll)
162 def test_expression_diff(self) -> None:
163 source, _ = read_data("expression.py")
164 expected, _ = read_data("expression.diff")
165 tmp_file = Path(black.dump_to_file(source))
166 hold_stdout = sys.stdout
168 sys.stdout = StringIO()
169 self.assertTrue(ff(tmp_file, write_back=black.WriteBack.DIFF))
171 actual = sys.stdout.read()
172 actual = actual.replace(tmp_file.name, "<stdin>")
174 sys.stdout = hold_stdout
176 actual = actual.rstrip() + "\n" # the diff output has a trailing space
177 self.assertEqual(expected, actual)
179 @patch("black.dump_to_file", dump_to_stderr)
180 def test_fstring(self) -> None:
181 source, expected = read_data("fstring")
183 self.assertFormatEqual(expected, actual)
184 black.assert_equivalent(source, actual)
185 black.assert_stable(source, actual, line_length=ll)
187 @patch("black.dump_to_file", dump_to_stderr)
188 def test_string_quotes(self) -> None:
189 source, expected = read_data("string_quotes")
191 self.assertFormatEqual(expected, actual)
192 black.assert_equivalent(source, actual)
193 black.assert_stable(source, actual, line_length=ll)
195 @patch("black.dump_to_file", dump_to_stderr)
196 def test_comments(self) -> None:
197 source, expected = read_data("comments")
199 self.assertFormatEqual(expected, actual)
200 black.assert_equivalent(source, actual)
201 black.assert_stable(source, actual, line_length=ll)
203 @patch("black.dump_to_file", dump_to_stderr)
204 def test_comments2(self) -> None:
205 source, expected = read_data("comments2")
207 self.assertFormatEqual(expected, actual)
208 black.assert_equivalent(source, actual)
209 black.assert_stable(source, actual, line_length=ll)
211 @patch("black.dump_to_file", dump_to_stderr)
212 def test_comments3(self) -> None:
213 source, expected = read_data("comments3")
215 self.assertFormatEqual(expected, actual)
216 black.assert_equivalent(source, actual)
217 black.assert_stable(source, actual, line_length=ll)
219 @patch("black.dump_to_file", dump_to_stderr)
220 def test_comments4(self) -> None:
221 source, expected = read_data("comments4")
223 self.assertFormatEqual(expected, actual)
224 black.assert_equivalent(source, actual)
225 black.assert_stable(source, actual, line_length=ll)
227 @patch("black.dump_to_file", dump_to_stderr)
228 def test_cantfit(self) -> None:
229 source, expected = read_data("cantfit")
231 self.assertFormatEqual(expected, actual)
232 black.assert_equivalent(source, actual)
233 black.assert_stable(source, actual, line_length=ll)
235 @patch("black.dump_to_file", dump_to_stderr)
236 def test_import_spacing(self) -> None:
237 source, expected = read_data("import_spacing")
239 self.assertFormatEqual(expected, actual)
240 black.assert_equivalent(source, actual)
241 black.assert_stable(source, actual, line_length=ll)
243 @patch("black.dump_to_file", dump_to_stderr)
244 def test_composition(self) -> None:
245 source, expected = read_data("composition")
247 self.assertFormatEqual(expected, actual)
248 black.assert_equivalent(source, actual)
249 black.assert_stable(source, actual, line_length=ll)
251 @patch("black.dump_to_file", dump_to_stderr)
252 def test_empty_lines(self) -> None:
253 source, expected = read_data("empty_lines")
255 self.assertFormatEqual(expected, actual)
256 black.assert_equivalent(source, actual)
257 black.assert_stable(source, actual, line_length=ll)
259 @patch("black.dump_to_file", dump_to_stderr)
260 def test_python2(self) -> None:
261 source, expected = read_data("python2")
263 self.assertFormatEqual(expected, actual)
264 # black.assert_equivalent(source, actual)
265 black.assert_stable(source, actual, line_length=ll)
267 @patch("black.dump_to_file", dump_to_stderr)
268 def test_fmtonoff(self) -> None:
269 source, expected = read_data("fmtonoff")
271 self.assertFormatEqual(expected, actual)
272 black.assert_equivalent(source, actual)
273 black.assert_stable(source, actual, line_length=ll)
275 def test_report(self) -> None:
276 report = black.Report()
280 def out(msg: str, **kwargs: Any) -> None:
281 out_lines.append(msg)
283 def err(msg: str, **kwargs: Any) -> None:
284 err_lines.append(msg)
286 with patch("black.out", out), patch("black.err", err):
287 report.done(Path("f1"), changed=False)
288 self.assertEqual(len(out_lines), 1)
289 self.assertEqual(len(err_lines), 0)
290 self.assertEqual(out_lines[-1], "f1 already well formatted, good job.")
291 self.assertEqual(unstyle(str(report)), "1 file left unchanged.")
292 self.assertEqual(report.return_code, 0)
293 report.done(Path("f2"), changed=True)
294 self.assertEqual(len(out_lines), 2)
295 self.assertEqual(len(err_lines), 0)
296 self.assertEqual(out_lines[-1], "reformatted f2")
298 unstyle(str(report)), "1 file reformatted, 1 file left unchanged."
300 self.assertEqual(report.return_code, 0)
302 self.assertEqual(report.return_code, 1)
304 report.failed(Path("e1"), "boom")
305 self.assertEqual(len(out_lines), 2)
306 self.assertEqual(len(err_lines), 1)
307 self.assertEqual(err_lines[-1], "error: cannot format e1: boom")
309 unstyle(str(report)),
310 "1 file reformatted, 1 file left unchanged, "
311 "1 file failed to reformat.",
313 self.assertEqual(report.return_code, 123)
314 report.done(Path("f3"), changed=True)
315 self.assertEqual(len(out_lines), 3)
316 self.assertEqual(len(err_lines), 1)
317 self.assertEqual(out_lines[-1], "reformatted f3")
319 unstyle(str(report)),
320 "2 files reformatted, 1 file left unchanged, "
321 "1 file failed to reformat.",
323 self.assertEqual(report.return_code, 123)
324 report.failed(Path("e2"), "boom")
325 self.assertEqual(len(out_lines), 3)
326 self.assertEqual(len(err_lines), 2)
327 self.assertEqual(err_lines[-1], "error: cannot format e2: boom")
329 unstyle(str(report)),
330 "2 files reformatted, 1 file left unchanged, "
331 "2 files failed to reformat.",
333 self.assertEqual(report.return_code, 123)
334 report.done(Path("f4"), changed=False)
335 self.assertEqual(len(out_lines), 4)
336 self.assertEqual(len(err_lines), 2)
337 self.assertEqual(out_lines[-1], "f4 already well formatted, good job.")
339 unstyle(str(report)),
340 "2 files reformatted, 2 files left unchanged, "
341 "2 files failed to reformat.",
343 self.assertEqual(report.return_code, 123)
346 unstyle(str(report)),
347 "2 files would be reformatted, 2 files would be left unchanged, "
348 "2 files would fail to reformat.",
351 def test_is_python36(self) -> None:
352 node = black.lib2to3_parse("def f(*, arg): ...\n")
353 self.assertFalse(black.is_python36(node))
354 node = black.lib2to3_parse("def f(*, arg,): ...\n")
355 self.assertTrue(black.is_python36(node))
356 node = black.lib2to3_parse("def f(*, arg): f'string'\n")
357 self.assertTrue(black.is_python36(node))
358 source, expected = read_data("function")
359 node = black.lib2to3_parse(source)
360 self.assertTrue(black.is_python36(node))
361 node = black.lib2to3_parse(expected)
362 self.assertTrue(black.is_python36(node))
363 source, expected = read_data("expression")
364 node = black.lib2to3_parse(source)
365 self.assertFalse(black.is_python36(node))
366 node = black.lib2to3_parse(expected)
367 self.assertFalse(black.is_python36(node))
369 def test_debug_visitor(self) -> None:
370 source, _ = read_data("debug_visitor.py")
371 expected, _ = read_data("debug_visitor.out")
375 def out(msg: str, **kwargs: Any) -> None:
376 out_lines.append(msg)
378 def err(msg: str, **kwargs: Any) -> None:
379 err_lines.append(msg)
381 with patch("black.out", out), patch("black.err", err):
382 black.DebugVisitor.show(source)
383 actual = "\n".join(out_lines) + "\n"
385 if expected != actual:
386 log_name = black.dump_to_file(*out_lines)
390 f"AST print out is different. Actual version dumped to {log_name}",
393 def test_format_file_contents(self) -> None:
395 with self.assertRaises(black.NothingChanged):
396 black.format_file_contents(empty, line_length=ll, fast=False)
398 with self.assertRaises(black.NothingChanged):
399 black.format_file_contents(just_nl, line_length=ll, fast=False)
400 same = "l = [1, 2, 3]\n"
401 with self.assertRaises(black.NothingChanged):
402 black.format_file_contents(same, line_length=ll, fast=False)
403 different = "l = [1,2,3]"
405 actual = black.format_file_contents(different, line_length=ll, fast=False)
406 self.assertEqual(expected, actual)
407 invalid = "return if you can"
408 with self.assertRaises(ValueError) as e:
409 black.format_file_contents(invalid, line_length=ll, fast=False)
410 self.assertEqual(str(e.exception), "Cannot parse: 1:7: return if you can")
412 def test_endmarker(self) -> None:
413 n = black.lib2to3_parse("\n")
414 self.assertEqual(n.type, black.syms.file_input)
415 self.assertEqual(len(n.children), 1)
416 self.assertEqual(n.children[0].type, black.token.ENDMARKER)
418 @unittest.skipIf(os.environ.get("SKIP_AST_PRINT"), "user set SKIP_AST_PRINT")
419 def test_assertFormatEqual(self) -> None:
423 def out(msg: str, **kwargs: Any) -> None:
424 out_lines.append(msg)
426 def err(msg: str, **kwargs: Any) -> None:
427 err_lines.append(msg)
429 with patch("black.out", out), patch("black.err", err):
430 with self.assertRaises(AssertionError):
431 self.assertFormatEqual("l = [1, 2, 3]", "l = [1, 2, 3,]")
433 out_str = "".join(out_lines)
434 self.assertTrue("Expected tree:" in out_str)
435 self.assertTrue("Actual tree:" in out_str)
436 self.assertEqual("".join(err_lines), "")
439 if __name__ == "__main__":