Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pyformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ def format_file(filename, args, standard_out):
with autopep8.open_with_encoding(filename, mode='w',
encoding=encoding) as output_file:
output_file.write(formatted_source)
else:
elif args.diff:
diff = autopep8.get_diff_text(
io.StringIO(source).readlines(),
io.StringIO(formatted_source).readlines(),
filename)
standard_out.write(''.join(diff))
else:
standard_out.write(formatted_source)

return True

Expand Down Expand Up @@ -169,6 +171,8 @@ def parse_args(argv):
parser = argparse.ArgumentParser(description=__doc__, prog='pyformat')
parser.add_argument('-i', '--in-place', action='store_true',
help='make changes to files instead of printing diffs')
parser.add_argument('-d', '--diff', action='store_true',
help='print the diff for the fixed source')
parser.add_argument('-r', '--recursive', action='store_true',
help='drill down directories recursively')
parser.add_argument('-a', '--aggressive', action='count', default=0,
Expand Down
10 changes: 6 additions & 4 deletions test_pyformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_diff(self):
x = "abc"
''') as filename:
output_file = io.StringIO()
pyformat._main(argv=['my_fake_program', filename],
pyformat._main(argv=['my_fake_program','--diff', filename],
standard_out=output_file,
standard_error=None)
self.assertEqual('''\
Expand All @@ -166,7 +166,7 @@ def test_diff_with_aggressive(self):
x = "abc"
''') as filename:
output_file = io.StringIO()
pyformat._main(argv=['my_fake_program', '--aggressive', filename],
pyformat._main(argv=['my_fake_program', '--diff', '--aggressive', filename],
standard_out=output_file,
standard_error=None)
self.assertEqual('''\
Expand All @@ -193,7 +193,7 @@ def test_diff_with_encoding_declaration(self):
x = 1
""") as filename:
output_file = io.StringIO()
pyformat._main(argv=['my_fake_program', '--aggressive', filename],
pyformat._main(argv=['my_fake_program', '--diff', '--aggressive', filename],
standard_out=output_file,
standard_error=None)
self.assertEqual("""\
Expand Down Expand Up @@ -321,6 +321,7 @@ def test_recursive(self):
'--recursive',
'--exclude=zap',
'--exclude=x*oo*',
'--diff',
directory],
standard_out=output_file,
standard_error=None)
Expand Down Expand Up @@ -397,7 +398,7 @@ def test_end_to_end(self):
import os
x = "abc"
""") as filename:
output = subprocess.check_output(PYFORMAT_COMMAND + [filename])
output = subprocess.check_output(PYFORMAT_COMMAND + ['--diff', filename])
self.assertEqual("""\
import os
-x = "abc"
Expand Down Expand Up @@ -436,6 +437,7 @@ def test_no_config(self):

output_file = io.StringIO()
pyformat._main(argv=['my_fake_program',
'--diff',
'--aggressive',
'--no-config',
filename],
Expand Down