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")
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")
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)])
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")
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")
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")
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)
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")
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")
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)
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:
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)
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")
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")
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)
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:
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)
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")
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")
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)
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:
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():
@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")