youtube-dl/test/test_unicode_literals.py

67 lines
1.9 KiB
Python
Raw Normal View History

2014-01-05 09:52:03 +09:00
from __future__ import unicode_literals
2014-12-13 01:06:52 +09:00
# Allow direct execution
import os
2023-07-25 08:17:15 +09:00
import re
2014-12-13 01:06:52 +09:00
import sys
import unittest
2023-07-25 08:17:15 +09:00
dirn = os.path.dirname
rootDir = dirn(dirn(os.path.abspath(__file__)))
2014-01-05 09:52:03 +09:00
2023-07-25 08:17:15 +09:00
sys.path.insert(0, rootDir)
2014-01-05 09:52:03 +09:00
IGNORED_FILES = [
'setup.py', # http://bugs.python.org/issue13943
'conf.py',
'buildserver.py',
'get-pip.py',
]
IGNORED_DIRS = [
'.git',
'.tox',
]
2014-01-05 09:52:03 +09:00
from test.helper import assertRegexpMatches
2023-07-25 08:17:15 +09:00
from youtube_dl.compat import compat_open as open
2014-12-13 01:06:52 +09:00
2014-01-05 09:52:03 +09:00
class TestUnicodeLiterals(unittest.TestCase):
def test_all_files(self):
for dirpath, dirnames, filenames in os.walk(rootDir):
for ignore_dir in IGNORED_DIRS:
if ignore_dir in dirnames:
# If we remove the directory from dirnames os.walk won't
# recurse into it
dirnames.remove(ignore_dir)
2014-01-05 09:52:03 +09:00
for basename in filenames:
if not basename.endswith('.py'):
continue
if basename in IGNORED_FILES:
continue
2014-01-05 09:52:03 +09:00
fn = os.path.join(dirpath, basename)
2023-07-25 08:17:15 +09:00
with open(fn, encoding='utf-8') as inf:
2014-01-05 09:52:03 +09:00
code = inf.read()
if "'" not in code and '"' not in code:
continue
2014-12-13 01:06:52 +09:00
assertRegexpMatches(
self,
code,
2014-12-13 01:06:52 +09:00
r'(?:(?:#.*?|\s*)\n)*from __future__ import (?:[a-z_]+,\s*)*unicode_literals',
'unicode_literals import missing in %s' % fn)
2014-01-05 09:52:03 +09:00
m = re.search(r'(?<=\s)u[\'"](?!\)|,|$)', code)
if m is not None:
self.assertTrue(
m is None,
'u present in %s, around %s' % (
fn, code[m.start() - 10:m.end() + 10]))
if __name__ == '__main__':
unittest.main()