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.

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