]> jfr.im git - yt-dlp.git/blame - test/test_download.py
Fix Unicode handling GenericIE (Fixes #734)
[yt-dlp.git] / test / test_download.py
CommitLineData
fd5ff020
FV
1#!/usr/bin/env python
2
5c892b0b 3import errno
efe8902f 4import hashlib
fd5ff020 5import io
efe8902f 6import os
7f60b5aa 7import json
cdab8aa3
PH
8import unittest
9import sys
0eaf520d 10import hashlib
6b3aef80 11import socket
fd5ff020
FV
12
13# Allow direct execution
14sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
cdab8aa3 15
fd5ff020
FV
16import youtube_dl.FileDownloader
17import youtube_dl.InfoExtractors
18from youtube_dl.utils import *
1535ac2a 19
20DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
fd5ff020
FV
21PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
22
23# General configuration (from __init__, not very elegant...)
24jar = compat_cookiejar.CookieJar()
25cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
26proxy_handler = compat_urllib_request.ProxyHandler()
27opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
28compat_urllib_request.install_opener(opener)
d8bbf201 29socket.setdefaulttimeout(10)
fd5ff020 30
5c892b0b
PH
31def _try_rm(filename):
32 """ Remove a file if it exists """
33 try:
34 os.remove(filename)
35 except OSError as ose:
36 if ose.errno != errno.ENOENT:
37 raise
38
fd5ff020
FV
39class FileDownloader(youtube_dl.FileDownloader):
40 def __init__(self, *args, **kwargs):
fd5ff020 41 self.to_stderr = self.to_screen
0eaf520d
FV
42 self.processed_info_dicts = []
43 return youtube_dl.FileDownloader.__init__(self, *args, **kwargs)
44 def process_info(self, info_dict):
45 self.processed_info_dicts.append(info_dict)
46 return youtube_dl.FileDownloader.process_info(self, info_dict)
1535ac2a 47
fd5ff020
FV
48def _file_md5(fn):
49 with open(fn, 'rb') as f:
50 return hashlib.md5(f.read()).hexdigest()
51
52with io.open(DEF_FILE, encoding='utf-8') as deff:
53 defs = json.load(deff)
54with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
55 parameters = json.load(pf)
1535ac2a 56
0eaf520d 57
1535ac2a 58class TestDownload(unittest.TestCase):
fd5ff020
FV
59 def setUp(self):
60 self.parameters = parameters
61 self.defs = defs
62
911ee27e 63### Dynamically generate tests
5d01a647
PH
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
5c892b0b 71 if 'playlist' not in test_case and not test_case['file']:
6985325e
PH
72 print('Skipping: No output file specified')
73 return
fd5ff020
FV
74 if 'skip' in test_case:
75 print('Skipping: {0}'.format(test_case['skip']))
76 return
0eaf520d 77
c073e35b
PH
78 params = self.parameters.copy()
79 params.update(test_case.get('params', {}))
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')())
bffbd5f0
PH
85 finished_hook_called = set()
86 def _hook(status):
87 if status['status'] == 'finished':
88 finished_hook_called.add(status['filename'])
89 fd.add_progress_hook(_hook)
5c892b0b
PH
90
91 test_cases = test_case.get('playlist', [test_case])
92 for tc in test_cases:
93 _try_rm(tc['file'])
3a648b20 94 _try_rm(tc['file'] + '.part')
5c892b0b
PH
95 _try_rm(tc['file'] + '.info.json')
96 try:
97 fd.download([test_case['url']])
98
99 for tc in test_cases:
511eda8e 100 if not test_case.get('params', {}).get('skip_download', False):
233a2296 101 self.assertTrue(os.path.exists(tc['file']), msg='Missing file ' + tc['file'])
bffbd5f0 102 self.assertTrue(tc['file'] in finished_hook_called)
5c892b0b
PH
103 self.assertTrue(os.path.exists(tc['file'] + '.info.json'))
104 if 'md5' in tc:
105 md5_for_file = _file_md5(tc['file'])
106 self.assertEqual(md5_for_file, tc['md5'])
107 with io.open(tc['file'] + '.info.json', encoding='utf-8') as infof:
108 info_dict = json.load(infof)
109 for (info_field, value) in tc.get('info_dict', {}).items():
30e9f449 110 self.assertEqual(value, info_dict.get(info_field))
5c892b0b
PH
111 finally:
112 for tc in test_cases:
113 _try_rm(tc['file'])
3a648b20 114 _try_rm(tc['file'] + '.part')
5c892b0b 115 _try_rm(tc['file'] + '.info.json')
fd5ff020 116
1535ac2a 117 return test_template
fd5ff020 118
5d01a647 119### And add them to TestDownload
fd5ff020 120for test_case in defs:
5d01a647 121 test_method = generator(test_case)
fd5ff020
FV
122 test_method.__name__ = "test_{0}".format(test_case["name"])
123 setattr(TestDownload, test_method.__name__, test_method)
5d01a647 124 del test_method
cdab8aa3
PH
125
126
127if __name__ == '__main__':
128 unittest.main()