Skip to content

Commit affd4fd

Browse files
authored
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 <[email protected]>
1 parent 5e3e694 commit affd4fd

File tree

2 files changed

+6
-27
lines changed

2 files changed

+6
-27
lines changed

bandit/core/extension_loader.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#
22
# SPDX-License-Identifier: Apache-2.0
3+
import logging
34
import sys
45

56
from stevedore import extension
67

78
from bandit.core import utils
89

10+
LOG = logging.getLogger(__name__)
11+
912

1013
class Manager:
1114
# These IDs are for bandit built in tests
@@ -84,11 +87,11 @@ def validate_profile(self, profile):
8487
"""Validate that everything in the configured profiles looks good."""
8588
for inc in profile["include"]:
8689
if not self.check_id(inc):
87-
raise ValueError(f"Unknown test found in profile: {inc}")
90+
LOG.warning(f"Unknown test found in profile: {inc}")
8891

8992
for exc in profile["exclude"]:
9093
if not self.check_id(exc):
91-
raise ValueError(f"Unknown test found in profile: {exc}")
94+
LOG.warning(f"Unknown test found in profile: {exc}")
9295

9396
union = set(profile["include"]) & set(profile["exclude"])
9497
if len(union) > 0:

tests/unit/cli/test_main.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -215,33 +215,9 @@ def test_main_handle_ini_options(self):
215215
self.assertRaisesRegex(SystemExit, "2", bandit.main)
216216
self.assertEqual(
217217
str(err_mock.call_args[0][0]),
218-
"Unknown test found in profile: some_test",
218+
"No tests would be run, please check the profile.",
219219
)
220220

221-
@mock.patch(
222-
"sys.argv", ["bandit", "-c", "bandit.yaml", "-t", "badID", "test"]
223-
)
224-
def test_main_unknown_tests(self):
225-
# Test that bandit exits when an invalid test ID is provided
226-
temp_directory = self.useFixture(fixtures.TempDir()).path
227-
os.chdir(temp_directory)
228-
with open("bandit.yaml", "w") as fd:
229-
fd.write(bandit_config_content)
230-
# assert a SystemExit with code 2
231-
self.assertRaisesRegex(SystemExit, "2", bandit.main)
232-
233-
@mock.patch(
234-
"sys.argv", ["bandit", "-c", "bandit.yaml", "-s", "badID", "test"]
235-
)
236-
def test_main_unknown_skip_tests(self):
237-
# Test that bandit exits when an invalid test ID is provided to skip
238-
temp_directory = self.useFixture(fixtures.TempDir()).path
239-
os.chdir(temp_directory)
240-
with open("bandit.yaml", "w") as fd:
241-
fd.write(bandit_config_content)
242-
# assert a SystemExit with code 2
243-
self.assertRaisesRegex(SystemExit, "2", bandit.main)
244-
245221
@mock.patch(
246222
"sys.argv", ["bandit", "-c", "bandit.yaml", "-p", "bad", "test"]
247223
)

0 commit comments

Comments
 (0)