]>
Commit | Line | Data |
---|---|---|
6febd1c1 PH |
1 | from __future__ import unicode_literals |
2 | ||
8bdcb436 PH |
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 | ||
6febd1c1 | 9 | import io |
6febd1c1 | 10 | import re |
6febd1c1 PH |
11 | |
12 | rootDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
13 | ||
01c62591 PH |
14 | IGNORED_FILES = [ |
15 | 'setup.py', # http://bugs.python.org/issue13943 | |
dcddc10a PH |
16 | 'conf.py', |
17 | 'buildserver.py', | |
01c62591 PH |
18 | ] |
19 | ||
4d165248 JMF |
20 | IGNORED_DIRS = [ |
21 | '.git', | |
22 | '.tox', | |
23 | ] | |
6febd1c1 | 24 | |
da4d4191 | 25 | from test.helper import assertRegexpMatches |
8bdcb436 PH |
26 | |
27 | ||
6febd1c1 PH |
28 | class TestUnicodeLiterals(unittest.TestCase): |
29 | def test_all_files(self): | |
4d165248 JMF |
30 | for dirpath, dirnames, filenames in os.walk(rootDir): |
31 | for ignore_dir in IGNORED_DIRS: | |
32 | if ignore_dir in dirnames: | |
33 | # If we remove the directory from dirnames os.walk won't | |
34 | # recurse into it | |
35 | dirnames.remove(ignore_dir) | |
6febd1c1 PH |
36 | for basename in filenames: |
37 | if not basename.endswith('.py'): | |
38 | continue | |
01c62591 PH |
39 | if basename in IGNORED_FILES: |
40 | continue | |
41 | ||
6febd1c1 PH |
42 | fn = os.path.join(dirpath, basename) |
43 | with io.open(fn, encoding='utf-8') as inf: | |
44 | code = inf.read() | |
45 | ||
46 | if "'" not in code and '"' not in code: | |
47 | continue | |
8bdcb436 PH |
48 | assertRegexpMatches( |
49 | self, | |
dcddc10a | 50 | code, |
8bdcb436 | 51 | r'(?:(?:#.*?|\s*)\n)*from __future__ import (?:[a-z_]+,\s*)*unicode_literals', |
dcddc10a | 52 | 'unicode_literals import missing in %s' % fn) |
6febd1c1 PH |
53 | |
54 | m = re.search(r'(?<=\s)u[\'"](?!\)|,|$)', code) | |
55 | if m is not None: | |
56 | self.assertTrue( | |
57 | m is None, | |
58 | 'u present in %s, around %s' % ( | |
59 | fn, code[m.start() - 10:m.end() + 10])) | |
60 | ||
61 | ||
62 | if __name__ == '__main__': | |
63 | unittest.main() |