From affd4fd1f506819de211797f3ea4e11114c31bf1 Mon Sep 17 00:00:00 2001 From: Eric Brown Date: Tue, 4 Feb 2025 17:17:53 -0800 Subject: [PATCH] Warn not error on an nonexistant test given (#1230) When a user gives a test ID to include or skip, the current behavior raises an exception and exits the process. However, when tests end up getting deprecated and eventually removed, it is a lot more user friendly to simple present a warning to the user that the test ID given wasn't found rather than a hard error and exit. Fixes: #1228 Signed-off-by: Eric Brown --- bandit/core/extension_loader.py | 7 +++++-- tests/unit/cli/test_main.py | 26 +------------------------- 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/bandit/core/extension_loader.py b/bandit/core/extension_loader.py index 05fd9fbfc..ec28a0ab9 100644 --- a/bandit/core/extension_loader.py +++ b/bandit/core/extension_loader.py @@ -1,11 +1,14 @@ # # SPDX-License-Identifier: Apache-2.0 +import logging import sys from stevedore import extension from bandit.core import utils +LOG = logging.getLogger(__name__) + class Manager: # These IDs are for bandit built in tests @@ -84,11 +87,11 @@ def validate_profile(self, profile): """Validate that everything in the configured profiles looks good.""" for inc in profile["include"]: if not self.check_id(inc): - raise ValueError(f"Unknown test found in profile: {inc}") + LOG.warning(f"Unknown test found in profile: {inc}") for exc in profile["exclude"]: if not self.check_id(exc): - raise ValueError(f"Unknown test found in profile: {exc}") + LOG.warning(f"Unknown test found in profile: {exc}") union = set(profile["include"]) & set(profile["exclude"]) if len(union) > 0: diff --git a/tests/unit/cli/test_main.py b/tests/unit/cli/test_main.py index 0852db7de..98b95ec01 100644 --- a/tests/unit/cli/test_main.py +++ b/tests/unit/cli/test_main.py @@ -215,33 +215,9 @@ def test_main_handle_ini_options(self): self.assertRaisesRegex(SystemExit, "2", bandit.main) self.assertEqual( str(err_mock.call_args[0][0]), - "Unknown test found in profile: some_test", + "No tests would be run, please check the profile.", ) - @mock.patch( - "sys.argv", ["bandit", "-c", "bandit.yaml", "-t", "badID", "test"] - ) - def test_main_unknown_tests(self): - # Test that bandit exits when an invalid test ID is provided - temp_directory = self.useFixture(fixtures.TempDir()).path - os.chdir(temp_directory) - with open("bandit.yaml", "w") as fd: - fd.write(bandit_config_content) - # assert a SystemExit with code 2 - self.assertRaisesRegex(SystemExit, "2", bandit.main) - - @mock.patch( - "sys.argv", ["bandit", "-c", "bandit.yaml", "-s", "badID", "test"] - ) - def test_main_unknown_skip_tests(self): - # Test that bandit exits when an invalid test ID is provided to skip - temp_directory = self.useFixture(fixtures.TempDir()).path - os.chdir(temp_directory) - with open("bandit.yaml", "w") as fd: - fd.write(bandit_config_content) - # assert a SystemExit with code 2 - self.assertRaisesRegex(SystemExit, "2", bandit.main) - @mock.patch( "sys.argv", ["bandit", "-c", "bandit.yaml", "-p", "bad", "test"] )