Don't write back stdin to stdout when --check is passed

This commit is contained in:
Łukasz Langa 2018-03-20 18:20:20 -07:00
parent 5bc40707af
commit d1e0d79e38
2 changed files with 9 additions and 4 deletions

View File

@ -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,6 +193,7 @@ def format_stdin_to_stdout(line_length: int, fast: bool) -> bool:
return False
finally:
if write_back:
sys.stdout.write(contents)

View File

@ -89,7 +89,7 @@ def test_piping(self) -> None:
try:
sys.stdin, sys.stdout = StringIO(source), StringIO()
sys.stdin.name = '<stdin>'
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: