Merge "Use new checks in hacking 0.12"
This commit is contained in:
commit
d8a75430c0
@ -21,4 +21,3 @@ Neutron Library Specific Commandments
|
|||||||
- [N533] Validate that debug level logs are not translated
|
- [N533] Validate that debug level logs are not translated
|
||||||
- [N534] Exception messages should be translated
|
- [N534] Exception messages should be translated
|
||||||
- [N535] Usage of Python eventlet module not allowed
|
- [N535] Usage of Python eventlet module not allowed
|
||||||
- [N536] String interpolation should be delayed at logging calls.
|
|
||||||
|
@ -41,6 +41,19 @@ To adopt neutron-lib's hacking checks:
|
|||||||
[hacking]
|
[hacking]
|
||||||
local-check-factory = myproject.mypkg.my_factory
|
local-check-factory = myproject.mypkg.my_factory
|
||||||
|
|
||||||
|
#. Update your project's ``tox.ini`` enable any flake8 extensions neutron-lib's
|
||||||
|
``tox.ini`` does. These are hacking checks otherwise disabled by default
|
||||||
|
that neutron-lib expects to run.
|
||||||
|
|
||||||
|
For example in neutron-lib's ``tox.ini``::
|
||||||
|
|
||||||
|
[flake8]
|
||||||
|
# H904: Delay string interpolations at logging calls
|
||||||
|
enable-extensions=H904
|
||||||
|
|
||||||
|
In the example above, adopters should also add ``H904`` to the
|
||||||
|
``enable-extensions`` in their ``tox.ini``.
|
||||||
|
|
||||||
#. Actively adopt neutron-lib hacking checks that are incubating and will
|
#. Actively adopt neutron-lib hacking checks that are incubating and will
|
||||||
soon become adopter checks by manually running the checks on your project.
|
soon become adopter checks by manually running the checks on your project.
|
||||||
This can be done by modifying your ``tox.ini`` to use the
|
This can be done by modifying your ``tox.ini`` to use the
|
||||||
|
@ -255,7 +255,6 @@ def incubating_factory(register):
|
|||||||
:param register: The function to register the check functions with.
|
:param register: The function to register the check functions with.
|
||||||
:returns: None.
|
:returns: None.
|
||||||
"""
|
"""
|
||||||
register(translation_checks.check_delayed_string_interpolation)
|
|
||||||
|
|
||||||
|
|
||||||
def _neutron_lib_factory(register):
|
def _neutron_lib_factory(register):
|
||||||
|
@ -41,8 +41,6 @@ def _regex_for_level(level, hint):
|
|||||||
_log_translation_hint = re.compile(
|
_log_translation_hint = re.compile(
|
||||||
'|'.join('(?:%s)' % _regex_for_level(level, hint)
|
'|'.join('(?:%s)' % _regex_for_level(level, hint)
|
||||||
for level, hint in _all_log_levels.items()))
|
for level, hint in _all_log_levels.items()))
|
||||||
_log_string_interpolation = re.compile(
|
|
||||||
r".*LOG\.(error|warning|info|critical|exception|debug)\([^,]*%[^,]*[,)]")
|
|
||||||
|
|
||||||
|
|
||||||
def _translation_is_not_expected(filename):
|
def _translation_is_not_expected(filename):
|
||||||
@ -127,31 +125,3 @@ def check_raised_localized_exceptions(logical_line, filename):
|
|||||||
if exception_msg.startswith("\"") or exception_msg.startswith("\'"):
|
if exception_msg.startswith("\"") or exception_msg.startswith("\'"):
|
||||||
msg = "N534: Untranslated exception message."
|
msg = "N534: Untranslated exception message."
|
||||||
yield (logical_line.index(exception_msg), msg)
|
yield (logical_line.index(exception_msg), msg)
|
||||||
|
|
||||||
|
|
||||||
def check_delayed_string_interpolation(logical_line, filename, noqa):
|
|
||||||
"""N536 - String interpolation should be delayed at logging calls.
|
|
||||||
|
|
||||||
N536: LOG.debug('Example: %s' % 'bad')
|
|
||||||
Okay: LOG.debug('Example: %s', 'good')
|
|
||||||
|
|
||||||
:param logical_line: The logical line to check.
|
|
||||||
:param filename: The file name where the logical line exists.
|
|
||||||
:param noqa: Noqa indicator.
|
|
||||||
:returns: None if the logical line passes the check, otherwise a tuple
|
|
||||||
is yielded that contains the offending index in logical line and a
|
|
||||||
message describe the check validation failure.
|
|
||||||
"""
|
|
||||||
msg = ("N536 String interpolation should be delayed to be "
|
|
||||||
"handled by the logging code, rather than being done "
|
|
||||||
"at the point of the logging call. "
|
|
||||||
"Use ',' instead of '%'.")
|
|
||||||
|
|
||||||
if noqa:
|
|
||||||
return
|
|
||||||
|
|
||||||
if '/tests/' in filename:
|
|
||||||
return
|
|
||||||
|
|
||||||
if _log_string_interpolation.match(logical_line):
|
|
||||||
yield(logical_line.index('%'), msg)
|
|
||||||
|
@ -10,8 +10,6 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import re
|
|
||||||
|
|
||||||
import testtools
|
import testtools
|
||||||
|
|
||||||
from neutron_lib.hacking import checks
|
from neutron_lib.hacking import checks
|
||||||
@ -213,38 +211,6 @@ class HackingTestCase(base.BaseTestCase):
|
|||||||
self.assertLinePasses(f, "raise KeyError('Error text')",
|
self.assertLinePasses(f, "raise KeyError('Error text')",
|
||||||
'neutron_lib/tests/unit/mytest.py')
|
'neutron_lib/tests/unit/mytest.py')
|
||||||
|
|
||||||
def test_check_delayed_string_interpolation(self):
|
|
||||||
dummy_noqa = re.search('a', 'a')
|
|
||||||
f = tc.check_delayed_string_interpolation
|
|
||||||
|
|
||||||
# In 'logical_line', Contents of strings replaced with
|
|
||||||
# "xxx" of same length.
|
|
||||||
self.assertLineFails(f, 'LOG.error(_LE("xxxxxxxxxxxxxxx") % value)',
|
|
||||||
'neutron_lib/db/utils.py', None)
|
|
||||||
self.assertLineFails(f, "LOG.warning(msg % 'xxxxx')",
|
|
||||||
'neutron_lib/db/utils.py', None)
|
|
||||||
self.assertLineFails(f, "LOG.info(_LI('xxxxxxxxxxxxxxxxxxxxxxxxx"
|
|
||||||
"xxxxxxxxx') % {'xx': v1, 'xx': v2})",
|
|
||||||
'neutron_lib/db/utils.py', None)
|
|
||||||
|
|
||||||
self.assertLinePasses(
|
|
||||||
f, 'LOG.error(_LE("xxxxxxxxxxxxxxxxxx"), value)',
|
|
||||||
'neutron_lib/db/utils.py', None)
|
|
||||||
self.assertLinePasses(f, "LOG.warning(msg, 'xxxxx')",
|
|
||||||
'neutron_lib/db/utils.py', None)
|
|
||||||
self.assertLinePasses(f, "LOG.info(_LI('xxxxxxxxxxxxxxxxxxxxxxxx"
|
|
||||||
"xxxxxxxxx'), {'xx': v1, 'xx': v2})",
|
|
||||||
'neutron_lib/db/utils.py', None)
|
|
||||||
|
|
||||||
# check a file in neutron_lib/tests
|
|
||||||
self.assertLinePasses(f, 'LOG.error(_LE("xxxxxxxxxxxxxxx") % value)',
|
|
||||||
'neutron_lib/tests/unit/test_neutron_lib.py',
|
|
||||||
None)
|
|
||||||
# check code including 'noqa'
|
|
||||||
self.assertLinePasses(f, 'LOG.error(_LE("xxxxxxxxxxxxxxx") % value)',
|
|
||||||
'neutron_lib/db/utils.py',
|
|
||||||
dummy_noqa)
|
|
||||||
|
|
||||||
def test_check_eventlet_imports(self):
|
def test_check_eventlet_imports(self):
|
||||||
f = checks.check_no_eventlet_imports
|
f = checks.check_no_eventlet_imports
|
||||||
self.assertLineFails(f, "import eventlet")
|
self.assertLineFails(f, "import eventlet")
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
other:
|
||||||
|
- OpenStack dev hacking check ``H904`` is now enabled in ``tox.ini``
|
||||||
|
via the ``enable-extensions`` configuration property. Neutron-lib
|
||||||
|
adopters should also enable this hacking check in their ``tox.ini``.
|
@ -2,7 +2,7 @@
|
|||||||
# of appearance. Changing the order has an impact on the overall integration
|
# of appearance. Changing the order has an impact on the overall integration
|
||||||
# process, which may cause wedges in the gate later.
|
# process, which may cause wedges in the gate later.
|
||||||
|
|
||||||
hacking<0.12,>=0.11.0 # Apache-2.0
|
hacking>=0.12.0,!=0.13.0,<0.14 # Apache-2.0
|
||||||
|
|
||||||
coverage>=4.0 # Apache-2.0
|
coverage>=4.0 # Apache-2.0
|
||||||
discover # BSD
|
discover # BSD
|
||||||
|
2
tox.ini
2
tox.ini
@ -48,6 +48,8 @@ commands =
|
|||||||
{toxinidir}/tools/api_report.sh
|
{toxinidir}/tools/api_report.sh
|
||||||
|
|
||||||
[flake8]
|
[flake8]
|
||||||
|
# H904: Delay string interpolations at logging calls
|
||||||
|
enable-extensions=H904
|
||||||
show-source = True
|
show-source = True
|
||||||
exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build,tools
|
exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build,tools
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user