test_unicode_literals.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. IGNORED_DIRS = [
  16. '.git',
  17. '.tox',
  18. ]
  19. from test.helper import assertRegexpMatches
  20. class TestUnicodeLiterals(unittest.TestCase):
  21. def test_all_files(self):
  22. for dirpath, dirnames, filenames in os.walk(rootDir):
  23. for ignore_dir in IGNORED_DIRS:
  24. if ignore_dir in dirnames:
  25. # If we remove the directory from dirnames os.walk won't
  26. # recurse into it
  27. dirnames.remove(ignore_dir)
  28. for basename in filenames:
  29. if not basename.endswith('.py'):
  30. continue
  31. if basename in IGNORED_FILES:
  32. continue
  33. fn = os.path.join(dirpath, basename)
  34. with io.open(fn, encoding='utf-8') as inf:
  35. code = inf.read()
  36. if "'" not in code and '"' not in code:
  37. continue
  38. assertRegexpMatches(
  39. self,
  40. code,
  41. r'(?:(?:#.*?|\s*)\n)*from __future__ import (?:[a-z_]+,\s*)*unicode_literals',
  42. 'unicode_literals import missing in %s' % fn)
  43. m = re.search(r'(?<=\s)u[\'"](?!\)|,|$)', code)
  44. if m is not None:
  45. self.assertTrue(
  46. m is None,
  47. 'u present in %s, around %s' % (
  48. fn, code[m.start() - 10:m.end() + 10]))
  49. if __name__ == '__main__':
  50. unittest.main()