From 062f3431dfc56afa0e61752be47e17b31496a335 Mon Sep 17 00:00:00 2001 From: Morgan Fainberg Date: Fri, 11 Apr 2014 12:05:55 -0700 Subject: [PATCH] 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 --- keystone/tests/ksfixtures/hacking.py | 92 +++++++++++++++++++++++++++ keystone/tests/test_hacking_checks.py | 80 +++++------------------ 2 files changed, 107 insertions(+), 65 deletions(-) create mode 100644 keystone/tests/ksfixtures/hacking.py diff --git a/keystone/tests/ksfixtures/hacking.py b/keystone/tests/ksfixtures/hacking.py new file mode 100644 index 000000000..738f1a3d0 --- /dev/null +++ b/keystone/tests/ksfixtures/hacking.py @@ -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'), + ]} diff --git a/keystone/tests/test_hacking_checks.py b/keystone/tests/test_hacking_checks.py index a94108dc7..57dd3af67 100644 --- a/keystone/tests/test_hacking_checks.py +++ b/keystone/tests/test_hacking_checks.py @@ -17,10 +17,16 @@ import pep8 import testtools from keystone.hacking import checks +from keystone.tests.ksfixtures import hacking as hacking_fixtures 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): """Returns the checker to be used for tests in this class.""" raise NotImplemented('subclasses must provide a real implementation') @@ -50,38 +56,9 @@ class TestCheckForMutableDefaultArgs(BaseStyleCheck): return checks.CheckForMutableDefaultArgs def test(self): - 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'), - ] - self.assert_has_errors(code, expected_errors=expected_errors) + code = self.code_ex.mutable_default_args['code'] + errors = self.code_ex.mutable_default_args['expected_errors'] + self.assert_has_errors(code, expected_errors=errors) class TestBlockCommentsBeginWithASpace(BaseStyleCheck): @@ -90,20 +67,9 @@ class TestBlockCommentsBeginWithASpace(BaseStyleCheck): return checks.block_comments_begin_with_a_space def test(self): - # NOTE(dstanek): The 'noqa' line below will stop the normal CI - # pep8 process from flaging an error when running against this code. - # The unit tests use pep8 directly and the 'noqa' has no effect so we - # 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')]) + code = self.code_ex.comments_begin_with_space['code'] + errors = self.code_ex.comments_begin_with_space['expected_errors'] + self.assert_has_errors(code, expected_errors=errors) class TestAssertingNoneEquality(BaseStyleCheck): @@ -112,22 +78,6 @@ class TestAssertingNoneEquality(BaseStyleCheck): return checks.CheckForAssertingNoneEquality def test(self): - 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'), - ] - self.assert_has_errors(code, expected_errors=expected_errors) + code = self.code_ex.asserting_none_equality['code'] + errors = self.code_ex.asserting_none_equality['expected_errors'] + self.assert_has_errors(code, expected_errors=errors)