Add hacking check to ensure not use xrange()
Added hacking check to ensure not to use xrange. Change-Id: I28731e16cf0636f004bf96795c85eecbdf2f8fbd Closes-Bug: #1538118
This commit is contained in:
parent
c171aa12b6
commit
4205f39a00
|
@ -22,3 +22,4 @@ Magnum Specific Commandments
|
||||||
- [M336] Must use a dict comprehension instead of a dict constructor
|
- [M336] Must use a dict comprehension instead of a dict constructor
|
||||||
with a sequence of key-value pairs.
|
with a sequence of key-value pairs.
|
||||||
- [M338] Use assertIn/NotIn(A, B) rather than assertEqual(A in B, True/False).
|
- [M338] Use assertIn/NotIn(A, B) rather than assertEqual(A in B, True/False).
|
||||||
|
- [M339] Don't use xrange()
|
||||||
|
|
|
@ -49,6 +49,8 @@ assert_true_isinstance_re = re.compile(
|
||||||
r"(.)*assertTrue\(isinstance\((\w|\.|\'|\"|\[|\])+, "
|
r"(.)*assertTrue\(isinstance\((\w|\.|\'|\"|\[|\])+, "
|
||||||
"(\w|\.|\'|\"|\[|\])+\)\)")
|
"(\w|\.|\'|\"|\[|\])+\)\)")
|
||||||
dict_constructor_with_list_copy_re = re.compile(r".*\bdict\((\[)?(\(|\[)")
|
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):
|
def assert_equal_none(logical_line):
|
||||||
|
@ -112,6 +114,15 @@ def assert_equal_in(logical_line):
|
||||||
"contents.")
|
"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):
|
def use_timeutils_utcnow(logical_line, filename):
|
||||||
# tools are OK to use the standard datetime module
|
# tools are OK to use the standard datetime module
|
||||||
if "/tools/" in filename:
|
if "/tools/" in filename:
|
||||||
|
@ -142,3 +153,4 @@ def factory(register):
|
||||||
register(assert_equal_in)
|
register(assert_equal_in)
|
||||||
register(use_timeutils_utcnow)
|
register(use_timeutils_utcnow)
|
||||||
register(dict_constructor_with_list_copy)
|
register(dict_constructor_with_list_copy)
|
||||||
|
register(no_xrange)
|
||||||
|
|
|
@ -179,6 +179,16 @@ class HackingTestCase(base.TestCase):
|
||||||
code = "self.assertTrue()"
|
code = "self.assertTrue()"
|
||||||
self._assert_has_no_errors(code, check)
|
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):
|
def test_use_timeunitls_utcow(self):
|
||||||
errors = [(1, 0, "M310")]
|
errors = [(1, 0, "M310")]
|
||||||
check = checks.use_timeutils_utcnow
|
check = checks.use_timeutils_utcnow
|
||||||
|
|
Loading…
Reference in New Issue