Skip to content

Commit 12e14f6

Browse files
authored
Prepend ./ for files specified as CLI args (#1094)
The get_module_qualname_from_path() function called by the node visistor expects that all files are explicitly named with a "head" and "tail" which are path delimiters to denote where the file is within a python project. However, if someone uses the command line and simply asks bandit to scan dummy.py in the current working directory, it will be missing the explicit "./" prefix in order for get_module_qualname_from_path to run and determine the module fully qualified name from the path. So this fix simply prepends a dot and delimiter to explicitly denote a file in the current working directory as given from the CLI. Fixes #907 Signed-off-by: Eric Brown <[email protected]>
1 parent 0779eb0 commit 12e14f6

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

bandit/core/manager.py

+2
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ def discover_files(self, targets, recursive=False, excluded_paths=""):
249249
excluded_path_globs,
250250
enforce_glob=False,
251251
):
252+
if fname != "-":
253+
fname = os.path.join(".", fname)
252254
files_list.add(fname)
253255
else:
254256
excluded_files.add(fname)

tests/unit/core/test_manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def test_discover_files_exclude_glob(self, isdir):
289289
self.manager.discover_files(
290290
["a.py", "test_a.py", "test.py"], True, excluded_paths="test_*.py"
291291
)
292-
self.assertEqual(["a.py", "test.py"], self.manager.files_list)
292+
self.assertEqual(["./a.py", "./test.py"], self.manager.files_list)
293293
self.assertEqual(["test_a.py"], self.manager.excluded_files)
294294

295295
@mock.patch("os.path.isdir")
@@ -298,7 +298,7 @@ def test_discover_files_include(self, isdir):
298298
with mock.patch.object(manager, "_is_file_included") as m:
299299
m.return_value = True
300300
self.manager.discover_files(["thing"], True)
301-
self.assertEqual(["thing"], self.manager.files_list)
301+
self.assertEqual(["./thing"], self.manager.files_list)
302302
self.assertEqual([], self.manager.excluded_files)
303303

304304
def test_run_tests_keyboardinterrupt(self):

0 commit comments

Comments
 (0)