def test_expression_diff(self) -> None: source, _ = read_data("expression.py") expected, _ = read_data("expression.diff") 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") hold_stdout = sys.stdout try: sys.stdout = TextIOWrapper(BytesIO(), encoding="utf8") self.assertTrue(ff(tmp_file, write_back=black.WriteBack.DIFF)) sys.stdout.seek(0) actual = sys.stdout.read() actual = diff_header.sub("[Deterministic header]", actual) finally: sys.stdout = hold_stdout os.unlink(tmp_file) actual = actual.rstrip() + "\n" # the diff output has a trailing space 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/expression.diff with {dump}") self.assertEqual(expected, actual, msg)
def test_empty_ff(self) -> None: expected = "" tmp_file = Path(black.dump_to_file()) try: self.assertFalse(ff(tmp_file, write_back=black.WriteBack.YES)) with open(tmp_file, encoding="utf8") as f: actual = f.read() finally: os.unlink(tmp_file) self.assertFormatEqual(expected, actual)
def test_expression_ff(self) -> None: source, expected = read_data("expression") tmp_file = Path(black.dump_to_file(source)) try: self.assertTrue(ff(tmp_file, write_back=black.WriteBack.YES)) with open(tmp_file, encoding="utf8") as f: actual = f.read() finally: os.unlink(tmp_file) self.assertFormatEqual(expected, actual) with patch("black.dump_to_file", dump_to_stderr): black.assert_equivalent(source, actual) black.assert_stable(source, actual, line_length=ll)
def test_expression_diff(self) -> None: source, _ = read_data("expression.py") expected, _ = read_data("expression.diff") tmp_file = Path(black.dump_to_file(source)) hold_stdout = sys.stdout try: sys.stdout = StringIO() self.assertTrue(ff(tmp_file, write_back=black.WriteBack.DIFF)) sys.stdout.seek(0) actual = sys.stdout.read() actual = actual.replace(str(tmp_file), "<stdin>") finally: sys.stdout = hold_stdout os.unlink(tmp_file) actual = actual.rstrip() + "\n" # the diff output has a trailing space 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/expression.diff with {dump}") self.assertEqual(expected, actual, msg)
def test_expression_diff(self) -> None: source, _ = read_data("expression.py") expected, _ = read_data("expression.diff") tmp_file = Path(black.dump_to_file(source)) hold_stdout = sys.stdout try: sys.stdout = StringIO() self.assertTrue(ff(tmp_file, write_back=black.WriteBack.DIFF)) sys.stdout.seek(0) actual = sys.stdout.read() actual = actual.replace(str(tmp_file), "<stdin>") finally: sys.stdout = hold_stdout os.unlink(tmp_file) actual = actual.rstrip() + "\n" # the diff output has a trailing space 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/expression.diff with {dump}" ) self.assertEqual(expected, actual, msg)
def test_expression_diff(self) -> None: source, _ = read_data("expression.py") expected, _ = read_data("expression.diff") 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") stderrbuf = BytesIO() try: result = BlackRunner(stderrbuf).invoke( black.main, ["--diff", str(tmp_file)]) self.assertEqual(result.exit_code, 0) finally: os.unlink(tmp_file) actual = result.output actual = diff_header.sub("[Deterministic header]", actual) actual = actual.rstrip() + "\n" # the diff output has a trailing space 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/expression.diff with {dump}") self.assertEqual(expected, actual, msg)
def test_expression_diff(self) -> None: source, _ = read_data("expression.py") expected, _ = read_data("expression.diff") tmp_file = Path(black.dump_to_file(source)) hold_stdout = sys.stdout try: sys.stdout = StringIO() self.assertTrue(ff(tmp_file, write_back=black.WriteBack.DIFF)) sys.stdout.seek(0) actual = sys.stdout.read() actual = actual.replace(tmp_file.name, "<stdin>") finally: sys.stdout = hold_stdout os.unlink(tmp_file) actual = actual.rstrip() + "\n" # the diff output has a trailing space self.assertEqual(expected, actual)
def test_black(): test_file = os.path.join(TEST_DIR, "bad_continuation_tabs.py") with open(test_file, "r") as f: orig_contents = f.read() orig_filepath = dump_to_file(orig_contents) expected_file = test_file.replace(".py", ".blacked.py") with open(expected_file, "r") as f: expected_contents = f.read() transformer = Black() transformer.transform([orig_filepath]) with open(orig_filepath, "r") as f: actual_contents = f.read() os.remove(orig_filepath) assert actual_contents == expected_contents
def test_pyautodev(): test_file = os.path.join(TEST_DIR, "comment_overflow.py") with open(test_file, 'r') as f: orig_contents = f.read() orig_filepath = dump_to_file(orig_contents) expected_file = test_file.replace(".py", ".pyautodev.py") with open(expected_file, "r") as f: expected_contents = f.read() transformer = PyAutoDev() transformer.transform([orig_filepath]) with open(orig_filepath, "r") as f: actual_contents = f.read() os.remove(orig_filepath) assert actual_contents == expected_contents
def test_debug_visitor(self) -> None: source, _ = read_data('debug_visitor.py') expected, _ = read_data('debug_visitor.out') 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): black.DebugVisitor.show(source) actual = '\n'.join(out_lines) + '\n' log_name = '' if expected != actual: log_name = black.dump_to_file(*out_lines) self.assertEqual( expected, actual, f"AST print out is different. Actual version dumped to {log_name}", )
def test_debug_visitor(self) -> None: source, _ = read_data("debug_visitor.py") expected, _ = read_data("debug_visitor.out") 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): black.DebugVisitor.show(source) actual = "\n".join(out_lines) + "\n" log_name = "" if expected != actual: log_name = black.dump_to_file(*out_lines) self.assertEqual( expected, actual, f"AST print out is different. Actual version dumped to {log_name}", )
def test_debug_visitor(self) -> None: source, _ = read_data("debug_visitor.py") expected, _ = read_data("debug_visitor.out") 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("out", out), patch("err", err): DebugVisitor.show(source) actual = "\n".join(out_lines) + "\n" log_name = "" if expected != actual: log_name = dump_to_file(*out_lines) self.assertEqual( expected, actual, f"AST print out is different. Actual version dumped to {log_name}", )