Browse Source

Add hacking check to ensure not use xrange()

Added hacking check to ensure not to use xrange.

Change-Id: I28731e16cf0636f004bf96795c85eecbdf2f8fbd
Closes-Bug: #1538118
changes/42/292042/7
Deeksha 6 years ago
parent
commit
4205f39a00
  1. 1
      HACKING.rst
  2. 12
      magnum/hacking/checks.py
  3. 10
      magnum/tests/unit/test_hacking.py

1
HACKING.rst

@ -22,3 +22,4 @@ Magnum Specific Commandments
- [M336] Must use a dict comprehension instead of a dict constructor
with a sequence of key-value pairs.
- [M338] Use assertIn/NotIn(A, B) rather than assertEqual(A in B, True/False).
- [M339] Don't use xrange()

12
magnum/hacking/checks.py

@ -49,6 +49,8 @@ assert_true_isinstance_re = re.compile(
r"(.)*assertTrue\(isinstance\((\w|\.|\'|\"|\[|\])+, "
"(\w|\.|\'|\"|\[|\])+\)\)")
dict_constructor_with_list_copy_re = re.compile(r".*\bdict\((\[)?(\(|\[)")
assert_xrange_re = re.compile(
r"\s*xrange\s*\(")
def assert_equal_none(logical_line):
@ -112,6 +114,15 @@ def assert_equal_in(logical_line):
"contents.")
def no_xrange(logical_line):
"""Disallow 'xrange()'
M339
"""
if assert_xrange_re.match(logical_line):
yield(0, "M339: Do not use xrange().")
def use_timeutils_utcnow(logical_line, filename):
# tools are OK to use the standard datetime module
if "/tools/" in filename:
@ -142,3 +153,4 @@ def factory(register):
register(assert_equal_in)
register(use_timeutils_utcnow)
register(dict_constructor_with_list_copy)
register(no_xrange)

10
magnum/tests/unit/test_hacking.py

@ -179,6 +179,16 @@ class HackingTestCase(base.TestCase):
code = "self.assertTrue()"
self._assert_has_no_errors(code, check)
def test_no_xrange(self):
errors = [(1, 0, "M339")]
check = checks.no_xrange
code = "xrange(45)"
self._assert_has_errors(code, check, errors)
code = "range(45)"
self._assert_has_no_errors(code, check)
def test_use_timeunitls_utcow(self):
errors = [(1, 0, "M310")]
check = checks.use_timeutils_utcnow

Loading…
Cancel
Save