Skip to content

Commit

Permalink
Add check argument (#45)
Browse files Browse the repository at this point in the history
* Add check argument

By specifying the --check argument, the program will exit with a code of 1. This is useful if using as part of a CI pipeline that should fail if changes are neccesary

* Remove extra newlines
  • Loading branch information
jamescurtin authored and myint committed Feb 9, 2019
1 parent 5130533 commit 61b09b3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
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

0 comments on commit 61b09b3

Please sign in to comment.