From 9ca63bfc0528232ed28fa9915a379c023854a704 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Mare=C5=A1?= Date: Wed, 13 Jan 2021 14:44:13 +0100 Subject: [PATCH] Fix tests --- bandit/core/utils.py | 4 +++- tests/unit/cli/test_main.py | 39 ++++++++++++++++++++++++------------ tests/unit/core/test_util.py | 3 +-- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/bandit/core/utils.py b/bandit/core/utils.py index 7a0a3f275..531a9771e 100644 --- a/bandit/core/utils.py +++ b/bandit/core/utils.py @@ -95,6 +95,7 @@ class InvalidModulePath(Exception): class ConfigError(Exception): """Raised when the config file fails validation.""" + def __init__(self, message, config_file): self.config_file = config_file self.message = "{0} : {1}".format(config_file, message) @@ -103,6 +104,7 @@ def __init__(self, message, config_file): class ProfileNotFound(Exception): """Raised when chosen profile cannot be found.""" + def __init__(self, config_file, profile): self.config_file = config_file self.profile = profile @@ -312,7 +314,7 @@ def parse_ini_file(f_loc): LOG.warning("Unable to parse config file %s or missing [bandit] " "section", f_loc) - return None + return {} def check_ast_node(name): diff --git a/tests/unit/cli/test_main.py b/tests/unit/cli/test_main.py index 815e3c118..2a099ac28 100644 --- a/tests/unit/cli/test_main.py +++ b/tests/unit/cli/test_main.py @@ -8,7 +8,6 @@ import fixtures import mock import testtools - from bandit.cli import main as bandit from bandit.core import extension_loader as ext_loader from bandit.core import utils @@ -89,20 +88,20 @@ def tearDown(self): def test_get_options_from_ini_no_ini_path_no_target(self): # Test that no config options are loaded when no ini path or target # directory are provided - self.assertIsNone(bandit._get_options_from_ini(None, [])) + self.assertEqual({}, bandit._get_options_from_ini(None, [])) def test_get_options_from_ini_empty_directory_no_target(self): # Test that no config options are loaded when an empty directory is # provided as the ini path and no target directory is provided ini_directory = self.useFixture(fixtures.TempDir()).path - self.assertIsNone(bandit._get_options_from_ini(ini_directory, [])) + self.assertEqual({}, bandit._get_options_from_ini(ini_directory, [])) def test_get_options_from_ini_no_ini_path_no_bandit_files(self): # Test that no config options are loaded when no ini path is provided # and the target directory contains no bandit config files (.bandit) target_directory = self.useFixture(fixtures.TempDir()).path - self.assertIsNone(bandit._get_options_from_ini(None, - [target_directory])) + self.assertEqual({}, bandit._get_options_from_ini( + None, [target_directory])) def test_get_options_from_ini_no_ini_path_multi_bandit_files(self): # Test that bandit exits when no ini path is provided and the target @@ -124,27 +123,41 @@ def test_init_extensions(self): # Test that an extension loader manager is returned self.assertEqual(ext_loader.MANAGER, bandit._init_extensions()) - def test_log_option_source_arg_val(self): + def test_decide_option_source_arg_val(self): # Test that the command argument value is returned when provided arg_val = 'file' ini_val = 'vuln' option_name = 'aggregate' - self.assertEqual(arg_val, bandit._log_option_source(arg_val, ini_val, - option_name)) + self.assertEqual(arg_val, bandit._decide_option_source(arg_val, + ini_val, + None, + option_name)) - def test_log_option_source_ini_value(self): + def test_decide_option_source_ini_value(self): # Test that the ini value is returned when no command argument is # provided ini_val = 'vuln' option_name = 'aggregate' - self.assertEqual(ini_val, bandit._log_option_source(None, ini_val, - option_name)) + self.assertEqual(ini_val, bandit._decide_option_source(None, ini_val, + None, + option_name)) + + def test_decide_option_source_default_value(self): + # Test that the ini value is returned when no command argument is + # provided + default_val = 'vuln' + option_name = 'aggregate' + self.assertEqual(default_val, + bandit._decide_option_source(None, None, + default_val, + option_name)) - def test_log_option_source_no_values(self): + def test_decide_option_source_no_values(self): # Test that None is returned when no command argument or ini value are # provided option_name = 'aggregate' - self.assertIsNone(bandit._log_option_source(None, None, option_name)) + self.assertIsNone(bandit._decide_option_source( + None, None, None, option_name)) @mock.patch('sys.argv', ['bandit', '-c', 'bandit.yaml', 'test']) def test_main_config_unopenable(self): diff --git a/tests/unit/core/test_util.py b/tests/unit/core/test_util.py index 13c31f6a6..4d0c716a0 100644 --- a/tests/unit/core/test_util.py +++ b/tests/unit/core/test_util.py @@ -12,7 +12,6 @@ import tempfile import testtools - from bandit.core import utils as b_utils @@ -272,7 +271,7 @@ def test_parse_ini_file(self): 'expected': {'exclude': '/abc,/def'}}, {'content': '[Blabla]\nsomething=something', - 'expected': None}] + 'expected': {}}] with tempfile.NamedTemporaryFile('r+') as t: for test in tests: