From bcd67fce2da0ff36de4a8bad4b35de652b0ab2ee Mon Sep 17 00:00:00 2001 From: Stephanie Oh Date: Mon, 27 Apr 2026 22:55:52 -0400 Subject: [PATCH 1/2] Fix exit code for invalid scan targets --- bandit/cli/main.py | 16 +++++++++++++++- tests/functional/test_runtime.py | 2 +- tests/unit/cli/test_main.py | 20 +++++++++++++++++--- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/bandit/cli/main.py b/bandit/cli/main.py index d7dba2efa..ba35c5fdf 100644 --- a/bandit/cli/main.py +++ b/bandit/cli/main.py @@ -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 ): diff --git a/tests/functional/test_runtime.py b/tests/functional/test_runtime.py index a9eb21608..a06ff3e24 100644 --- a/tests/functional/test_runtime.py +++ b/tests/functional/test_runtime.py @@ -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) diff --git a/tests/unit/cli/test_main.py b/tests/unit/cli/test_main.py index 98b95ec01..870db190f 100644 --- a/tests/unit/cli/test_main.py +++ b/tests/unit/cli/test_main.py @@ -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 @@ -312,10 +312,11 @@ 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 + #Test that bandit exits with 0 on results and zero flag temp_directory = self.useFixture(fixtures.TempDir()).path os.chdir(temp_directory) with open("bandit.yaml", "w") as fd: @@ -326,3 +327,16 @@ 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) + + \ No newline at end of file From a8504b6326f68caf66e76b759a3120b1ca3e31e6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 28 Apr 2026 03:02:02 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unit/cli/test_main.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/unit/cli/test_main.py b/tests/unit/cli/test_main.py index 870db190f..cc5e3508d 100644 --- a/tests/unit/cli/test_main.py +++ b/tests/unit/cli/test_main.py @@ -314,9 +314,8 @@ def test_main_exit_with_no_results(self): "sys.argv", ["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 + # Test that bandit exits with 0 on results and zero flag temp_directory = self.useFixture(fixtures.TempDir()).path os.chdir(temp_directory) with open("bandit.yaml", "w") as fd: @@ -329,7 +328,8 @@ def test_main_exit_with_results_and_with_exit_zero_flag(self): self.assertRaisesRegex(SystemExit, "0", bandit.main) @mock.patch( - "sys.argv", ["bandit", "-c", "bandit.yaml", "nonexistent_dir", "-o", "output"] + "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 @@ -338,5 +338,3 @@ def test_main_exit_with_invalid_target(self): fd.write(bandit_config_content) self.assertRaisesRegex(SystemExit, "1", bandit.main) - - \ No newline at end of file