the test didn't load our Gzip opener
[youtube-dl] / 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('    fd = FileDownloader(self.parameters)')
108             write('    fd.add_info_extractor(youtube_dl.InfoExtractors.' + name + 'IE())')
109             for ien in d.get('addIEs', []):
110                 write('    fd.add_info_extractor(youtube_dl.InfoExtractors.' + ien + 'IE())')
111             write('    fd.download([' + repr(d['url']) + '])')
112             write('    self.assertTrue(os.path.exists(filename))')
113             if 'size' in d:
114                 write('    self.assertEqual(os.path.getsize(filename), ' + repr(d['size']) + ')')
115             if 'md5' in d:
116                 write('    md5_for_file = _file_md5(filename)')
117                 write('    self.assertEqual(md5_for_file, ' + repr(d['md5']) + ')')
118
119         testf.write(u'\n\n')
120         write('def tearDown(self):')
121         for d in defs:
122             if d['file']:
123                 write('    if os.path.exists(' + repr(d['file']) + '):')
124                 write('        os.remove(' + repr(d['file']) + ')')
125             else:
126                 write('    # No file specified for ' + d['name'])
127         testf.write(u'\n')
128         testf.write(FOOTER)
129
130 if __name__ == '__main__':
131     gentests()