test_unicode_literals.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from __future__ import unicode_literals
  2. # Allow direct execution
  3. import os
  4. import re
  5. import sys
  6. import unittest
  7. dirn = os.path.dirname
  8. rootDir = dirn(dirn(os.path.abspath(__file__)))
  9. sys.path.insert(0, rootDir)
  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. from youtube_dl.compat import compat_open as open
  22. class TestUnicodeLiterals(unittest.TestCase):
  23. def test_all_files(self):
  24. for dirpath, dirnames, filenames in os.walk(rootDir):
  25. for ignore_dir in IGNORED_DIRS:
  26. if ignore_dir in dirnames:
  27. # If we remove the directory from dirnames os.walk won't
  28. # recurse into it
  29. dirnames.remove(ignore_dir)
  30. for basename in filenames:
  31. if not basename.endswith('.py'):
  32. continue
  33. if basename in IGNORED_FILES:
  34. continue
  35. fn = os.path.join(dirpath, basename)
  36. with open(fn, encoding='utf-8') as inf:
  37. code = inf.read()
  38. if "'" not in code and '"' not in code:
  39. continue
  40. assertRegexpMatches(
  41. self,
  42. code,
  43. r'(?:(?:#.*?|\s*)\n)*from __future__ import (?:[a-z_]+,\s*)*unicode_literals',
  44. 'unicode_literals import missing in %s' % fn)
  45. m = re.search(r'(?<=\s)u[\'"](?!\)|,|$)', code)
  46. if m is not None:
  47. self.assertTrue(
  48. m is None,
  49. 'u present in %s, around %s' % (
  50. fn, code[m.start() - 10:m.end() + 10]))
  51. if __name__ == '__main__':
  52. unittest.main()