Kaydet (Commit) 00d404e4 authored tarafından Jake Kaufman's avatar Jake Kaufman

Add '-' param to read from stdin

Tools like ALE and so forth generally pass the buffer of the editor
directly to the tool to edit and expect to read it back on stdout. I've
added the `-` to signal to add-trailing-comma to read data from stdin
and output to stdout.

I modeled this mostly off of black. Black doesn't have any error
handling around passing multiple files and `-`. Black also only briefly
mentions it in the docs.

I've also added a basic tests to check that a (mocked) stdin works.
üst 4dff0db2
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
import argparse
......@@ -405,24 +406,29 @@ def _fix_src(contents_text, py35_plus, py36_plus):
def fix_file(filename, args):
with open(filename, 'rb') as f:
contents_bytes = f.read()
if filename == '-':
contents_bytes = getattr(sys.stdin, 'buffer', sys.stdin).read()
else:
with open(filename, 'rb') as f:
contents_bytes = f.read()
try:
contents_text_orig = contents_text = contents_bytes.decode('UTF-8')
except UnicodeDecodeError:
print('{} is non-utf-8 (not supported)'.format(filename))
msg = '{} is non-utf-8 (not supported)'.format(filename)
print(msg, file=sys.stderr)
return 1
contents_text = _fix_src(contents_text, args.py35_plus, args.py36_plus)
if contents_text != contents_text_orig:
print('Rewriting {}'.format(filename))
if filename == '-':
print(contents_text, end='')
elif contents_text != contents_text_orig:
print('Rewriting {}'.format(filename), file=sys.stderr)
with io.open(filename, 'w', newline='', encoding='UTF-8') as f:
f.write(contents_text)
return 1
return 0
return contents_text != contents_text_orig
def main(argv=None):
......
coverage
flake8
mock
pre-commit
pytest
......@@ -3,8 +3,10 @@ from __future__ import absolute_import
from __future__ import unicode_literals
import ast
import io
import sys
import mock
import pytest
from add_trailing_comma import _fix_src
......@@ -846,8 +848,8 @@ def test_main_changes_a_file(tmpdir, capsys):
f = tmpdir.join('f.py')
f.write('x(\n 1\n)\n')
assert main((f.strpath,)) == 1
out, _ = capsys.readouterr()
assert out == 'Rewriting {}\n'.format(f.strpath)
_, err = capsys.readouterr()
assert err == 'Rewriting {}\n'.format(f.strpath)
assert f.read() == 'x(\n 1,\n)\n'
......@@ -855,8 +857,8 @@ def test_main_preserves_line_endings(tmpdir, capsys):
f = tmpdir.join('f.py')
f.write_binary(b'x(\r\n 1\r\n)\r\n')
assert main((f.strpath,)) == 1
out, _ = capsys.readouterr()
assert out == 'Rewriting {}\n'.format(f.strpath)
_, err = capsys.readouterr()
assert err == 'Rewriting {}\n'.format(f.strpath)
assert f.read_binary() == b'x(\r\n 1,\r\n)\r\n'
......@@ -870,8 +872,8 @@ def test_main_non_utf8_bytes(tmpdir, capsys):
f = tmpdir.join('f.py')
f.write_binary('# -*- coding: cp1252 -*-\nx = €\n'.encode('cp1252'))
assert main((f.strpath,)) == 1
out, _ = capsys.readouterr()
assert out == '{} is non-utf-8 (not supported)\n'.format(f.strpath)
_, err = capsys.readouterr()
assert err == '{} is non-utf-8 (not supported)\n'.format(f.strpath)
def test_main_py35_plus_argument_star_args(tmpdir):
......@@ -911,3 +913,23 @@ def test_main_py36_plus_function_trailing_commas(
assert f.read() == 'def f(\n **kwargs\n): pass\n'
assert main((f.strpath, '--py36-plus')) == 1
assert f.read() == 'def f(\n **kwargs,\n): pass\n'
def test_main_stdin_no_changes(capsys):
with mock.patch.object(
sys, 'stdin',
io.TextIOWrapper(io.BytesIO(b'x = 5\n'), 'UTF-8'),
):
assert main(('-',)) == 0
out, err = capsys.readouterr()
assert out == 'x = 5\n'
def test_main_stdin_with_changes(capsys):
with mock.patch.object(
sys, 'stdin',
io.TextIOWrapper(io.BytesIO(b'x(\n 1\n)\n'), 'UTF-8'),
):
assert main(('-',)) == 1
out, err = capsys.readouterr()
assert out == 'x(\n 1,\n)\n'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment