"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "cloudkitty/tests/test_hacking.py" between
cloudkitty-12.1.0.tar.gz and cloudkitty-13.0.0.tar.gz

About: OpenStack CloudKitty provides Rating-as-a-Service for OpenStack.
The "Victoria" series (latest release).

test_hacking.py  (cloudkitty-12.1.0):test_hacking.py  (cloudkitty-13.0.0)
skipping to change at line 15 skipping to change at line 15
# a copy of the License at # a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# 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 sys
import textwrap import textwrap
from unittest import mock
import ddt import ddt
import mock import pycodestyle
import pep8
from cloudkitty.hacking import checks from cloudkitty.hacking import checks
from cloudkitty import tests from cloudkitty import tests
@ddt.ddt @ddt.ddt
class HackingTestCase(tests.TestCase): class HackingTestCase(tests.TestCase):
"""Hacking test cases """Hacking test cases
This class tests the hacking checks in cloudkitty.hacking.checks by passing This class tests the hacking checks in cloudkitty.hacking.checks by passing
strings to the check methods like the pep8/flake8 parser would. The parser strings to the check methods like the pep8/flake8 parser would. The parser
skipping to change at line 105 skipping to change at line 104
# Complete code coverage by falling through all checks # Complete code coverage by falling through all checks
self.assertEqual(0, len(list(checks.check_explicit_underscore_import( self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"LOG.info('My info message')", "LOG.info('My info message')",
"cloudkitty.tests.unit/other_files4.py")))) "cloudkitty.tests.unit/other_files4.py"))))
self.assertEqual(1, len(list(checks.check_explicit_underscore_import( self.assertEqual(1, len(list(checks.check_explicit_underscore_import(
"msg = _('My message')", "msg = _('My message')",
"cloudkitty.tests.unit/other_files5.py")))) "cloudkitty.tests.unit/other_files5.py"))))
# We are patching pep8 so that only the check under test is actually # We are patching pep8 so that only the check under test is actually
# installed. # installed.
@mock.patch('pep8._checks', @mock.patch('pycodestyle._checks',
{'physical_line': {}, 'logical_line': {}, 'tree': {}}) {'physical_line': {}, 'logical_line': {}, 'tree': {}})
def _run_check(self, code, checker, filename=None): def _run_check(self, code, checker, filename=None):
pep8.register_check(checker) pycodestyle.register_check(checker)
lines = textwrap.dedent(code).strip().splitlines(True) lines = textwrap.dedent(code).strip().splitlines(True)
checker = pep8.Checker(filename=filename, lines=lines) checker = pycodestyle.Checker(filename=filename, lines=lines)
checker.check_all() checker.check_all()
checker.report._deferred_print.sort() checker.report._deferred_print.sort()
return checker.report._deferred_print return checker.report._deferred_print
def _assert_has_errors(self, code, checker, expected_errors=None, def _assert_has_errors(self, code, checker, expected_errors=None,
filename=None): filename=None):
actual_errors = [e[:3] for e in actual_errors = [e[:3] for e in
self._run_check(code, checker, filename)] self._run_check(code, checker, filename)]
self.assertEqual(expected_errors or [], actual_errors) self.assertEqual(expected_errors or [], actual_errors)
skipping to change at line 148 skipping to change at line 147
@ddt.data(*checks.CheckLoggingFormatArgs.LOG_METHODS) @ddt.data(*checks.CheckLoggingFormatArgs.LOG_METHODS)
def test_logging_with_tuple_argument(self, log_method): def test_logging_with_tuple_argument(self, log_method):
checker = checks.CheckLoggingFormatArgs checker = checks.CheckLoggingFormatArgs
code = """ code = """
import logging import logging
LOG = logging.getLogger() LOG = logging.getLogger()
LOG.{0}("Volume %s caught fire and is at %d degrees C and " LOG.{0}("Volume %s caught fire and is at %d degrees C and "
"climbing.", ('volume1', 500)) "climbing.", ('volume1', 500))
""" """
self._assert_has_errors(code.format(log_method), checker, self._assert_has_errors(code.format(log_method), checker,
expected_errors=[(4, 21, 'C310')]) expected_errors=[(4, mock.ANY, 'C310')])
def test_str_on_exception(self): def test_str_on_exception(self):
checker = checks.CheckForStrUnicodeExc checker = checks.CheckForStrUnicodeExc
code = """ code = """
def f(a, b): def f(a, b):
try: try:
p = str(a) + str(b) p = str(a) + str(b)
except ValueError as e: except ValueError as e:
p = str(e) p = str(e)
return p return p
""" """
errors = [(5, 16, 'C314')] errors = [(5, mock.ANY, 'C314')]
self._assert_has_errors(code, checker, expected_errors=errors) self._assert_has_errors(code, checker, expected_errors=errors)
def test_no_str_unicode_on_exception(self): def test_no_str_unicode_on_exception(self):
checker = checks.CheckForStrUnicodeExc checker = checks.CheckForStrUnicodeExc
code = """ code = """
def f(a, b): def f(a, b):
try: try:
p = unicode(a) + str(b) p = unicode(a) + str(b)
except ValueError as e: except ValueError as e:
p = e p = e
skipping to change at line 186 skipping to change at line 185
def test_unicode_on_exception(self): def test_unicode_on_exception(self):
checker = checks.CheckForStrUnicodeExc checker = checks.CheckForStrUnicodeExc
code = """ code = """
def f(a, b): def f(a, b):
try: try:
p = str(a) + str(b) p = str(a) + str(b)
except ValueError as e: except ValueError as e:
p = unicode(e) p = unicode(e)
return p return p
""" """
errors = [(5, 20, 'C314')] errors = [(5, mock.ANY, 'C314')]
self._assert_has_errors(code, checker, expected_errors=errors) self._assert_has_errors(code, checker, expected_errors=errors)
def test_str_on_multiple_exceptions(self): def test_str_on_multiple_exceptions(self):
checker = checks.CheckForStrUnicodeExc checker = checks.CheckForStrUnicodeExc
code = """ code = """
def f(a, b): def f(a, b):
try: try:
p = str(a) + str(b) p = str(a) + str(b)
except ValueError as e: except ValueError as e:
try: try:
p = unicode(a) + unicode(b) p = unicode(a) + unicode(b)
except ValueError as ve: except ValueError as ve:
p = str(e) + str(ve) p = str(e) + str(ve)
p = e p = e
return p return p
""" """
errors = [(8, 20, 'C314'), (8, 29, 'C314')] errors = [(8, mock.ANY, 'C314'), (8, mock.ANY, 'C314')]
self._assert_has_errors(code, checker, expected_errors=errors) self._assert_has_errors(code, checker, expected_errors=errors)
def test_str_unicode_on_multiple_exceptions(self): def test_str_unicode_on_multiple_exceptions(self):
checker = checks.CheckForStrUnicodeExc checker = checks.CheckForStrUnicodeExc
code = """ code = """
def f(a, b): def f(a, b):
try: try:
p = str(a) + str(b) p = str(a) + str(b)
except ValueError as e: except ValueError as e:
try: try:
p = unicode(a) + unicode(b) p = unicode(a) + unicode(b)
except ValueError as ve: except ValueError as ve:
p = str(e) + unicode(ve) p = str(e) + unicode(ve)
p = str(e) p = str(e)
return p return p
""" """
errors = [(8, 20, 'C314'), (8, 33, 'C314'), (9, 16, 'C314')] errors = [(8, mock.ANY, 'C314'),
(8, mock.ANY, 'C314'),
(9, mock.ANY, 'C314')]
self._assert_has_errors(code, checker, expected_errors=errors) self._assert_has_errors(code, checker, expected_errors=errors)
def test_trans_add(self): def test_trans_add(self):
checker = checks.CheckForTransAdd checker = checks.CheckForTransAdd
code = """ code = """
def fake_tran(msg): def fake_tran(msg):
return msg return msg
_ = fake_tran _ = fake_tran
def f(a, b): def f(a, b):
msg = _('test') + 'add me' msg = _('test') + 'add me'
msg = 'add to me' + _('test') msg = 'add to me' + _('test')
return msg return msg
""" """
# Python 3.4.0 introduced a change to the column calculation during AST # We don't assert on specific column numbers since there is a small
# parsing. This was reversed in Python 3.4.3, hence the version-based # change in calculation between <py38 and >=py38
# expected value calculation. See #1499743 for more background. errors = [(9, mock.ANY, 'C315'), (10, mock.ANY, 'C315')]
if sys.version_info < (3, 4, 0) or sys.version_info >= (3, 4, 3):
errors = [(9, 10, 'C315'), (10, 24, 'C315')]
else:
errors = [(9, 11, 'C315'), (10, 25, 'C315')]
self._assert_has_errors(code, checker, expected_errors=errors) self._assert_has_errors(code, checker, expected_errors=errors)
code = """ code = """
def f(a, b): def f(a, b):
msg = 'test' + 'add me' msg = 'test' + 'add me'
return msg return msg
""" """
errors = [] errors = []
self._assert_has_errors(code, checker, expected_errors=errors) self._assert_has_errors(code, checker, expected_errors=errors)
 End of changes. 12 change blocks. 
18 lines changed or deleted 15 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)