Skip to content

Commit 61b09b3

Browse files
jamescurtinmyint
authored andcommitted
Add check argument (#45)
* 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
1 parent 5130533 commit 61b09b3

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

AUTHORS.rst

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Contributors
88
- Adhika Setya Pramudita (https://github.com/adhikasp)
99
- Andrew Dassonville (https://github.com/andrewda)
1010
- toddrme2178 (https://github.com/toddrme2178)
11+
- James Curtin (https://github.com/jamescurtin)

README.rst

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Below is the full listing of options::
103103

104104
optional arguments:
105105
-h, --help show this help message and exit
106+
-c, --check return error code if changes are needed
106107
-i, --in-place make changes to files instead of printing diffs
107108
-r, --recursive drill down directories recursively
108109
--exclude globs exclude file/directory names that match these comma-

autoflake.py

+8
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,9 @@ def fix_file(filename, args, standard_out):
650650
)
651651

652652
if original_source != filtered_source:
653+
if args.check:
654+
standard_out.write('Unused imports/variables detected.')
655+
sys.exit(1)
653656
if args.in_place:
654657
with open_with_encoding(filename, mode='w',
655658
encoding=encoding) as output_file:
@@ -660,6 +663,9 @@ def fix_file(filename, args, standard_out):
660663
io.StringIO(filtered_source).readlines(),
661664
filename)
662665
standard_out.write(''.join(diff))
666+
else:
667+
if args.check:
668+
standard_out.write('No issues detected!')
663669

664670

665671
def open_with_encoding(filename, encoding, mode='r',
@@ -795,6 +801,8 @@ def _main(argv, standard_out, standard_error):
795801
"""
796802
import argparse
797803
parser = argparse.ArgumentParser(description=__doc__, prog='autoflake')
804+
parser.add_argument('-c', '--check', action='store_true',
805+
help='return error code if changes are needed')
798806
parser.add_argument('-i', '--in-place', action='store_true',
799807
help='make changes to files instead of printing diffs')
800808
parser.add_argument('-r', '--recursive', action='store_true',

test_autoflake.py

+46
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,52 @@ def test_in_place(self):
13541354
pass
13551355
""", f.read())
13561356

1357+
def test_check_with_empty_file(self):
1358+
line = ''
1359+
1360+
with temporary_file(line) as filename:
1361+
output_file = io.StringIO()
1362+
autoflake._main(argv=['my_fake_program', '--check', filename],
1363+
standard_out=output_file,
1364+
standard_error=None)
1365+
self.assertEqual('No issues detected!', output_file.getvalue())
1366+
1367+
def test_check_correct_file(self):
1368+
with temporary_file("""\
1369+
import foo
1370+
x = foo.bar
1371+
print(x)
1372+
""") as filename:
1373+
output_file = io.StringIO()
1374+
autoflake._main(argv=['my_fake_program', '--check', filename],
1375+
standard_out=output_file,
1376+
standard_error=None)
1377+
self.assertEqual('No issues detected!', output_file.getvalue())
1378+
1379+
def test_check_useless_pass(self):
1380+
with temporary_file("""\
1381+
import foo
1382+
x = foo
1383+
import subprocess
1384+
x()
1385+
1386+
try:
1387+
pass
1388+
import os
1389+
except ImportError:
1390+
pass
1391+
import os
1392+
import sys
1393+
""") as filename:
1394+
output_file = io.StringIO()
1395+
with self.assertRaises(SystemExit) as cm:
1396+
autoflake._main(argv=['my_fake_program', '--check', filename],
1397+
standard_out=output_file,
1398+
standard_error=None)
1399+
self.assertEqual(cm.exception.code, 1)
1400+
self.assertEqual('Unused imports/variables detected.',
1401+
output_file.getvalue())
1402+
13571403
def test_in_place_with_empty_file(self):
13581404
line = ''
13591405

0 commit comments

Comments
 (0)