12 # Allow direct execution
13 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
15 import youtube_dl.FileDownloader
16 import youtube_dl.InfoExtractors
17 from youtube_dl.utils import *
19 DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
20 PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
22 # General configuration (from __init__, not very elegant...)
23 jar = compat_cookiejar.CookieJar()
24 cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
25 proxy_handler = compat_urllib_request.ProxyHandler()
26 opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
27 compat_urllib_request.install_opener(opener)
28 socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
30 class FileDownloader(youtube_dl.FileDownloader):
31 def __init__(self, *args, **kwargs):
32 self.to_stderr = self.to_screen
33 self.processed_info_dicts = []
34 return youtube_dl.FileDownloader.__init__(self, *args, **kwargs)
35 def process_info(self, info_dict):
36 self.processed_info_dicts.append(info_dict)
37 return youtube_dl.FileDownloader.process_info(self, info_dict)
40 with open(fn, 'rb') as f:
41 return hashlib.md5(f.read()).hexdigest()
43 with io.open(DEF_FILE, encoding='utf-8') as deff:
44 defs = json.load(deff)
45 with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
46 parameters = json.load(pf)
49 class TestDownload(unittest.TestCase):
51 self.parameters = parameters
58 for fn in [ test.get('file', False) for test in self.defs ]:
59 if fn and os.path.exists(fn):
63 ### Dinamically generate tests
64 def generator(test_case):
66 def test_template(self):
67 ie = getattr(youtube_dl.InfoExtractors, test_case['name'] + 'IE')
69 print('Skipping: IE marked as not _WORKING')
71 if not test_case['file']:
72 print('Skipping: No output file specified')
74 if 'skip' in test_case:
75 print('Skipping: {0}'.format(test_case['skip']))
78 params = dict(self.parameters) # Duplicate it locally
79 for p in test_case.get('params', {}):
80 params[p] = test_case['params'][p]
82 fd = FileDownloader(params)
83 fd.add_info_extractor(ie())
84 for ien in test_case.get('add_ie', []):
85 fd.add_info_extractor(getattr(youtube_dl.InfoExtractors, ien + 'IE')())
86 fd.download([test_case['url']])
88 self.assertTrue(os.path.exists(test_case['file']))
89 if 'md5' in test_case:
90 md5_for_file = _file_md5(test_case['file'])
91 self.assertEqual(md5_for_file, test_case['md5'])
92 info_dict = fd.processed_info_dicts[0]
93 for (info_element, value) in test_case.get('info_dict', {}).items():
94 if value.startswith('md5:'):
95 md5_info_value = hashlib.md5(info_dict[info_element]).hexdigest()
96 self.assertEqual(value[3:], md5_info_value)
98 self.assertEqual(value, info_dict[info_element])
102 ### And add them to TestDownload
103 for test_case in defs:
104 test_method = generator(test_case)
105 test_method.__name__ = "test_{0}".format(test_case["name"])
106 setattr(TestDownload, test_method.__name__, test_method)
110 if __name__ == '__main__':