test_unicode_literals.py 1.9 KB

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