You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.5 KiB

12 years ago
12 years ago
  1. #!/usr/bin/env python3
  2. import io # for python 2
  3. import json
  4. import os
  5. import sys
  6. import unittest
  7. # Allow direct execution
  8. import os
  9. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  10. import youtube_dl.InfoExtractors
  11. HEADER = u'''#!/usr/bin/env python
  12. # DO NOT EDIT THIS FILE BY HAND!
  13. # It is auto-generated from tests.json and gentests.py.
  14. import hashlib
  15. import io
  16. import os
  17. import json
  18. import unittest
  19. import sys
  20. import socket
  21. # Allow direct execution
  22. import os
  23. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  24. import youtube_dl.FileDownloader
  25. import youtube_dl.InfoExtractors
  26. from youtube_dl.utils import *
  27. # General configuration (from __init__, not very elegant...)
  28. jar = compat_cookiejar.CookieJar()
  29. cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
  30. proxy_handler = compat_urllib_request.ProxyHandler()
  31. opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
  32. compat_urllib_request.install_opener(opener)
  33. socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
  34. class FileDownloader(youtube_dl.FileDownloader):
  35. def __init__(self, *args, **kwargs):
  36. youtube_dl.FileDownloader.__init__(self, *args, **kwargs)
  37. self.to_stderr = self.to_screen
  38. def _file_md5(fn):
  39. with open(fn, 'rb') as f:
  40. return hashlib.md5(f.read()).hexdigest()
  41. try:
  42. _skip_unless = unittest.skipUnless
  43. except AttributeError: # Python 2.6
  44. def _skip_unless(cond, reason='No reason given'):
  45. def resfunc(f):
  46. # Start the function name with test to appease nosetests-2.6
  47. def test_wfunc(*args, **kwargs):
  48. if cond:
  49. return f(*args, **kwargs)
  50. else:
  51. print('Skipped test')
  52. return
  53. test_wfunc.__name__ = f.__name__
  54. return test_wfunc
  55. return resfunc
  56. _skip = lambda *args, **kwargs: _skip_unless(False, *args, **kwargs)
  57. class DownloadTest(unittest.TestCase):
  58. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
  59. def setUp(self):
  60. # Clear old files
  61. self.tearDown()
  62. with io.open(self.PARAMETERS_FILE, encoding='utf-8') as pf:
  63. self.parameters = json.load(pf)
  64. '''
  65. FOOTER = u'''
  66. if __name__ == '__main__':
  67. unittest.main()
  68. '''
  69. DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
  70. TEST_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_download.py')
  71. def gentests():
  72. with io.open(DEF_FILE, encoding='utf-8') as deff:
  73. defs = json.load(deff)
  74. with io.open(TEST_FILE, 'w', encoding='utf-8') as testf:
  75. testf.write(HEADER)
  76. spaces = ' ' * 4
  77. write = lambda l: testf.write(spaces + l + u'\n')
  78. for d in defs:
  79. name = d['name']
  80. ie = getattr(youtube_dl.InfoExtractors, name + 'IE')
  81. testf.write(u'\n')
  82. write('@_skip_unless(youtube_dl.InfoExtractors.' + name + 'IE._WORKING, "IE marked as not _WORKING")')
  83. if not d['file']:
  84. write('@_skip("No output file specified")')
  85. elif 'skip' in d:
  86. write('@_skip(' + repr(d['skip']) + ')')
  87. write('def test_' + name + '(self):')
  88. write(' filename = ' + repr(d['file']))
  89. write(' params = self.parameters')
  90. for p in d.get('params', {}):
  91. write(' params["' + p + '"] = ' + repr(d['params'][p]))
  92. write(' fd = FileDownloader(params)')
  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 'md5' in d:
  99. write(' md5_for_file = _file_md5(filename)')
  100. write(' self.assertEqual(md5_for_file, ' + repr(d['md5']) + ')')
  101. testf.write(u'\n\n')
  102. write('def tearDown(self):')
  103. for d in defs:
  104. if d['file']:
  105. write(' if os.path.exists(' + repr(d['file']) + '):')
  106. write(' os.remove(' + repr(d['file']) + ')')
  107. else:
  108. write(' # No file specified for ' + d['name'])
  109. testf.write(u'\n')
  110. testf.write(FOOTER)
  111. if __name__ == '__main__':
  112. gentests()