Add translation_checks for i18n
This is the first patch to enable the translation checks. These translation checks were taken from neutron_lib/hacking Translation checks include: * validate_log_translation: Translate log messages * no_translate_debug_logs: Don't transate debug level logs * check_raised_localization_exception: Translate exception message The pep8 tests will fail until all files meet the standards enforced by these translaiton checks. So this patch is depenedent on the following: Depends-On: Ieb7e006de497c974756941608aea91f63e860dc5 Depends-On: I1d0649f240365b7f62b13af6edb1e8dbb5d06597 Depends-On: I48f95b3c090e9b6f91185c14113daae5bc621d0b Depends-On: I5feafe8945960e34a8d0e038ad4617f39502f19b Depends-On: If29f7b5df7f6958cec4b26f72babc47ca70f3706 Depends-On: Iddd738c2d9a7c9a57fcd445650d087123dbccfc4 Depends-On: Ifdf426684473fb05cae63877c334e6cf65aa5234 Depends-On: I94a341ba1fb4178e0c358c37ea31623d48938ec6 Depends-On: Ic8664bf105d601909c6efa5cf73db6c27adaf42e Depends-On: I80f2ea6a20944cfbfdc79a8a71ad744f23aeaac1 Depends-On: If68b38ef5bdeba2fdc28f94d964ef8ce6d15c352 Depends-On: Ia54014fa8e44aacc7935bd73c1ee8d3139a19735 Depends-On: Iea58f4df337c79785dc7afe0e31bcfa7e231f374 Depends-On: I2b0313021fd9599bdaeb375358e8cf834581d493 Depends-On: I56972d16634654b8b71853d6aba10299cc9e2418 Depends-On: If632727fd29ec8d36c6890ebd156a46be70ba783 Depends-On: I1df8d4cba6a3a2ec23e8a1b2aa4ffc03c97b149e Depends-On: I4070117b6335f4d85f35cc6473653c17d7c14bac Depends-On: I83f2ccd623588af3fc5f94334b753cebcc3b6c18 Depends-On: I44ac13e9431018e18980e8ff7ff442c7fea68a13 Depends-On: I88284f61b5f37d78ad050a97c679f6bde23d832d Depends-On: I6fb2bdcc4b83457e08b24599fb4a297ef6ec6c14 Change-Id: Ia2844799a2af8e020470d4c513ad55a51ec36ce1
This commit is contained in:
0
trove/tests/unittests/hacking/__init__.py
Normal file
0
trove/tests/unittests/hacking/__init__.py
Normal file
89
trove/tests/unittests/hacking/test_translation_checks.py
Normal file
89
trove/tests/unittests/hacking/test_translation_checks.py
Normal file
@@ -0,0 +1,89 @@
|
||||
# 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.
|
||||
|
||||
from trove.hacking import translation_checks as tc
|
||||
from trove.tests.unittests import trove_testtools
|
||||
|
||||
|
||||
class HackingTestCase(trove_testtools.TestCase):
|
||||
|
||||
def assertLinePasses(self, func, *args):
|
||||
def check_callable(f, *args):
|
||||
return next(f(*args))
|
||||
self.assertRaises(StopIteration, check_callable, func, *args)
|
||||
|
||||
def assertLineFails(self, func, *args):
|
||||
self.assertIsInstance(next(func(*args)), tuple)
|
||||
|
||||
def test_factory(self):
|
||||
def check_callable(fn):
|
||||
self.assertTrue(hasattr(fn, '__call__'))
|
||||
self.assertIsNone(tc.factory(check_callable))
|
||||
|
||||
def test_log_translations(self):
|
||||
expected_marks = {
|
||||
'error': '_',
|
||||
'info': '_',
|
||||
'warning': '_',
|
||||
'critical': '_',
|
||||
'exception': '_',
|
||||
}
|
||||
logs = expected_marks.keys()
|
||||
debug = "LOG.debug('OK')"
|
||||
self.assertEqual(
|
||||
0, len(list(tc.validate_log_translations(debug, debug, 'f'))))
|
||||
for log in logs:
|
||||
bad = 'LOG.%s("Bad")' % log
|
||||
self.assertEqual(
|
||||
1, len(list(tc.validate_log_translations(bad, bad, 'f'))))
|
||||
ok = 'LOG.%s(_("OK"))' % log
|
||||
self.assertEqual(
|
||||
0, len(list(tc.validate_log_translations(ok, ok, 'f'))))
|
||||
ok = "LOG.%s('OK') # noqa" % log
|
||||
self.assertEqual(
|
||||
0, len(list(tc.validate_log_translations(ok, ok, 'f'))))
|
||||
ok = "LOG.%s(variable)" % log
|
||||
self.assertEqual(
|
||||
0, len(list(tc.validate_log_translations(ok, ok, 'f'))))
|
||||
# Do not do validations in tests
|
||||
ok = 'LOG.%s("OK - unit tests")' % log
|
||||
self.assertEqual(
|
||||
0, len(list(tc.validate_log_translations(ok, ok,
|
||||
'f/tests/f'))))
|
||||
|
||||
for mark in tc._all_hints:
|
||||
stmt = "LOG.%s(%s('test'))" % (log, mark)
|
||||
self.assertEqual(
|
||||
0 if expected_marks[log] == mark else 1,
|
||||
len(list(tc.validate_log_translations(stmt, stmt, 'f'))))
|
||||
|
||||
def test_no_translate_debug_logs(self):
|
||||
for hint in tc._all_hints:
|
||||
bad = "LOG.debug(%s('bad'))" % hint
|
||||
self.assertEqual(
|
||||
1, len(list(tc.no_translate_debug_logs(bad, 'f'))))
|
||||
|
||||
def test_check_localized_exception_messages(self):
|
||||
f = tc.check_raised_localized_exceptions
|
||||
self.assertLineFails(f, " raise KeyError('Error text')", '')
|
||||
self.assertLineFails(f, ' raise KeyError("Error text")', '')
|
||||
self.assertLinePasses(f, ' raise KeyError(_("Error text"))', '')
|
||||
self.assertLinePasses(f, ' raise KeyError(_ERR("Error text"))', '')
|
||||
self.assertLinePasses(f, " raise KeyError(translated_msg)", '')
|
||||
self.assertLinePasses(f, '# raise KeyError("Not translated")', '')
|
||||
self.assertLinePasses(f, 'print("raise KeyError("Not '
|
||||
'translated")")', '')
|
||||
|
||||
def test_check_localized_exception_message_skip_tests(self):
|
||||
f = tc.check_raised_localized_exceptions
|
||||
self.assertLinePasses(f, "raise KeyError('Error text')",
|
||||
'neutron_lib/tests/unit/mytest.py')
|
||||
Reference in New Issue
Block a user