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.

109 lines
3.5 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. #!/usr/bin/env python
  2. import hashlib
  3. import io
  4. import os
  5. import json
  6. import unittest
  7. import sys
  8. import hashlib
  9. import socket
  10. # Allow direct execution
  11. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  12. import youtube_dl.FileDownloader
  13. import youtube_dl.InfoExtractors
  14. from youtube_dl.utils import *
  15. DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
  16. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
  17. # General configuration (from __init__, not very elegant...)
  18. jar = compat_cookiejar.CookieJar()
  19. cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
  20. proxy_handler = compat_urllib_request.ProxyHandler()
  21. opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
  22. compat_urllib_request.install_opener(opener)
  23. class FileDownloader(youtube_dl.FileDownloader):
  24. def __init__(self, *args, **kwargs):
  25. self.to_stderr = self.to_screen
  26. self.processed_info_dicts = []
  27. return youtube_dl.FileDownloader.__init__(self, *args, **kwargs)
  28. def process_info(self, info_dict):
  29. self.processed_info_dicts.append(info_dict)
  30. return youtube_dl.FileDownloader.process_info(self, info_dict)
  31. def _file_md5(fn):
  32. with open(fn, 'rb') as f:
  33. return hashlib.md5(f.read()).hexdigest()
  34. with io.open(DEF_FILE, encoding='utf-8') as deff:
  35. defs = json.load(deff)
  36. with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
  37. parameters = json.load(pf)
  38. class TestDownload(unittest.TestCase):
  39. def setUp(self):
  40. self.parameters = parameters
  41. self.defs = defs
  42. # Clear old files
  43. self.tearDown()
  44. def tearDown(self):
  45. for files in [ test['files'] for test in self.defs ]:
  46. for fn, md5 in files:
  47. if os.path.exists(fn):
  48. os.remove(fn)
  49. ### Dinamically generate tests
  50. def generator(test_case):
  51. def test_template(self):
  52. ie = getattr(youtube_dl.InfoExtractors, test_case['name'] + 'IE')
  53. if not ie._WORKING:
  54. print('Skipping: IE marked as not _WORKING')
  55. return
  56. if 'skip' in test_case:
  57. print('Skipping: {0}'.format(test_case['skip']))
  58. return
  59. params = dict(self.parameters) # Duplicate it locally
  60. for p in test_case.get('params', {}):
  61. params[p] = test_case['params'][p]
  62. fd = FileDownloader(params)
  63. fd.add_info_extractor(ie())
  64. for ien in test_case.get('add_ie', []):
  65. fd.add_info_extractor(getattr(youtube_dl.InfoExtractors, ien + 'IE')())
  66. fd.download([test_case['url']])
  67. for filename, md5 in test_case['files']:
  68. self.assertTrue(os.path.exists(filename))
  69. if md5:
  70. md5_for_file = _file_md5(filename)
  71. self.assertEqual(md5_for_file, md5)
  72. info_dict = fd.processed_info_dicts[0]
  73. for (info_field, value) in test_case.get('info_dict', {}).items():
  74. if value.startswith('md5:'):
  75. md5_info_value = hashlib.md5(info_dict.get(info_field, '')).hexdigest()
  76. self.assertEqual(value[3:], md5_info_value)
  77. else:
  78. self.assertEqual(value, info_dict.get(info_field))
  79. return test_template
  80. ### And add them to TestDownload
  81. for test_case in defs:
  82. test_method = generator(test_case)
  83. test_method.__name__ = "test_{0}".format(test_case["name"])
  84. setattr(TestDownload, test_method.__name__, test_method)
  85. del test_method
  86. if __name__ == '__main__':
  87. unittest.main()