11 # Allow direct execution
12 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
14 import youtube_dl.FileDownloader
15 import youtube_dl.InfoExtractors
16 from youtube_dl.utils import *
18 DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
19 PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
21 # General configuration (from __init__, not very elegant...)
22 jar = compat_cookiejar.CookieJar()
23 cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
24 proxy_handler = compat_urllib_request.ProxyHandler()
25 opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
26 compat_urllib_request.install_opener(opener)
27 socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
29 class FileDownloader(youtube_dl.FileDownloader):
30 def __init__(self, *args, **kwargs):
31 youtube_dl.FileDownloader.__init__(self, *args, **kwargs)
32 self.to_stderr = self.to_screen
35 with open(fn, 'rb') as f:
36 return hashlib.md5(f.read()).hexdigest()
38 with io.open(DEF_FILE, encoding='utf-8') as deff:
39 defs = json.load(deff)
40 with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
41 parameters = json.load(pf)
43 class TestDownload(unittest.TestCase):
45 self.parameters = parameters
52 for test in self.defs:
54 if fn and os.path.exists(fn):
58 def make_test_method(test_case):
59 def test_template(self):
60 ie = getattr(youtube_dl.InfoExtractors, test_case['name'] + 'IE')
62 print('Skipping: IE marked as not _WORKING')
64 if not test_case['file']:
65 print('Skipping: No output file specified')
67 if 'skip' in test_case:
68 print('Skipping: {0}'.format(test_case['skip']))
70 params = dict(self.parameters) # Duplicate it locally
71 for p in test_case.get('params', {}):
72 params[p] = test_case['params'][p]
73 fd = FileDownloader(params)
74 fd.add_info_extractor(ie())
75 for ien in test_case.get('add_ie', []):
76 fd.add_info_extractor(getattr(youtube_dl.InfoExtractors, ien + 'IE')())
77 fd.download([test_case['url']])
78 self.assertTrue(os.path.exists(test_case['file']))
79 if 'md5' in test_case:
80 md5_for_file = _file_md5(test_case['file'])
81 self.assertEqual(md5_for_file, test_case['md5'])
83 # TODO proper skipping annotations
86 for test_case in defs:
87 test_method = make_test_method(test_case)
88 test_method.__name__ = "test_{0}".format(test_case["name"])
89 setattr(TestDownload, test_method.__name__, test_method)
92 if __name__ == '__main__':