Skip to content

Commit

Permalink
Warn not error on an nonexistant test given (#1230)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ericwb authored Feb 5, 2025
1 parent 5e3e694 commit affd4fd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 27 deletions.
7 changes: 5 additions & 2 deletions bandit/core/extension_loader.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
26 changes: 1 addition & 25 deletions tests/unit/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
)
Expand Down

0 comments on commit affd4fd

Please sign in to comment.