Remove uses of assert in production code (#32079)

* Remove uses of assert in production code

* Fix assertion

* Add code smell test for assertions, currently limited to lib/ansible

* Fix assertion

* Add docs for no-assert

* Remove new assert from enos

* Fix assert in module_utils.connection
This commit is contained in:
Matt Martz 2017-11-13 10:51:18 -06:00 committed by ansibot
commit 99d4f5bab4
38 changed files with 195 additions and 89 deletions

View file

@ -64,7 +64,7 @@ RETURN = """
import os
import sys
from ansible.module_utils.six.moves.urllib.parse import urlparse
from ansible.errors import AnsibleError
from ansible.errors import AnsibleError, AnsibleAssertionError
from ansible.plugins.lookup import LookupBase
try:
@ -131,7 +131,8 @@ class LookupModule(LookupBase):
for param in params[1:]:
if param and len(param) > 0:
name, value = param.split('=')
assert name in paramvals, "%s not a valid consul lookup parameter" % name
if name not in paramvals:
raise AnsibleAssertionError("%s not a valid consul lookup parameter" % name)
paramvals[name] = value
except (ValueError, AssertionError) as e:
raise AnsibleError(e)

View file

@ -51,7 +51,7 @@ import codecs
import csv
from collections import MutableSequence
from ansible.errors import AnsibleError
from ansible.errors import AnsibleError, AnsibleAssertionError
from ansible.plugins.lookup import LookupBase
from ansible.module_utils._text import to_bytes, to_native, to_text
@ -124,7 +124,8 @@ class LookupModule(LookupBase):
try:
for param in params[1:]:
name, value = param.split('=')
assert(name in paramvals)
if name not in paramvals:
raise AnsibleAssertionError('%s not in paramvals' % name)
paramvals[name] = value
except (ValueError, AssertionError) as e:
raise AnsibleError(e)

View file

@ -65,7 +65,7 @@ import re
from collections import MutableSequence
from io import StringIO
from ansible.errors import AnsibleError
from ansible.errors import AnsibleError, AnsibleAssertionError
from ansible.module_utils.six.moves import configparser
from ansible.module_utils._text import to_bytes, to_text
from ansible.plugins.lookup import LookupBase
@ -129,7 +129,8 @@ class LookupModule(LookupBase):
try:
for param in params[1:]:
name, value = param.split('=')
assert(name in paramvals)
if name not in paramvals:
raise AnsibleAssertionError('%s not in paramvals' % name)
paramvals[name] = value
except (ValueError, AssertionError) as e:
raise AnsibleError(e)

View file

@ -92,7 +92,7 @@ _raw:
import os
import string
from ansible.errors import AnsibleError
from ansible.errors import AnsibleError, AnsibleAssertionError
from ansible.module_utils._text import to_bytes, to_native, to_text
from ansible.parsing.splitter import parse_kv
from ansible.plugins.lookup import LookupBase
@ -250,7 +250,8 @@ def _format_content(password, salt, encrypt=True):
return password
# At this point, the calling code should have assured us that there is a salt value.
assert salt, '_format_content was called with encryption requested but no salt value'
if not salt:
raise AnsibleAssertionError('_format_content was called with encryption requested but no salt value')
return u'%s salt=%s' % (password, salt)

View file

@ -82,7 +82,7 @@ import subprocess
import time
from distutils import util
from ansible.errors import AnsibleError
from ansible.errors import AnsibleError, AnsibleAssertionError
from ansible.module_utils._text import to_bytes, to_native, to_text
from ansible.utils.encrypt import random_password
from ansible.plugins.lookup import LookupBase
@ -138,7 +138,8 @@ class LookupModule(LookupBase):
try:
for param in params[1:]:
name, value = param.split('=')
assert(name in self.paramvals)
if name not in self.paramvals:
raise AnsibleAssertionError('%s not in paramvals' % name)
self.paramvals[name] = value
except (ValueError, AssertionError) as e:
raise AnsibleError(e)

View file

@ -33,7 +33,7 @@ _list:
"""
import shelve
from ansible.errors import AnsibleError
from ansible.errors import AnsibleError, AnsibleAssertionError
from ansible.plugins.lookup import LookupBase
from ansible.module_utils._text import to_bytes, to_text
@ -63,7 +63,8 @@ class LookupModule(LookupBase):
try:
for param in params:
name, value = param.split('=')
assert(name in paramvals)
if name not in paramvals:
raise AnsibleAssertionError('%s not in paramvals' % name)
paramvals[name] = value
except (ValueError, AssertionError) as e: