Move hacking code to a separate fixture
Move the hacking checks to a separate file so we can flake8 test the test_hacking_checks test cases. This change simply isolates the code that would not pass flake8 into a fixture so that the keystone-specific hacking test-case classes can also be run through flake8. Change-Id: Id56cb78f39bd2f1766afe9c67443810008f5b25b
This commit is contained in:
parent
d4c4a9654a
commit
062f3431df
92
keystone/tests/ksfixtures/hacking.py
Normal file
92
keystone/tests/ksfixtures/hacking.py
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
# NOTE(morganfainberg) This file shouldn't have flake8 run on it as it has
|
||||||
|
# code examples that will fail normal CI pep8/flake8 tests. This is expected.
|
||||||
|
# The code has been moved here to ensure that proper tests occur on the
|
||||||
|
# test_hacking_checks test cases.
|
||||||
|
# flake8: noqa
|
||||||
|
|
||||||
|
import fixtures
|
||||||
|
|
||||||
|
|
||||||
|
class HackingCode(fixtures.Fixture):
|
||||||
|
"""A fixture to house the various code examples for the keystone hacking
|
||||||
|
style checks.
|
||||||
|
"""
|
||||||
|
|
||||||
|
mutable_default_args = {
|
||||||
|
'code': """
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
|
||||||
|
def f(a, b='', c=None):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def f(bad=[]):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def f(foo, bad=[], more_bad=[x for x in range(3)]):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def f(foo, bad={}):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def f(foo, bad={}, another_bad=[], fine=None):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def f(bad=[]): # noqa
|
||||||
|
pass
|
||||||
|
|
||||||
|
""",
|
||||||
|
'expected_errors': [
|
||||||
|
(7, 10, 'K001'),
|
||||||
|
(10, 15, 'K001'),
|
||||||
|
(10, 29, 'K001'),
|
||||||
|
(13, 15, 'K001'),
|
||||||
|
(16, 15, 'K001'),
|
||||||
|
(16, 31, 'K001'),
|
||||||
|
]}
|
||||||
|
|
||||||
|
comments_begin_with_space = {
|
||||||
|
'code': """
|
||||||
|
# This is a good comment
|
||||||
|
|
||||||
|
#This is a bad one
|
||||||
|
|
||||||
|
# This is alright and can
|
||||||
|
# be continued with extra indentation
|
||||||
|
# if that's what the developer wants.
|
||||||
|
""",
|
||||||
|
'expected_errors': [
|
||||||
|
(3, 0, 'K002'),
|
||||||
|
]}
|
||||||
|
|
||||||
|
asserting_none_equality = {
|
||||||
|
'code': """
|
||||||
|
class Test(object):
|
||||||
|
|
||||||
|
def test(self):
|
||||||
|
self.assertEqual('', '')
|
||||||
|
self.assertEqual('', None)
|
||||||
|
self.assertEqual(None, '')
|
||||||
|
self.assertNotEqual('', None)
|
||||||
|
self.assertNotEqual(None, '')
|
||||||
|
self.assertNotEqual('', None) # noqa
|
||||||
|
self.assertNotEqual(None, '') # noqa
|
||||||
|
""",
|
||||||
|
'expected_errors': [
|
||||||
|
(5, 8, 'K003'),
|
||||||
|
(6, 8, 'K003'),
|
||||||
|
(7, 8, 'K004'),
|
||||||
|
(8, 8, 'K004'),
|
||||||
|
]}
|
@ -17,10 +17,16 @@ import pep8
|
|||||||
import testtools
|
import testtools
|
||||||
|
|
||||||
from keystone.hacking import checks
|
from keystone.hacking import checks
|
||||||
|
from keystone.tests.ksfixtures import hacking as hacking_fixtures
|
||||||
|
|
||||||
|
|
||||||
class BaseStyleCheck(testtools.TestCase):
|
class BaseStyleCheck(testtools.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(BaseStyleCheck, self).setUp()
|
||||||
|
self.code_ex = self.useFixture(hacking_fixtures.HackingCode())
|
||||||
|
self.addCleanup(delattr, self, 'code_ex')
|
||||||
|
|
||||||
def get_checker(self):
|
def get_checker(self):
|
||||||
"""Returns the checker to be used for tests in this class."""
|
"""Returns the checker to be used for tests in this class."""
|
||||||
raise NotImplemented('subclasses must provide a real implementation')
|
raise NotImplemented('subclasses must provide a real implementation')
|
||||||
@ -50,38 +56,9 @@ class TestCheckForMutableDefaultArgs(BaseStyleCheck):
|
|||||||
return checks.CheckForMutableDefaultArgs
|
return checks.CheckForMutableDefaultArgs
|
||||||
|
|
||||||
def test(self):
|
def test(self):
|
||||||
code = """
|
code = self.code_ex.mutable_default_args['code']
|
||||||
def f():
|
errors = self.code_ex.mutable_default_args['expected_errors']
|
||||||
pass
|
self.assert_has_errors(code, expected_errors=errors)
|
||||||
|
|
||||||
def f(a, b='', c=None):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def f(bad=[]):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def f(foo, bad=[], more_bad=[x for x in range(3)]):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def f(foo, bad={}):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def f(foo, bad={}, another_bad=[], fine=None):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def f(bad=[]): # noqa
|
|
||||||
pass
|
|
||||||
|
|
||||||
"""
|
|
||||||
expected_errors = [
|
|
||||||
(7, 10, 'K001'),
|
|
||||||
(10, 15, 'K001'),
|
|
||||||
(10, 29, 'K001'),
|
|
||||||
(13, 15, 'K001'),
|
|
||||||
(16, 15, 'K001'),
|
|
||||||
(16, 31, 'K001'),
|
|
||||||
]
|
|
||||||
self.assert_has_errors(code, expected_errors=expected_errors)
|
|
||||||
|
|
||||||
|
|
||||||
class TestBlockCommentsBeginWithASpace(BaseStyleCheck):
|
class TestBlockCommentsBeginWithASpace(BaseStyleCheck):
|
||||||
@ -90,20 +67,9 @@ class TestBlockCommentsBeginWithASpace(BaseStyleCheck):
|
|||||||
return checks.block_comments_begin_with_a_space
|
return checks.block_comments_begin_with_a_space
|
||||||
|
|
||||||
def test(self):
|
def test(self):
|
||||||
# NOTE(dstanek): The 'noqa' line below will stop the normal CI
|
code = self.code_ex.comments_begin_with_space['code']
|
||||||
# pep8 process from flaging an error when running against this code.
|
errors = self.code_ex.comments_begin_with_space['expected_errors']
|
||||||
# The unit tests use pep8 directly and the 'noqa' has no effect so we
|
self.assert_has_errors(code, expected_errors=errors)
|
||||||
# can easilty test.
|
|
||||||
code = """
|
|
||||||
# This is a good comment
|
|
||||||
|
|
||||||
#This is a bad one # flake8: noqa
|
|
||||||
|
|
||||||
# This is alright and can
|
|
||||||
# be continued with extra indentation
|
|
||||||
# if that's what the developer wants.
|
|
||||||
"""
|
|
||||||
self.assert_has_errors(code, [(3, 0, 'K002')])
|
|
||||||
|
|
||||||
|
|
||||||
class TestAssertingNoneEquality(BaseStyleCheck):
|
class TestAssertingNoneEquality(BaseStyleCheck):
|
||||||
@ -112,22 +78,6 @@ class TestAssertingNoneEquality(BaseStyleCheck):
|
|||||||
return checks.CheckForAssertingNoneEquality
|
return checks.CheckForAssertingNoneEquality
|
||||||
|
|
||||||
def test(self):
|
def test(self):
|
||||||
code = """
|
code = self.code_ex.asserting_none_equality['code']
|
||||||
class Test(object):
|
errors = self.code_ex.asserting_none_equality['expected_errors']
|
||||||
|
self.assert_has_errors(code, expected_errors=errors)
|
||||||
def test(self):
|
|
||||||
self.assertEqual('', '')
|
|
||||||
self.assertEqual('', None)
|
|
||||||
self.assertEqual(None, '')
|
|
||||||
self.assertNotEqual('', None)
|
|
||||||
self.assertNotEqual(None, '')
|
|
||||||
self.assertNotEqual('', None) # noqa
|
|
||||||
self.assertNotEqual(None, '') # noqa
|
|
||||||
"""
|
|
||||||
expected_errors = [
|
|
||||||
(5, 8, 'K003'),
|
|
||||||
(6, 8, 'K003'),
|
|
||||||
(7, 8, 'K004'),
|
|
||||||
(8, 8, 'K004'),
|
|
||||||
]
|
|
||||||
self.assert_has_errors(code, expected_errors=expected_errors)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user