From: Ɓukasz Langa Date: Wed, 21 Mar 2018 01:20:20 +0000 (-0700) Subject: Don't write back stdin to stdout when --check is passed X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/d1e0d79e38db934a65dded44b903f05ab85829b4?ds=sidebyside;hp=5bc40707afa5fb53bbc2484ed34f69b011b98172 Don't write back stdin to stdout when --check is passed --- diff --git a/black.py b/black.py index 203fbfa..54044a5 100644 --- a/black.py +++ b/black.py @@ -102,7 +102,9 @@ def main( report = Report() try: if not p.is_file() and str(p) == '-': - changed = format_stdin_to_stdout(line_length=line_length, fast=fast) + changed = format_stdin_to_stdout( + line_length=line_length, fast=fast, write_back=not check + ) else: changed = format_file_in_place( p, line_length=line_length, fast=fast, write_back=not check @@ -178,7 +180,9 @@ def format_file_in_place( return True -def format_stdin_to_stdout(line_length: int, fast: bool) -> bool: +def format_stdin_to_stdout( + line_length: int, fast: bool, write_back: bool = False +) -> bool: """Format file on stdin and pipe output to stdout. Return True if changed.""" contents = sys.stdin.read() try: @@ -189,7 +193,8 @@ def format_stdin_to_stdout(line_length: int, fast: bool) -> bool: return False finally: - sys.stdout.write(contents) + if write_back: + sys.stdout.write(contents) def format_file_contents( diff --git a/tests/test_black.py b/tests/test_black.py index ee883ec..7dba611 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -89,7 +89,7 @@ class BlackTestCase(unittest.TestCase): try: sys.stdin, sys.stdout = StringIO(source), StringIO() sys.stdin.name = '' - black.format_stdin_to_stdout(line_length=ll, fast=True) + black.format_stdin_to_stdout(line_length=ll, fast=True, write_back=True) sys.stdout.seek(0) actual = sys.stdout.read() finally: