Skip to content

Commit

Permalink
fix: correct no-prefix no-suffix exclude for top-level dirs
Browse files Browse the repository at this point in the history
Resolves PyCQA#975
  • Loading branch information
b-kamphorst committed May 11, 2023
1 parent f1a3295 commit 18d1bf8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
27 changes: 7 additions & 20 deletions bandit/core/manager.py
Expand Up @@ -215,11 +215,7 @@ def discover_files(self, targets, recursive=False, excluded_paths=""):

# if there are command line provided exclusions add them to the list
if excluded_paths:
for path in excluded_paths.split(","):
if os.path.isdir(path):
path = os.path.join(path, "*")

excluded_path_globs.append(path)
excluded_path_globs.extend(excluded_paths.split(","))

# build list of files we will analyze
for fname in targets:
Expand Down Expand Up @@ -404,24 +400,15 @@ def _is_file_included(
:param enforce_glob: Can set to false to bypass extension check
:return: Boolean indicating whether a file should be included
"""
return_value = False

# if this is matches a glob of files we look at, and it isn't in an
# excluded path
if _matches_glob_list(path, included_globs) or not enforce_glob:
if not _matches_glob_list(path, excluded_path_strings) and not any(
x in path for x in excluded_path_strings
):
return_value = True

return return_value
if enforce_glob and not _matches_glob_list(path, included_globs):
return False
return not _matches_glob_list(path, excluded_path_strings) and not any(
x in path for x in excluded_path_strings
)


def _matches_glob_list(filename, glob_list):
for glob in glob_list:
if fnmatch.fnmatch(filename, glob):
return True
return False
return any(fnmatch.fnmatch(filename, glob) for glob in glob_list)


def _compare_baseline_results(baseline, results):
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/core/test_manager.py
Expand Up @@ -255,25 +255,25 @@ def test_discover_files_exclude_dir(self, isdir):
self.assertEqual(["./x/y.py"], self.manager.excluded_files)

# Test exclude dir without wildcard
isdir.side_effect = [True, False]
isdir.side_effect = [False]
self.manager.discover_files(["./x/y.py"], True, "./x/")
self.assertEqual([], self.manager.files_list)
self.assertEqual(["./x/y.py"], self.manager.excluded_files)

# Test exclude dir without wildcard or trailing slash
isdir.side_effect = [True, False]
isdir.side_effect = [False]
self.manager.discover_files(["./x/y.py"], True, "./x")
self.assertEqual([], self.manager.files_list)
self.assertEqual(["./x/y.py"], self.manager.excluded_files)

# Test exclude top-level dir without prefix or suffix
isdir.side_effect = [True, False]
isdir.side_effect = [False]
self.manager.discover_files(["./x/y/z.py"], True, "x")
self.assertEqual([], self.manager.files_list)
self.assertEqual(["./x/y/z.py"], self.manager.excluded_files)

# Test exclude lower-level dir without prefix or suffix
isdir.side_effect = [False, False]
isdir.side_effect = [False]
self.manager.discover_files(["./x/y/z.py"], True, "y")
self.assertEqual([], self.manager.files_list)
self.assertEqual(["./x/y/z.py"], self.manager.excluded_files)
Expand Down

0 comments on commit 18d1bf8

Please sign in to comment.