Skip to content

Commit 18d1bf8

Browse files
committed
fix: correct no-prefix no-suffix exclude for top-level dirs
Resolves #975
1 parent f1a3295 commit 18d1bf8

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

bandit/core/manager.py

+7-20
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,7 @@ def discover_files(self, targets, recursive=False, excluded_paths=""):
215215

216216
# if there are command line provided exclusions add them to the list
217217
if excluded_paths:
218-
for path in excluded_paths.split(","):
219-
if os.path.isdir(path):
220-
path = os.path.join(path, "*")
221-
222-
excluded_path_globs.append(path)
218+
excluded_path_globs.extend(excluded_paths.split(","))
223219

224220
# build list of files we will analyze
225221
for fname in targets:
@@ -404,24 +400,15 @@ def _is_file_included(
404400
:param enforce_glob: Can set to false to bypass extension check
405401
:return: Boolean indicating whether a file should be included
406402
"""
407-
return_value = False
408-
409-
# if this is matches a glob of files we look at, and it isn't in an
410-
# excluded path
411-
if _matches_glob_list(path, included_globs) or not enforce_glob:
412-
if not _matches_glob_list(path, excluded_path_strings) and not any(
413-
x in path for x in excluded_path_strings
414-
):
415-
return_value = True
416-
417-
return return_value
403+
if enforce_glob and not _matches_glob_list(path, included_globs):
404+
return False
405+
return not _matches_glob_list(path, excluded_path_strings) and not any(
406+
x in path for x in excluded_path_strings
407+
)
418408

419409

420410
def _matches_glob_list(filename, glob_list):
421-
for glob in glob_list:
422-
if fnmatch.fnmatch(filename, glob):
423-
return True
424-
return False
411+
return any(fnmatch.fnmatch(filename, glob) for glob in glob_list)
425412

426413

427414
def _compare_baseline_results(baseline, results):

tests/unit/core/test_manager.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -255,25 +255,25 @@ def test_discover_files_exclude_dir(self, isdir):
255255
self.assertEqual(["./x/y.py"], self.manager.excluded_files)
256256

257257
# Test exclude dir without wildcard
258-
isdir.side_effect = [True, False]
258+
isdir.side_effect = [False]
259259
self.manager.discover_files(["./x/y.py"], True, "./x/")
260260
self.assertEqual([], self.manager.files_list)
261261
self.assertEqual(["./x/y.py"], self.manager.excluded_files)
262262

263263
# Test exclude dir without wildcard or trailing slash
264-
isdir.side_effect = [True, False]
264+
isdir.side_effect = [False]
265265
self.manager.discover_files(["./x/y.py"], True, "./x")
266266
self.assertEqual([], self.manager.files_list)
267267
self.assertEqual(["./x/y.py"], self.manager.excluded_files)
268268

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

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

0 commit comments

Comments
 (0)