Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check argument #45

Merged
merged 2 commits into from
Feb 9, 2019
Merged
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
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Contributors
- Adhika Setya Pramudita (https://github.com/adhikasp)
- Andrew Dassonville (https://github.com/andrewda)
- toddrme2178 (https://github.com/toddrme2178)
- James Curtin (https://github.com/jamescurtin)
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Below is the full listing of options::

optional arguments:
-h, --help show this help message and exit
-c, --check return error code if changes are needed
-i, --in-place make changes to files instead of printing diffs
-r, --recursive drill down directories recursively
--exclude globs exclude file/directory names that match these comma-
Expand Down
8 changes: 8 additions & 0 deletions autoflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,9 @@ def fix_file(filename, args, standard_out):
)

if original_source != filtered_source:
if args.check:
standard_out.write('Unused imports/variables detected.')
sys.exit(1)
if args.in_place:
with open_with_encoding(filename, mode='w',
encoding=encoding) as output_file:
Expand All @@ -660,6 +663,9 @@ def fix_file(filename, args, standard_out):
io.StringIO(filtered_source).readlines(),
filename)
standard_out.write(''.join(diff))
else:
if args.check:
standard_out.write('No issues detected!')


def open_with_encoding(filename, encoding, mode='r',
Expand Down Expand Up @@ -795,6 +801,8 @@ def _main(argv, standard_out, standard_error):
"""
import argparse
parser = argparse.ArgumentParser(description=__doc__, prog='autoflake')
parser.add_argument('-c', '--check', action='store_true',
help='return error code if changes are needed')
parser.add_argument('-i', '--in-place', action='store_true',
help='make changes to files instead of printing diffs')
parser.add_argument('-r', '--recursive', action='store_true',
Expand Down
46 changes: 46 additions & 0 deletions test_autoflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,52 @@ def test_in_place(self):
pass
""", f.read())

def test_check_with_empty_file(self):
line = ''

with temporary_file(line) as filename:
output_file = io.StringIO()
autoflake._main(argv=['my_fake_program', '--check', filename],
standard_out=output_file,
standard_error=None)
self.assertEqual('No issues detected!', output_file.getvalue())

def test_check_correct_file(self):
with temporary_file("""\
import foo
x = foo.bar
print(x)
""") as filename:
output_file = io.StringIO()
autoflake._main(argv=['my_fake_program', '--check', filename],
standard_out=output_file,
standard_error=None)
self.assertEqual('No issues detected!', output_file.getvalue())

def test_check_useless_pass(self):
with temporary_file("""\
import foo
x = foo
import subprocess
x()

try:
pass
import os
except ImportError:
pass
import os
import sys
""") as filename:
output_file = io.StringIO()
with self.assertRaises(SystemExit) as cm:
autoflake._main(argv=['my_fake_program', '--check', filename],
standard_out=output_file,
standard_error=None)
self.assertEqual(cm.exception.code, 1)
self.assertEqual('Unused imports/variables detected.',
output_file.getvalue())

def test_in_place_with_empty_file(self):
line = ''

Expand Down