def test_piping(self) -> None: source, expected = read_data('../black') hold_stdin, hold_stdout = sys.stdin, sys.stdout try: sys.stdin, sys.stdout = StringIO(source), StringIO() sys.stdin.name = '<stdin>' black.format_stdin_to_stdout(line_length=ll, fast=True, write_back=True) sys.stdout.seek(0) actual = sys.stdout.read() finally: sys.stdin, sys.stdout = hold_stdin, hold_stdout self.assertFormatEqual(expected, actual) black.assert_equivalent(source, actual) black.assert_stable(source, actual, line_length=ll)
def test_piping_diff(self) -> None: source, _ = read_data("expression.py") expected, _ = read_data("expression.diff") hold_stdin, hold_stdout = sys.stdin, sys.stdout try: sys.stdin, sys.stdout = StringIO(source), StringIO() sys.stdin.name = "<stdin>" black.format_stdin_to_stdout( line_length=ll, fast=True, write_back=black.WriteBack.DIFF ) sys.stdout.seek(0) actual = sys.stdout.read() finally: sys.stdin, sys.stdout = hold_stdin, hold_stdout actual = actual.rstrip() + "\n" # the diff output has a trailing space self.assertEqual(expected, actual)
def test_piping(self) -> None: source, expected = read_data("../black") hold_stdin, hold_stdout = sys.stdin, sys.stdout try: sys.stdin, sys.stdout = StringIO(source), StringIO() sys.stdin.name = "<stdin>" black.format_stdin_to_stdout( line_length=ll, fast=True, write_back=black.WriteBack.YES ) sys.stdout.seek(0) actual = sys.stdout.read() finally: sys.stdin, sys.stdout = hold_stdin, hold_stdout self.assertFormatEqual(expected, actual) black.assert_equivalent(source, actual) black.assert_stable(source, actual, line_length=ll)
def test_piping(self) -> None: source, expected = read_data("../black") hold_stdin, hold_stdout = sys.stdin, sys.stdout try: sys.stdin = TextIOWrapper(BytesIO(source.encode("utf8")), encoding="utf8") sys.stdout = TextIOWrapper(BytesIO(), encoding="utf8") sys.stdin.buffer.name = "<stdin>" # type: ignore black.format_stdin_to_stdout(line_length=ll, fast=True, write_back=black.WriteBack.YES) sys.stdout.seek(0) actual = sys.stdout.read() finally: sys.stdin, sys.stdout = hold_stdin, hold_stdout self.assertFormatEqual(expected, actual) black.assert_equivalent(source, actual) black.assert_stable(source, actual, line_length=ll)
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") source, _ = read_data("expression.py") expected, _ = read_data("expression.diff") hold_stdin, hold_stdout = sys.stdin, sys.stdout try: sys.stdin = TextIOWrapper(BytesIO(source.encode("utf8")), encoding="utf8") sys.stdout = TextIOWrapper(BytesIO(), encoding="utf8") black.format_stdin_to_stdout(line_length=ll, fast=True, write_back=black.WriteBack.DIFF) sys.stdout.seek(0) actual = sys.stdout.read() actual = diff_header.sub("[Deterministic header]", actual) finally: sys.stdin, sys.stdout = hold_stdin, hold_stdout actual = actual.rstrip() + "\n" # the diff output has a trailing space self.assertEqual(expected, actual)