]>
Raphaƫl G. Git Repositories - youtubedl/blob - test/gentests.py
0ea5d9f21e74b86ebb95d4791dc6e877833370b2
3 import io
# for python 2
9 # Allow direct execution
11 sys
.path
.append(os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
13 import youtube_dl
.InfoExtractors
15 HEADER
= u
'''#!/usr/bin/env python
17 # DO NOT EDIT THIS FILE BY HAND!
18 # It is auto-generated from tests.json and gentests.py.
28 # Allow direct execution
30 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
32 import youtube_dl.FileDownloader
33 import youtube_dl.InfoExtractors
34 from youtube_dl.utils import *
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)
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
50 with open(fn, 'rb') as f:
51 return hashlib.md5(f.read()).hexdigest()
53 _skip_unless = unittest.skipUnless
54 except AttributeError: # Python 2.6
55 def _skip_unless(cond, reason='No reason given'):
57 # Start the function name with test to appease nosetests-2.6
58 def test_wfunc(*args, **kwargs):
60 return f(*args, **kwargs)
66 _skip = lambda *args, **kwargs: _skip_unless(False, *args, **kwargs)
68 class DownloadTest(unittest.TestCase):
69 PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
75 with io.open(self.PARAMETERS_FILE, encoding='utf-8') as pf:
76 self.parameters = json.load(pf)
81 if __name__ == '__main__':
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')
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
:
94 write
= lambda l
: testf
.write(spaces
+ l
+ u
'\n')
98 ie
= getattr(youtube_dl
.InfoExtractors
, name
+ 'IE')
100 write('@_skip_unless(youtube_dl.InfoExtractors.' + name
+ 'IE._WORKING, "IE marked as not _WORKING")')
102 write('@_skip("No output file specified")')
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))')
117 write(' md5_for_file = _file_md5(filename)')
118 write(' self.assertEqual(md5_for_file, ' + repr(d
['md5']) + ')')
121 write('def tearDown(self):')
124 write(' if os.path.exists(' + repr(d
['file']) + '):')
125 write(' os.remove(' + repr(d
['file']) + ')')
127 write(' # No file specified for ' + d
['name'])
131 if __name__
== '__main__':