X-Git-Url: https://git.madduck.net/etc/vim.git/blobdiff_plain/7a14a37981862ef418f3cdb4a7e2375856f97529..8c3c190f9978d08b8b6e505112a019a20cc9e6fc:/tests/test_black.py?ds=inline diff --git a/tests/test_black.py b/tests/test_black.py index 7a4a3bb..8d424ef 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -243,8 +243,8 @@ class BlackTestCase(unittest.TestCase): def test_piping_diff(self) -> None: diff_header = re.compile( - rf"(STDIN|STDOUT)\t\d\d\d\d-\d\d-\d\d " - rf"\d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d" + rf"(STDIN|STDOUT)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d " + rf"\+\d\d\d\d" ) source, _ = read_data("expression.py") expected, _ = read_data("expression.diff") @@ -264,6 +264,28 @@ class BlackTestCase(unittest.TestCase): actual = actual.rstrip() + "\n" # the diff output has a trailing space self.assertEqual(expected, actual) + def test_piping_diff_with_color(self) -> None: + source, _ = read_data("expression.py") + config = THIS_DIR / "data" / "empty_pyproject.toml" + args = [ + "-", + "--fast", + f"--line-length={black.DEFAULT_LINE_LENGTH}", + "--diff", + "--color", + f"--config={config}", + ] + result = BlackRunner().invoke( + black.main, args, input=BytesIO(source.encode("utf8")) + ) + actual = result.output + # Again, the contents are checked in a different test, so only look for colors. + self.assertIn("\033[1;37m", actual) + self.assertIn("\033[36m", actual) + self.assertIn("\033[32m", actual) + self.assertIn("\033[31m", actual) + self.assertIn("\033[0m", actual) + @patch("black.dump_to_file", dump_to_stderr) def test_function(self) -> None: source, expected = read_data("function") @@ -333,7 +355,7 @@ class BlackTestCase(unittest.TestCase): tmp_file = Path(black.dump_to_file(source)) diff_header = re.compile( rf"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d " - rf"\d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d" + r"\d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d" ) try: result = BlackRunner().invoke(black.main, ["--diff", str(tmp_file)]) @@ -346,12 +368,31 @@ class BlackTestCase(unittest.TestCase): if expected != actual: dump = black.dump_to_file(actual) msg = ( - f"Expected diff isn't equal to the actual. If you made changes " - f"to expression.py and this is an anticipated difference, " - f"overwrite tests/data/expression.diff with {dump}" + "Expected diff isn't equal to the actual. If you made changes to" + " expression.py and this is an anticipated difference, overwrite" + f" tests/data/expression.diff with {dump}" ) self.assertEqual(expected, actual, msg) + def test_expression_diff_with_color(self) -> None: + source, _ = read_data("expression.py") + expected, _ = read_data("expression.diff") + tmp_file = Path(black.dump_to_file(source)) + try: + result = BlackRunner().invoke( + black.main, ["--diff", "--color", str(tmp_file)] + ) + finally: + os.unlink(tmp_file) + actual = result.output + # We check the contents of the diff in `test_expression_diff`. All + # we need to check here is that color codes exist in the result. + self.assertIn("\033[1;37m", actual) + self.assertIn("\033[36m", actual) + self.assertIn("\033[32m", actual) + self.assertIn("\033[31m", actual) + self.assertIn("\033[0m", actual) + @patch("black.dump_to_file", dump_to_stderr) def test_fstring(self) -> None: source, expected = read_data("fstring") @@ -386,10 +427,44 @@ class BlackTestCase(unittest.TestCase): black.assert_stable(source, actual, black.FileMode()) mode = black.FileMode(string_normalization=False) not_normalized = fs(source, mode=mode) - self.assertFormatEqual(source, not_normalized) + self.assertFormatEqual(source.replace("\\\n", ""), not_normalized) black.assert_equivalent(source, not_normalized) black.assert_stable(source, not_normalized, mode=mode) + @patch("black.dump_to_file", dump_to_stderr) + def test_docstring(self) -> None: + source, expected = read_data("docstring") + actual = fs(source) + self.assertFormatEqual(expected, actual) + black.assert_equivalent(source, actual) + black.assert_stable(source, actual, black.FileMode()) + + def test_long_strings(self) -> None: + """Tests for splitting long strings.""" + source, expected = read_data("long_strings") + 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_long_strings__edge_case(self) -> None: + """Edge-case tests for splitting long strings.""" + source, expected = read_data("long_strings__edge_case") + 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_long_strings__regression(self) -> None: + """Regression tests for splitting long strings.""" + source, expected = read_data("long_strings__regression") + 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_slices(self) -> None: source, expected = read_data("slices") @@ -632,6 +707,14 @@ class BlackTestCase(unittest.TestCase): black.assert_equivalent(source, actual) black.assert_stable(source, actual, black.FileMode()) + @patch("black.dump_to_file", dump_to_stderr) + def test_fmtonoff4(self) -> None: + source, expected = read_data("fmtonoff4") + 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_remove_empty_parentheses_after_class(self) -> None: source, expected = read_data("class_blank_parentheses") @@ -738,8 +821,8 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(err_lines[-1], "error: cannot format e1: boom") self.assertEqual( unstyle(str(report)), - "1 file reformatted, 2 files left unchanged, " - "1 file failed to reformat.", + "1 file reformatted, 2 files left unchanged, 1 file failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.done(Path("f3"), black.Changed.YES) @@ -748,8 +831,8 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(out_lines[-1], "reformatted f3") self.assertEqual( unstyle(str(report)), - "2 files reformatted, 2 files left unchanged, " - "1 file failed to reformat.", + "2 files reformatted, 2 files left unchanged, 1 file failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.failed(Path("e2"), "boom") @@ -758,8 +841,8 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(err_lines[-1], "error: cannot format e2: boom") self.assertEqual( unstyle(str(report)), - "2 files reformatted, 2 files left unchanged, " - "2 files failed to reformat.", + "2 files reformatted, 2 files left unchanged, 2 files failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.path_ignored(Path("wat"), "no match") @@ -768,8 +851,8 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(out_lines[-1], "wat ignored: no match") self.assertEqual( unstyle(str(report)), - "2 files reformatted, 2 files left unchanged, " - "2 files failed to reformat.", + "2 files reformatted, 2 files left unchanged, 2 files failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.done(Path("f4"), black.Changed.NO) @@ -778,22 +861,22 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(out_lines[-1], "f4 already well formatted, good job.") self.assertEqual( unstyle(str(report)), - "2 files reformatted, 3 files left unchanged, " - "2 files failed to reformat.", + "2 files reformatted, 3 files left unchanged, 2 files failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.check = True self.assertEqual( unstyle(str(report)), - "2 files would be reformatted, 3 files would be left unchanged, " - "2 files would fail to reformat.", + "2 files would be reformatted, 3 files would be left unchanged, 2 files" + " would fail to reformat.", ) report.check = False report.diff = True self.assertEqual( unstyle(str(report)), - "2 files would be reformatted, 3 files would be left unchanged, " - "2 files would fail to reformat.", + "2 files would be reformatted, 3 files would be left unchanged, 2 files" + " would fail to reformat.", ) def test_report_quiet(self) -> None: @@ -835,8 +918,8 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(err_lines[-1], "error: cannot format e1: boom") self.assertEqual( unstyle(str(report)), - "1 file reformatted, 2 files left unchanged, " - "1 file failed to reformat.", + "1 file reformatted, 2 files left unchanged, 1 file failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.done(Path("f3"), black.Changed.YES) @@ -844,8 +927,8 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(len(err_lines), 1) self.assertEqual( unstyle(str(report)), - "2 files reformatted, 2 files left unchanged, " - "1 file failed to reformat.", + "2 files reformatted, 2 files left unchanged, 1 file failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.failed(Path("e2"), "boom") @@ -854,8 +937,8 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(err_lines[-1], "error: cannot format e2: boom") self.assertEqual( unstyle(str(report)), - "2 files reformatted, 2 files left unchanged, " - "2 files failed to reformat.", + "2 files reformatted, 2 files left unchanged, 2 files failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.path_ignored(Path("wat"), "no match") @@ -863,8 +946,8 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(len(err_lines), 2) self.assertEqual( unstyle(str(report)), - "2 files reformatted, 2 files left unchanged, " - "2 files failed to reformat.", + "2 files reformatted, 2 files left unchanged, 2 files failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.done(Path("f4"), black.Changed.NO) @@ -872,22 +955,22 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(len(err_lines), 2) self.assertEqual( unstyle(str(report)), - "2 files reformatted, 3 files left unchanged, " - "2 files failed to reformat.", + "2 files reformatted, 3 files left unchanged, 2 files failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.check = True self.assertEqual( unstyle(str(report)), - "2 files would be reformatted, 3 files would be left unchanged, " - "2 files would fail to reformat.", + "2 files would be reformatted, 3 files would be left unchanged, 2 files" + " would fail to reformat.", ) report.check = False report.diff = True self.assertEqual( unstyle(str(report)), - "2 files would be reformatted, 3 files would be left unchanged, " - "2 files would fail to reformat.", + "2 files would be reformatted, 3 files would be left unchanged, 2 files" + " would fail to reformat.", ) def test_report_normal(self) -> None: @@ -931,8 +1014,8 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(err_lines[-1], "error: cannot format e1: boom") self.assertEqual( unstyle(str(report)), - "1 file reformatted, 2 files left unchanged, " - "1 file failed to reformat.", + "1 file reformatted, 2 files left unchanged, 1 file failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.done(Path("f3"), black.Changed.YES) @@ -941,8 +1024,8 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(out_lines[-1], "reformatted f3") self.assertEqual( unstyle(str(report)), - "2 files reformatted, 2 files left unchanged, " - "1 file failed to reformat.", + "2 files reformatted, 2 files left unchanged, 1 file failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.failed(Path("e2"), "boom") @@ -951,8 +1034,8 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(err_lines[-1], "error: cannot format e2: boom") self.assertEqual( unstyle(str(report)), - "2 files reformatted, 2 files left unchanged, " - "2 files failed to reformat.", + "2 files reformatted, 2 files left unchanged, 2 files failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.path_ignored(Path("wat"), "no match") @@ -960,8 +1043,8 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(len(err_lines), 2) self.assertEqual( unstyle(str(report)), - "2 files reformatted, 2 files left unchanged, " - "2 files failed to reformat.", + "2 files reformatted, 2 files left unchanged, 2 files failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.done(Path("f4"), black.Changed.NO) @@ -969,22 +1052,22 @@ class BlackTestCase(unittest.TestCase): self.assertEqual(len(err_lines), 2) self.assertEqual( unstyle(str(report)), - "2 files reformatted, 3 files left unchanged, " - "2 files failed to reformat.", + "2 files reformatted, 3 files left unchanged, 2 files failed to" + " reformat.", ) self.assertEqual(report.return_code, 123) report.check = True self.assertEqual( unstyle(str(report)), - "2 files would be reformatted, 3 files would be left unchanged, " - "2 files would fail to reformat.", + "2 files would be reformatted, 3 files would be left unchanged, 2 files" + " would fail to reformat.", ) report.check = False report.diff = True self.assertEqual( unstyle(str(report)), - "2 files would be reformatted, 3 files would be left unchanged, " - "2 files would fail to reformat.", + "2 files would be reformatted, 3 files would be left unchanged, 2 files" + " would fail to reformat.", ) def test_lib2to3_parse(self) -> None: @@ -1271,6 +1354,17 @@ class BlackTestCase(unittest.TestCase): mock.side_effect = OSError black.write_cache({}, [], mode) + @patch("black.ProcessPoolExecutor", autospec=True) + def test_works_in_mono_process_only_environment(self, executor: MagicMock) -> None: + executor.side_effect = OSError() + with cache_dir() as workspace: + for f in [ + (workspace / "one.py").resolve(), + (workspace / "two.py").resolve(), + ]: + f.write_text("print('hello')") + self.invokeBlack([str(workspace)]) + @event_loop(close=False) def test_check_diff_use_together(self) -> None: with cache_dir(): @@ -1745,8 +1839,7 @@ class BlackDTestCase(AioHTTPTestCase): @unittest_run_loop async def test_blackd_diff(self) -> None: diff_header = re.compile( - rf"(In|Out)\t\d\d\d\d-\d\d-\d\d " - rf"\d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d" + rf"(In|Out)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d" ) source, _ = read_data("blackd_diff.py")