Display nice error when profile is not found

Change-Id: I72afda9bf28e7565b9e702655a6f90e52bc227f8
Closes-bug: 1505673
This commit is contained in:
Stanisław Pitucha
2016-01-04 11:47:44 +11:00
parent 32c5921185
commit bafadf471a
4 changed files with 24 additions and 9 deletions

View File

@@ -225,10 +225,14 @@ def main():
log_format = b_conf.get_option('log_format')
_init_logger(debug, log_format=log_format)
b_mgr = b_manager.BanditManager(b_conf, args.agg_type, args.debug,
profile_name=args.profile,
verbose=args.verbose,
ignore_nosec=args.ignore_nosec)
try:
b_mgr = b_manager.BanditManager(b_conf, args.agg_type, args.debug,
profile_name=args.profile,
verbose=args.verbose,
ignore_nosec=args.ignore_nosec)
except utils.ProfileNotFound as e:
logger.error(e)
sys.exit(2)
if args.baseline is not None:
try:

View File

@@ -28,6 +28,7 @@ from bandit.core import meta_ast as b_meta_ast
from bandit.core import metrics
from bandit.core import node_visitor as b_node_visitor
from bandit.core import test_set as b_test_set
from bandit.core import utils
logger = logging.getLogger(__name__)
@@ -72,9 +73,8 @@ class BanditManager():
profile_name, profile
)
else:
raise RuntimeError('unable to find profile (%s) in config '
'file: %s' % (profile_name,
self.b_conf.config_file))
raise utils.ProfileNotFound(self.b_conf.config_file,
profile_name)
else:
profile = None

View File

@@ -140,6 +140,16 @@ class ConfigFileInvalidYaml(Exception):
super(ConfigFileInvalidYaml, self).__init__(message)
class ProfileNotFound(Exception):
"""Raised when chosen profile cannot be found."""
def __init__(self, config_file, profile):
self.config_file = config_file
self.profile = profile
message = 'Unable to find profile (%s) in config file: %s' % (
self.profile, self.config_file)
super(ProfileNotFound, self).__init__(message)
def warnings_formatter(message,
category=UserWarning,
filename='',

View File

@@ -26,6 +26,7 @@ from bandit.core import config
from bandit.core import issue
from bandit.core import constants
from bandit.core import extension_loader
from bandit.core import utils
class TempFile(fixtures.Fixture):
@@ -95,11 +96,11 @@ class ManagerTests(testtools.TestCase):
m = manager.BanditManager(config=self.config, agg_type='file',
debug=False, verbose=False,
profile_name='Bad')
except RuntimeError as e:
except utils.ProfileNotFound as e:
err = str(e)
self.assertTrue(err.startswith(
"unable to find profile (Bad) in config file:"))
"Unable to find profile (Bad) in config file:"))
def test_matches_globlist(self):
self.assertTrue(manager._matches_glob_list('test', ['*tes*']))