You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.6 KiB

  1. from __future__ import unicode_literals
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. import io
  8. import os
  9. import re
  10. import unittest
  11. rootDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  12. IGNORED_FILES = [
  13. 'setup.py', # http://bugs.python.org/issue13943
  14. 'conf.py',
  15. 'buildserver.py',
  16. ]
  17. from test.helper import assertRegexpMatches
  18. class TestUnicodeLiterals(unittest.TestCase):
  19. def test_all_files(self):
  20. for dirpath, _, filenames in os.walk(rootDir):
  21. for basename in filenames:
  22. if not basename.endswith('.py'):
  23. continue
  24. if basename in IGNORED_FILES:
  25. continue
  26. fn = os.path.join(dirpath, basename)
  27. with io.open(fn, encoding='utf-8') as inf:
  28. code = inf.read()
  29. if "'" not in code and '"' not in code:
  30. continue
  31. assertRegexpMatches(
  32. self,
  33. code,
  34. r'(?:(?:#.*?|\s*)\n)*from __future__ import (?:[a-z_]+,\s*)*unicode_literals',
  35. 'unicode_literals import missing in %s' % fn)
  36. m = re.search(r'(?<=\s)u[\'"](?!\)|,|$)', code)
  37. if m is not None:
  38. self.assertTrue(
  39. m is None,
  40. 'u present in %s, around %s' % (
  41. fn, code[m.start() - 10:m.end() + 10]))
  42. if __name__ == '__main__':
  43. unittest.main()