Update download tests
[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
38 def md5_for_file(filename, block_size=2**20):
39     with open(filename) as f:
40         md5 = hashlib.md5()
41         while True:
42             data = f.read(block_size)
43             if not data:
44                 break
45             md5.update(data)
46             return md5.hexdigest()
47 _file_md5 = md5_for_file
48
49 class DownloadTest(unittest.TestCase):
50     PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
51
52     def setUp(self):
53         # Clear old files
54         self.tearDown()
55
56         with io.open(self.PARAMETERS_FILE, encoding='utf-8') as pf:
57             self.parameters = json.load(pf)
58 '''
59
60 FOOTER = u'''
61
62 if __name__ == '__main__':
63     unittest.main()
64 '''
65
66 DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
67 TEST_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_download.py')
68
69 def gentests():
70     with io.open(DEF_FILE, encoding='utf-8') as deff:
71         defs = json.load(deff)
72     with io.open(TEST_FILE, 'w', encoding='utf-8') as testf:
73         testf.write(HEADER)
74         spaces = ' ' * 4
75         write = lambda l: testf.write(spaces + l + '\n')
76
77         for d in defs:
78             name = d['name']
79             ie = getattr(youtube_dl.InfoExtractors, name + 'IE')
80             testf.write('\n')
81             if not ie._WORKING:
82                 write('@unittest.skip("IE marked as not _WORKING")')
83             elif not d['file']:
84                 write('@unittest.skip("No output file specified")')
85             elif 'skip' in d:
86                 write('@unittest.skip(' + repr(d['skip']) + ')')
87             write('def test_' + name + '(self):')
88             write('    ' + name + 'IE = youtube_dl.InfoExtractors.' + name + 'IE')
89             write('    filename = ' + repr(d['file']))
90             write('    fd = FileDownloader(self.parameters)')
91             write('    fd.add_info_extractor(' + name + 'IE())')
92             for ien in d.get('addIEs', []):
93                 write('    fd.add_info_extractor(youtube_dl.InfoExtractors.' + ien + 'IE())')
94             write('    fd.download([' + repr(d['url']) + '])')
95             write('    self.assertTrue(os.path.exists(filename))')
96             if 'size' in d:
97                 write('    self.assertEqual(os.path.getsize(filename), ' + repr(d['size']) + ')')
98             if 'md5' in d:
99                 write('    md5_for_file = _file_md5(filename)')
100                 write('    self.assertEqual(md5_for_file, ' + repr(d['md5']) + ')')
101
102         testf.write('\n\n')
103         write('def tearDown(self):')
104         for d in defs:
105             if d['file']:
106                 write('    if os.path.exists(' + repr(d['file']) + '):')
107                 write('        os.remove(' + repr(d['file']) + ')')
108             else:
109                 write('    # No file specified for ' + d['name'])
110         testf.write('\n')
111         testf.write(FOOTER)
112
113 if __name__ == '__main__':
114     gentests()