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..cc5e3508d 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,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 @@ -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)