Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maresmar committed Jan 13, 2021
1 parent e1e5ff2 commit 9ca63bf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
4 changes: 3 additions & 1 deletion bandit/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
39 changes: 26 additions & 13 deletions tests/unit/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/core/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import tempfile

import testtools

from bandit.core import utils as b_utils


Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 9ca63bf

Please sign in to comment.