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.

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