]>
Commit | Line | Data |
---|---|---|
fd5ff020 FV |
1 | #!/usr/bin/env python |
2 | ||
efe8902f | 3 | import hashlib |
fd5ff020 | 4 | import io |
efe8902f | 5 | import os |
7f60b5aa | 6 | import json |
cdab8aa3 PH |
7 | import unittest |
8 | import sys | |
0eaf520d | 9 | import hashlib |
6b3aef80 | 10 | import socket |
fd5ff020 FV |
11 | |
12 | # Allow direct execution | |
13 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
cdab8aa3 | 14 | |
fd5ff020 FV |
15 | import youtube_dl.FileDownloader |
16 | import youtube_dl.InfoExtractors | |
17 | from youtube_dl.utils import * | |
1535ac2a | 18 | |
19 | DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json') | |
fd5ff020 FV |
20 | PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json") |
21 | ||
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) | |
fd5ff020 FV |
28 | |
29 | class FileDownloader(youtube_dl.FileDownloader): | |
30 | def __init__(self, *args, **kwargs): | |
fd5ff020 | 31 | self.to_stderr = self.to_screen |
0eaf520d FV |
32 | self.processed_info_dicts = [] |
33 | return youtube_dl.FileDownloader.__init__(self, *args, **kwargs) | |
34 | def process_info(self, info_dict): | |
35 | self.processed_info_dicts.append(info_dict) | |
36 | return youtube_dl.FileDownloader.process_info(self, info_dict) | |
1535ac2a | 37 | |
fd5ff020 FV |
38 | def _file_md5(fn): |
39 | with open(fn, 'rb') as f: | |
40 | return hashlib.md5(f.read()).hexdigest() | |
41 | ||
42 | with io.open(DEF_FILE, encoding='utf-8') as deff: | |
43 | defs = json.load(deff) | |
44 | with io.open(PARAMETERS_FILE, encoding='utf-8') as pf: | |
45 | parameters = json.load(pf) | |
1535ac2a | 46 | |
0eaf520d | 47 | |
1535ac2a | 48 | class TestDownload(unittest.TestCase): |
fd5ff020 FV |
49 | def setUp(self): |
50 | self.parameters = parameters | |
51 | self.defs = defs | |
52 | ||
53 | # Clear old files | |
54 | self.tearDown() | |
1535ac2a | 55 | |
fd5ff020 | 56 | def tearDown(self): |
6985325e PH |
57 | for fn in [ test.get('file', False) for test in self.defs ]: |
58 | if fn and os.path.exists(fn): | |
59 | os.remove(fn) | |
1535ac2a | 60 | |
1535ac2a | 61 | |
911ee27e | 62 | ### Dynamically generate tests |
5d01a647 PH |
63 | def generator(test_case): |
64 | ||
1535ac2a | 65 | def test_template(self): |
fd5ff020 FV |
66 | ie = getattr(youtube_dl.InfoExtractors, test_case['name'] + 'IE') |
67 | if not ie._WORKING: | |
68 | print('Skipping: IE marked as not _WORKING') | |
69 | return | |
6985325e PH |
70 | if not test_case['file']: |
71 | print('Skipping: No output file specified') | |
72 | return | |
fd5ff020 FV |
73 | if 'skip' in test_case: |
74 | print('Skipping: {0}'.format(test_case['skip'])) | |
75 | return | |
0eaf520d | 76 | |
fd5ff020 FV |
77 | params = dict(self.parameters) # Duplicate it locally |
78 | for p in test_case.get('params', {}): | |
79 | params[p] = test_case['params'][p] | |
0eaf520d | 80 | |
fd5ff020 FV |
81 | fd = FileDownloader(params) |
82 | fd.add_info_extractor(ie()) | |
83 | for ien in test_case.get('add_ie', []): | |
84 | fd.add_info_extractor(getattr(youtube_dl.InfoExtractors, ien + 'IE')()) | |
85 | fd.download([test_case['url']]) | |
0eaf520d | 86 | |
6985325e PH |
87 | self.assertTrue(os.path.exists(test_case['file'])) |
88 | if 'md5' in test_case: | |
89 | md5_for_file = _file_md5(test_case['file']) | |
90 | self.assertEqual(md5_for_file, test_case['md5']) | |
0eaf520d | 91 | info_dict = fd.processed_info_dicts[0] |
6b3aef80 | 92 | for (info_field, value) in test_case.get('info_dict', {}).items(): |
0eaf520d | 93 | if value.startswith('md5:'): |
6b3aef80 | 94 | md5_info_value = hashlib.md5(info_dict.get(info_field, '')).hexdigest() |
0eaf520d FV |
95 | self.assertEqual(value[3:], md5_info_value) |
96 | else: | |
6b3aef80 | 97 | self.assertEqual(value, info_dict.get(info_field)) |
fd5ff020 | 98 | |
1535ac2a | 99 | return test_template |
fd5ff020 | 100 | |
5d01a647 | 101 | ### And add them to TestDownload |
fd5ff020 | 102 | for test_case in defs: |
5d01a647 | 103 | test_method = generator(test_case) |
fd5ff020 FV |
104 | test_method.__name__ = "test_{0}".format(test_case["name"]) |
105 | setattr(TestDownload, test_method.__name__, test_method) | |
5d01a647 | 106 | del test_method |
cdab8aa3 PH |
107 | |
108 | ||
109 | if __name__ == '__main__': | |
110 | unittest.main() |