+ def test_report_quiet(self) -> None:
+ report = black.Report(quiet=True)
+ out_lines = []
+ err_lines = []
+
+ def out(msg: str, **kwargs: Any) -> None:
+ out_lines.append(msg)
+
+ def err(msg: str, **kwargs: Any) -> None:
+ err_lines.append(msg)
+
+ with patch("black.out", out), patch("black.err", err):
+ report.done(Path("f1"), black.Changed.NO)
+ self.assertEqual(len(out_lines), 0)
+ self.assertEqual(len(err_lines), 0)
+ self.assertEqual(unstyle(str(report)), "1 file left unchanged.")
+ self.assertEqual(report.return_code, 0)
+ report.done(Path("f2"), black.Changed.YES)
+ self.assertEqual(len(out_lines), 0)
+ self.assertEqual(len(err_lines), 0)
+ self.assertEqual(
+ unstyle(str(report)), "1 file reformatted, 1 file left unchanged."
+ )
+ report.done(Path("f3"), black.Changed.CACHED)
+ self.assertEqual(len(out_lines), 0)
+ self.assertEqual(len(err_lines), 0)
+ self.assertEqual(
+ unstyle(str(report)), "1 file reformatted, 2 files left unchanged."
+ )
+ self.assertEqual(report.return_code, 0)
+ report.check = True
+ self.assertEqual(report.return_code, 1)
+ report.check = False
+ report.failed(Path("e1"), "boom")
+ self.assertEqual(len(out_lines), 0)
+ self.assertEqual(len(err_lines), 1)
+ 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.",
+ )
+ self.assertEqual(report.return_code, 123)
+ report.done(Path("f3"), black.Changed.YES)
+ self.assertEqual(len(out_lines), 0)
+ self.assertEqual(len(err_lines), 1)
+ self.assertEqual(
+ unstyle(str(report)),
+ "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(len(out_lines), 0)
+ self.assertEqual(len(err_lines), 2)
+ 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.",
+ )
+ self.assertEqual(report.return_code, 123)
+ report.path_ignored(Path("wat"), "no match")
+ self.assertEqual(len(out_lines), 0)
+ self.assertEqual(len(err_lines), 2)
+ self.assertEqual(
+ unstyle(str(report)),
+ "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(out_lines), 0)
+ self.assertEqual(len(err_lines), 2)
+ self.assertEqual(
+ unstyle(str(report)),
+ "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.",
+ )
+
+ def test_report_normal(self) -> None:
+ report = black.Report()
+ out_lines = []
+ err_lines = []
+
+ def out(msg: str, **kwargs: Any) -> None:
+ out_lines.append(msg)
+
+ def err(msg: str, **kwargs: Any) -> None:
+ err_lines.append(msg)
+
+ with patch("black.out", out), patch("black.err", err):
+ report.done(Path("f1"), black.Changed.NO)
+ self.assertEqual(len(out_lines), 0)
+ self.assertEqual(len(err_lines), 0)
+ self.assertEqual(unstyle(str(report)), "1 file left unchanged.")
+ self.assertEqual(report.return_code, 0)
+ report.done(Path("f2"), black.Changed.YES)
+ self.assertEqual(len(out_lines), 1)
+ self.assertEqual(len(err_lines), 0)
+ self.assertEqual(out_lines[-1], "reformatted f2")
+ self.assertEqual(
+ unstyle(str(report)), "1 file reformatted, 1 file left unchanged."
+ )
+ report.done(Path("f3"), black.Changed.CACHED)
+ self.assertEqual(len(out_lines), 1)
+ self.assertEqual(len(err_lines), 0)
+ self.assertEqual(out_lines[-1], "reformatted f2")
+ self.assertEqual(
+ unstyle(str(report)), "1 file reformatted, 2 files left unchanged."
+ )
+ self.assertEqual(report.return_code, 0)
+ report.check = True
+ self.assertEqual(report.return_code, 1)
+ report.check = False
+ report.failed(Path("e1"), "boom")
+ self.assertEqual(len(out_lines), 1)
+ self.assertEqual(len(err_lines), 1)
+ 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.",
+ )
+ self.assertEqual(report.return_code, 123)
+ report.done(Path("f3"), black.Changed.YES)
+ self.assertEqual(len(out_lines), 2)
+ self.assertEqual(len(err_lines), 1)
+ self.assertEqual(out_lines[-1], "reformatted f3")
+ self.assertEqual(
+ unstyle(str(report)),
+ "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(len(out_lines), 2)
+ self.assertEqual(len(err_lines), 2)
+ 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.",
+ )
+ self.assertEqual(report.return_code, 123)
+ report.path_ignored(Path("wat"), "no match")
+ self.assertEqual(len(out_lines), 2)
+ self.assertEqual(len(err_lines), 2)
+ self.assertEqual(
+ unstyle(str(report)),
+ "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(out_lines), 2)
+ self.assertEqual(len(err_lines), 2)
+ self.assertEqual(
+ unstyle(str(report)),
+ "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.",
+ )
+