diff --git a/tests/hacking/README.rst b/tests/hacking/README.rst index 233d4c91..49dd2240 100644 --- a/tests/hacking/README.rst +++ b/tests/hacking/README.rst @@ -25,4 +25,5 @@ Rally Specific Commandments * [N341] - Ensure that we are importing oslo_xyz packages instead of deprecated oslo.xyz ones * [N350] - Ensure that single quotes are not used * [N351] - Ensure that data structs (i.e Lists and Dicts) are declared literally rather than using constructors -* [N353] - Ensure that string formatting only uses a mapping if multiple mapping keys are used. +* [N352] - Ensure that string formatting only uses a mapping if multiple mapping keys are used. +* [N353] - Ensure that unicode() function is not uset because of absence in py3 diff --git a/tests/hacking/checks.py b/tests/hacking/checks.py index 04f9c72c..a413233b 100644 --- a/tests/hacking/checks.py +++ b/tests/hacking/checks.py @@ -427,6 +427,18 @@ def check_dict_formatting_in_string(logical_line, tokens): current_string = "" +@skip_ignored_lines +def check_using_unicode(logical_line, filename): + """Check crosspython unicode usage + + N353 + """ + + if re.search(r"\bunicode\(", logical_line): + yield (0, "N353 'unicode' function is absent in python3. Please " + "use 'six.text_type' instead.") + + def factory(register): register(check_assert_methods_from_mock) register(check_import_of_logging) @@ -442,3 +454,4 @@ def factory(register): register(check_quotes) register(check_no_constructor_data_struct) register(check_dict_formatting_in_string) + register(check_using_unicode) diff --git a/tests/unit/test_hacking.py b/tests/unit/test_hacking.py index cc4a12e9..21ea3696 100644 --- a/tests/unit/test_hacking.py +++ b/tests/unit/test_hacking.py @@ -306,3 +306,15 @@ class HackingTestCase(test.TestCase): self.assertEqual( [], list(checks.check_dict_formatting_in_string(sample, tokens))) + + def test_check_using_unicode(self): + + checkres = checks.check_using_unicode("text = unicode('sometext')", + "fakefile") + self.assertIsNotNone(next(checkres)) + self.assertEqual([], list(checkres)) + + checkres = checks.check_using_unicode( + "text = process(unicode('sometext'))", "fakefile") + self.assertIsNotNone(next(checkres)) + self.assertEqual([], list(checkres))