Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skip configuration to assert_used #633

Merged
merged 3 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions bandit/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,7 @@ def is_module_imported_like(self, module):
if module in imp:
return True
return False

@property
def filename(self):
return self._context.get('filename')
23 changes: 22 additions & 1 deletion bandit/plugins/asserts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement for
more info on ``assert``

**Config Options:**

You can configure files that skip this check. This is often useful when you
use assert statements in test cases.

.. code-block:: yaml

assert_used:
skips: ['*_test.py', 'test_*.py']

:Example:

.. code-block:: none
Expand All @@ -39,14 +49,25 @@
.. versionadded:: 0.11.0

"""
import fnmatch

import bandit
wilbertom marked this conversation as resolved.
Show resolved Hide resolved
from bandit.core import test_properties as test


def gen_config(name):
if name == 'assert_used':
return {'skips': []}


@test.takes_config
@test.test_id('B101')
@test.checks('Assert')
def assert_used(context):
def assert_used(context, config):
for skip in config.get('skips', []):
if fnmatch.fnmatch(context.filename, skip):
return None

return bandit.Issue(
severity=bandit.LOW,
confidence=bandit.HIGH,
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,24 @@ def test_httpoxy(self):

def test_asserts(self):
'''Test catching the use of assert.'''
test = next((x for x in self.b_mgr.b_ts.tests['Assert']
if x.__name__ == 'assert_used'))

test._config = {'skips': []}
expect = {
'SEVERITY': {'UNDEFINED': 0, 'LOW': 1, 'MEDIUM': 0, 'HIGH': 0},
'CONFIDENCE': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 0, 'HIGH': 1}
}
self.check_example('assert.py', expect)

test._config = {'skips': ['*assert.py']}
expect = {
'SEVERITY': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 0, 'HIGH': 0},
'CONFIDENCE': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 0, 'HIGH': 0}
}
self.check_example('assert.py', expect)

test._config = {}
expect = {
'SEVERITY': {'UNDEFINED': 0, 'LOW': 1, 'MEDIUM': 0, 'HIGH': 0},
'CONFIDENCE': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 0, 'HIGH': 1}
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/core/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,12 @@ def test_is_module_imported_like(self):

new_context = context.Context()
self.assertFalse(new_context.is_module_imported_like('spam'))

def test_filename(self):
ref_context = dict(filename='spam.py')
new_context = context.Context(context_object=ref_context)

self.assertEqual(new_context.filename, 'spam.py')

new_context = context.Context()
self.assertIsNone(new_context.filename)