Skip to content

Commit

Permalink
Fix running when empty config file
Browse files Browse the repository at this point in the history
Bandit would error with a traceback when the config file was empty.
Bandit doesn't have a problem with a non-empty config file that
contains a bunch of invalid keys, so it could just work with an
empty config file.

Change-Id: I7fd912076aa89c15b6b9400d3a784c8afdf3278d
Closes-Bug: 1492068
  • Loading branch information
Brant Knudson committed Sep 12, 2015
1 parent 7deb645 commit b157a39
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bandit/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_option(self, option_string):
option_levels = option_string.split('.')
cur_item = self._config
for level in option_levels:
if level in cur_item:
if cur_item and (level in cur_item):
cur_item = cur_item[level]
else:
return None
Expand Down
18 changes: 8 additions & 10 deletions tests/unit/core/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,14 @@
class TempFile(fixtures.Fixture):
def __init__(self, contents=None):
super(TempFile, self).__init__()

if not contents:
self.example_key = uuid.uuid4().hex
self.example_value = uuid.uuid4().hex
contents = '%s: %s' % (self.example_key, self.example_value)

self.contents = contents

def setUp(self):
super(TempFile, self).setUp()

with tempfile.NamedTemporaryFile(mode='wt', delete=False) as f:
f.write(self.contents)
if self.contents:
f.write(self.contents)

self.addCleanup(os.unlink, f.name)

Expand All @@ -52,7 +47,10 @@ class TestInit(testtools.TestCase):
def test_settings(self):
# Can initialize a BanditConfig.

f = self.useFixture(TempFile())
example_key = uuid.uuid4().hex
example_value = self.getUniqueString()
contents = '%s: %s' % (example_key, example_value)
f = self.useFixture(TempFile(contents))
b_config = config.BanditConfig(f.name)

# After initialization, can get settings.
Expand All @@ -64,8 +62,8 @@ def test_settings(self):
self.assertEqual('', b_config.get_setting('color_HIGH'))
self.assertEqual('*.py', b_config.get_setting('plugin_name_pattern'))

self.assertEqual({f.example_key: f.example_value}, b_config.config)
self.assertEqual(f.example_value, b_config.get_option(f.example_key))
self.assertEqual({example_key: example_value}, b_config.config)
self.assertEqual(example_value, b_config.get_option(example_key))

def test_file_does_not_exist(self):
# When the config file doesn't exist, ConfigFileUnopenable is raised.
Expand Down

0 comments on commit b157a39

Please sign in to comment.