Add support for comedycentral clips (closes #233)
[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             test_wfunc.__name__ = f.__name__
65             return test_wfunc
66         return resfunc
67 _skip = lambda *args, **kwargs: _skip_unless(False, *args, **kwargs)
68
69 class DownloadTest(unittest.TestCase):
70     PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
71
72     def setUp(self):
73         # Clear old files
74         self.tearDown()
75
76         with io.open(self.PARAMETERS_FILE, encoding='utf-8') as pf:
77             self.parameters = json.load(pf)
78 '''
79
80 FOOTER = u'''
81
82 if __name__ == '__main__':
83     unittest.main()
84 '''
85
86 DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
87 TEST_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_download.py')
88
89 def gentests():
90     with io.open(DEF_FILE, encoding='utf-8') as deff:
91         defs = json.load(deff)
92     with io.open(TEST_FILE, 'w', encoding='utf-8') as testf:
93         testf.write(HEADER)
94         spaces = ' ' * 4
95         write = lambda l: testf.write(spaces + l + u'\n')
96
97         for d in defs:
98             name = d['name']
99             ie = getattr(youtube_dl.InfoExtractors, name + 'IE')
100             testf.write(u'\n')
101             write('@_skip_unless(youtube_dl.InfoExtractors.' + name + 'IE._WORKING, "IE marked as not _WORKING")')
102             if not d['file']:
103                 write('@_skip("No output file specified")')
104             elif 'skip' in d:
105                 write('@_skip(' + repr(d['skip']) + ')')
106             write('def test_' + name + '(self):')
107             write('    filename = ' + repr(d['file']))
108             write('    params = self.parameters')
109             for p in d.get('params', {}):
110                 write('    params["' + p + '"] = ' + repr(d['params'][p]))
111             write('    fd = FileDownloader(params)')
112             write('    fd.add_info_extractor(youtube_dl.InfoExtractors.' + name + 'IE())')
113             for ien in d.get('addIEs', []):
114                 write('    fd.add_info_extractor(youtube_dl.InfoExtractors.' + ien + 'IE())')
115             write('    fd.download([' + repr(d['url']) + '])')
116             write('    self.assertTrue(os.path.exists(filename))')
117             if 'md5' in d:
118                 write('    md5_for_file = _file_md5(filename)')
119                 write('    self.assertEqual(md5_for_file, ' + repr(d['md5']) + ')')
120
121         testf.write(u'\n\n')
122         write('def tearDown(self):')
123         for d in defs:
124             if d['file']:
125                 write('    if os.path.exists(' + repr(d['file']) + '):')
126                 write('        os.remove(' + repr(d['file']) + ')')
127             else:
128                 write('    # No file specified for ' + d['name'])
129         testf.write(u'\n')
130         testf.write(FOOTER)
131
132 if __name__ == '__main__':
133     gentests()