]> Raphaƫl G. Git Repositories - youtubedl/blob - test/gentests.py
debian/rules: Remove repackaging need: Upstream adopted a new strategy.
[youtubedl] / test / gentests.py
1 #!/usr/bin/env python3
2
3 import io # for python 2
4 import json
5 import os
6 import sys
7 import unittest
8
9 # Allow direct execution
10 import os
11 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
12
13 import youtube_dl.InfoExtractors
14
15 HEADER = u'''#!/usr/bin/env python
16
17 # DO NOT EDIT THIS FILE BY HAND!
18 # It is auto-generated from tests.json and gentests.py.
19
20 import hashlib
21 import io
22 import os
23 import json
24 import unittest
25 import sys
26 import socket
27
28 # Allow direct execution
29 import os
30 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
31
32 import youtube_dl.FileDownloader
33 import youtube_dl.InfoExtractors
34 from youtube_dl.utils import *
35
36 # General configuration (from __init__, not very elegant...)
37 jar = compat_cookiejar.CookieJar()
38 cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
39 proxy_handler = compat_urllib_request.ProxyHandler()
40 opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
41 compat_urllib_request.install_opener(opener)
42 socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
43
44 class FileDownloader(youtube_dl.FileDownloader):
45 def __init__(self, *args, **kwargs):
46 youtube_dl.FileDownloader.__init__(self, *args, **kwargs)
47 self.to_stderr = self.to_screen
48
49 def _file_md5(fn):
50 with open(fn, 'rb') as f:
51 return hashlib.md5(f.read()).hexdigest()
52 try:
53 _skip_unless = unittest.skipUnless
54 except AttributeError: # Python 2.6
55 def _skip_unless(cond, reason='No reason given'):
56 def resfunc(f):
57 # Start the function name with test to appease nosetests-2.6
58 def test_wfunc(*args, **kwargs):
59 if cond:
60 return f(*args, **kwargs)
61 else:
62 print('Skipped test')
63 return
64 return test_wfunc
65 return resfunc
66 _skip = lambda *args, **kwargs: _skip_unless(False, *args, **kwargs)
67
68 class DownloadTest(unittest.TestCase):
69 PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
70
71 def setUp(self):
72 # Clear old files
73 self.tearDown()
74
75 with io.open(self.PARAMETERS_FILE, encoding='utf-8') as pf:
76 self.parameters = json.load(pf)
77 '''
78
79 FOOTER = u'''
80
81 if __name__ == '__main__':
82 unittest.main()
83 '''
84
85 DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
86 TEST_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_download.py')
87
88 def gentests():
89 with io.open(DEF_FILE, encoding='utf-8') as deff:
90 defs = json.load(deff)
91 with io.open(TEST_FILE, 'w', encoding='utf-8') as testf:
92 testf.write(HEADER)
93 spaces = ' ' * 4
94 write = lambda l: testf.write(spaces + l + u'\n')
95
96 for d in defs:
97 name = d['name']
98 ie = getattr(youtube_dl.InfoExtractors, name + 'IE')
99 testf.write(u'\n')
100 write('@_skip_unless(youtube_dl.InfoExtractors.' + name + 'IE._WORKING, "IE marked as not _WORKING")')
101 if not d['file']:
102 write('@_skip("No output file specified")')
103 elif 'skip' in d:
104 write('@_skip(' + repr(d['skip']) + ')')
105 write('def test_' + name + '(self):')
106 write(' filename = ' + repr(d['file']))
107 write(' params = self.parameters')
108 for p in d.get('params', {}):
109 write(' params["' + p + '"] = ' + repr(d['params'][p]))
110 write(' fd = FileDownloader(params)')
111 write(' fd.add_info_extractor(youtube_dl.InfoExtractors.' + name + 'IE())')
112 for ien in d.get('addIEs', []):
113 write(' fd.add_info_extractor(youtube_dl.InfoExtractors.' + ien + 'IE())')
114 write(' fd.download([' + repr(d['url']) + '])')
115 write(' self.assertTrue(os.path.exists(filename))')
116 if 'md5' in d:
117 write(' md5_for_file = _file_md5(filename)')
118 write(' self.assertEqual(md5_for_file, ' + repr(d['md5']) + ')')
119
120 testf.write(u'\n\n')
121 write('def tearDown(self):')
122 for d in defs:
123 if d['file']:
124 write(' if os.path.exists(' + repr(d['file']) + '):')
125 write(' os.remove(' + repr(d['file']) + ')')
126 else:
127 write(' # No file specified for ' + d['name'])
128 testf.write(u'\n')
129 testf.write(FOOTER)
130
131 if __name__ == '__main__':
132 gentests()