Merge 'rg3/master' into fork_master
[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
27 # Allow direct execution
28 import os
29 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
30
31 from youtube_dl.FileDownloader import FileDownloader
32 import youtube_dl.InfoExtractors
33
34 def _file_md5(fn):
35     with open(fn, 'rb') as f:
36         return hashlib.md5(f.read()).hexdigest()
37 try:
38     _skip_unless = unittest.skipUnless
39 except AttributeError: # Python 2.6
40     def _skip_unless(cond, reason='No reason given'):
41         def resfunc(f):
42             # Start the function name with test to appease nosetests-2.6
43             def test_wfunc(*args, **kwargs):
44                 if cond:
45                     return f(*args, **kwargs)
46                 else:
47                     print('Skipped test')
48                     return
49             return test_wfunc
50         return resfunc
51 _skip = lambda *args, **kwargs: _skip_unless(False, *args, **kwargs)
52
53 class DownloadTest(unittest.TestCase):
54     PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
55
56     def setUp(self):
57         # Clear old files
58         self.tearDown()
59
60         with io.open(self.PARAMETERS_FILE, encoding='utf-8') as pf:
61             self.parameters = json.load(pf)
62 '''
63
64 FOOTER = u'''
65
66 if __name__ == '__main__':
67     unittest.main()
68 '''
69
70 DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
71 TEST_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_download.py')
72
73 def gentests():
74     with io.open(DEF_FILE, encoding='utf-8') as deff:
75         defs = json.load(deff)
76     with io.open(TEST_FILE, 'w', encoding='utf-8') as testf:
77         testf.write(HEADER)
78         spaces = ' ' * 4
79         write = lambda l: testf.write(spaces + l + '\n')
80
81         for d in defs:
82             name = d['name']
83             ie = getattr(youtube_dl.InfoExtractors, name + 'IE')
84             testf.write('\n')
85             write('@_skip_unless(youtube_dl.InfoExtractors.' + name + 'IE._WORKING, "IE marked as not _WORKING")')
86             if not d['file']:
87                 write('@_skip("No output file specified")')
88             elif 'skip' in d:
89                 write('@_skip(' + repr(d['skip']) + ')')
90             write('def test_' + name + '(self):')
91             write('    filename = ' + repr(d['file']))
92             write('    fd = FileDownloader(self.parameters)')
93             write('    fd.add_info_extractor(youtube_dl.InfoExtractors.' + name + 'IE())')
94             for ien in d.get('addIEs', []):
95                 write('    fd.add_info_extractor(youtube_dl.InfoExtractors.' + ien + 'IE())')
96             write('    fd.download([' + repr(d['url']) + '])')
97             write('    self.assertTrue(os.path.exists(filename))')
98             if 'size' in d:
99                 write('    self.assertEqual(os.path.getsize(filename), ' + repr(d['size']) + ')')
100             if 'md5' in d:
101                 write('    md5_for_file = _file_md5(filename)')
102                 write('    self.assertEqual(md5_for_file, ' + repr(d['md5']) + ')')
103
104         testf.write('\n\n')
105         write('def tearDown(self):')
106         for d in defs:
107             if d['file']:
108                 write('    if os.path.exists(' + repr(d['file']) + '):')
109                 write('        os.remove(' + repr(d['file']) + ')')
110             else:
111                 write('    # No file specified for ' + d['name'])
112         testf.write('\n')
113         testf.write(FOOTER)
114
115 if __name__ == '__main__':
116     gentests()