Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion bandit/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,21 @@ def main():
args.msg_template,
)

if (
skipped = getattr(b_mgr, "skipped", None)
if not isinstance(skipped, list):
skipped = []

has_scan_error = any(
isinstance(item, tuple)
and len(item) >= 2
and isinstance(item[1], str)
and "No such file or directory" in item[1]
for item in skipped
)

if has_scan_error:
sys.exit(1)
elif (
b_mgr.results_count(sev_filter=sev_level, conf_filter=conf_level) > 0
and not args.exit_zero
):
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_example_nonexistent(self):
"nonexistent.py",
],
)
self.assertEqual(0, retcode)
self.assertEqual(1, retcode)
self.assertIn("Files skipped (1):", output)
self.assertIn("nonexistent.py (No such file or directory", output)

Expand Down
16 changes: 14 additions & 2 deletions tests/unit/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def test_main_exit_with_results(self):
self.assertRaisesRegex(SystemExit, "1", bandit.main)

@mock.patch(
"sys.argv", ["bandit", "-c", "bandit.yaml", "test", "-o", "output"]
"sys.argv", ["bandit", "-c", "bandit.yaml", ".", "-o", "output"]
)
def test_main_exit_with_no_results(self):
# Test that bandit exits when there are no results
Expand All @@ -312,7 +312,7 @@ def test_main_exit_with_no_results(self):

@mock.patch(
"sys.argv",
["bandit", "-c", "bandit.yaml", "test", "-o", "output", "--exit-zero"],
["bandit", "-c", "bandit.yaml", ".", "-o", "output", "--exit-zero"],
)
def test_main_exit_with_results_and_with_exit_zero_flag(self):
# Test that bandit exits with 0 on results and zero flag
Expand All @@ -326,3 +326,15 @@ def test_main_exit_with_results_and_with_exit_zero_flag(self):
mock_mgr_results_ct.return_value = 1

self.assertRaisesRegex(SystemExit, "0", bandit.main)

@mock.patch(
"sys.argv",
["bandit", "-c", "bandit.yaml", "nonexistent_dir", "-o", "output"],
)
def test_main_exit_with_invalid_target(self):
temp_directory = self.useFixture(fixtures.TempDir()).path
os.chdir(temp_directory)
with open("bandit.yaml", "w") as fd:
fd.write(bandit_config_content)

self.assertRaisesRegex(SystemExit, "1", bandit.main)