Skip to content

Commit

Permalink
reflected exclude option when use glob (#44)
Browse files Browse the repository at this point in the history
* apply exclude option when use glob

* add unit test for is_exclude_file
  • Loading branch information
sh4869 authored and myint committed Jan 18, 2019
1 parent 9fe7aae commit 5130533
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
24 changes: 16 additions & 8 deletions autoflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ def fix_code(source, additional_imports=None, expand_star_imports=False,
remove_duplicate_keys=remove_duplicate_keys,
remove_unused_variables=remove_unused_variables,
ignore_init_module_imports=ignore_init_module_imports,
))))
))))

if filtered_source == source:
break
Expand Down Expand Up @@ -647,7 +647,7 @@ def fix_file(filename, args, standard_out):
remove_duplicate_keys=args.remove_duplicate_keys,
remove_unused_variables=args.remove_unused_variables,
ignore_init_module_imports=ignore_init_module_imports,
)
)

if original_source != filtered_source:
if args.in_place:
Expand Down Expand Up @@ -745,18 +745,25 @@ def is_python_file(filename):
return True


def match_file(filename, exclude):
"""Return True if file is okay for modifying/recursing."""
def is_exclude_file(filename, exclude):
"""Return True if file matches exclude pattern."""
base_name = os.path.basename(filename)

if base_name.startswith('.'):
return False
return True

for pattern in exclude:
if fnmatch.fnmatch(base_name, pattern):
return False
return True
if fnmatch.fnmatch(filename, pattern):
return False
return True
return False


def match_file(filename, exclude):
"""Return True if file is okay for modifying/recursing."""
if is_exclude_file(filename, exclude):
return False

if not os.path.isdir(filename) and not is_python_file(filename):
return False
Expand All @@ -777,7 +784,8 @@ def find_files(filenames, recursive, exclude):
if match_file(os.path.join(root, d),
exclude)]
else:
yield name
if not is_exclude_file(name, exclude):
yield name


def _main(argv, standard_out, standard_error):
Expand Down
17 changes: 17 additions & 0 deletions test_autoflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,23 @@ def test_is_python_file(self):
self.assertFalse(autoflake.is_python_file(os.devnull))
self.assertFalse(autoflake.is_python_file('/bin/bash'))

def test_is_exclude_file(self):
self.assertTrue(autoflake.is_exclude_file(
"1.py", ["test*", "1*"]))

self.assertFalse(autoflake.is_exclude_file(
"2.py", ["test*", "1*"]))

# folder glob
self.assertTrue(autoflake.is_exclude_file(
"test/test.py", ["test/**.py"]))

self.assertTrue(autoflake.is_exclude_file(
"test/auto_test.py", ["test/*_test.py"]))

self.assertFalse(autoflake.is_exclude_file(
"test/auto_auto.py", ["test/*_test.py"]))

def test_match_file(self):
with temporary_file('', suffix='.py', prefix='.') as filename:
self.assertFalse(autoflake.match_file(filename, exclude=[]),
Expand Down

0 comments on commit 5130533

Please sign in to comment.