]> jfr.im git - yt-dlp.git/blame - test/test_download.py
credit @jaimeMF
[yt-dlp.git] / test / test_download.py
CommitLineData
fd5ff020
FV
1#!/usr/bin/env python
2
efe8902f 3import hashlib
fd5ff020 4import io
efe8902f 5import os
7f60b5aa 6import json
cdab8aa3
PH
7import unittest
8import sys
0eaf520d 9import hashlib
6b3aef80 10import socket
fd5ff020
FV
11
12# Allow direct execution
13sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
cdab8aa3 14
fd5ff020
FV
15import youtube_dl.FileDownloader
16import youtube_dl.InfoExtractors
17from youtube_dl.utils import *
1535ac2a 18
19DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
fd5ff020
FV
20PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
21
22# General configuration (from __init__, not very elegant...)
23jar = compat_cookiejar.CookieJar()
24cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
25proxy_handler = compat_urllib_request.ProxyHandler()
26opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
27compat_urllib_request.install_opener(opener)
fd5ff020
FV
28
29class 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
38def _file_md5(fn):
39 with open(fn, 'rb') as f:
40 return hashlib.md5(f.read()).hexdigest()
41
42with io.open(DEF_FILE, encoding='utf-8') as deff:
43 defs = json.load(deff)
44with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
45 parameters = json.load(pf)
1535ac2a 46
0eaf520d 47
1535ac2a 48class 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):
6535e951
FV
57 for files in [ test['files'] for test in self.defs ]:
58 for fn, md5 in files:
59 if os.path.exists(fn):
60 os.remove(fn)
1535ac2a 61
1535ac2a 62
5d01a647
PH
63### Dinamically generate tests
64def generator(test_case):
65
1535ac2a 66 def test_template(self):
fd5ff020
FV
67 ie = getattr(youtube_dl.InfoExtractors, test_case['name'] + 'IE')
68 if not ie._WORKING:
69 print('Skipping: IE marked as not _WORKING')
70 return
fd5ff020
FV
71 if 'skip' in test_case:
72 print('Skipping: {0}'.format(test_case['skip']))
73 return
0eaf520d 74
fd5ff020
FV
75 params = dict(self.parameters) # Duplicate it locally
76 for p in test_case.get('params', {}):
77 params[p] = test_case['params'][p]
0eaf520d 78
fd5ff020
FV
79 fd = FileDownloader(params)
80 fd.add_info_extractor(ie())
81 for ien in test_case.get('add_ie', []):
82 fd.add_info_extractor(getattr(youtube_dl.InfoExtractors, ien + 'IE')())
83 fd.download([test_case['url']])
0eaf520d 84
6535e951
FV
85 for filename, md5 in test_case['files']:
86 self.assertTrue(os.path.exists(filename))
87 if md5:
88 md5_for_file = _file_md5(filename)
89 self.assertEqual(md5_for_file, md5)
0eaf520d 90 info_dict = fd.processed_info_dicts[0]
6b3aef80 91 for (info_field, value) in test_case.get('info_dict', {}).items():
0eaf520d 92 if value.startswith('md5:'):
6b3aef80 93 md5_info_value = hashlib.md5(info_dict.get(info_field, '')).hexdigest()
0eaf520d
FV
94 self.assertEqual(value[3:], md5_info_value)
95 else:
6b3aef80 96 self.assertEqual(value, info_dict.get(info_field))
fd5ff020 97
1535ac2a 98 return test_template
fd5ff020 99
5d01a647 100### And add them to TestDownload
fd5ff020 101for test_case in defs:
5d01a647 102 test_method = generator(test_case)
fd5ff020
FV
103 test_method.__name__ = "test_{0}".format(test_case["name"])
104 setattr(TestDownload, test_method.__name__, test_method)
5d01a647 105 del test_method
cdab8aa3
PH
106
107
108if __name__ == '__main__':
109 unittest.main()