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