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.

128 lines
4.0 KiB

  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. # Allow direct execution
  21. import os
  22. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  23. from youtube_dl.FileDownloader import FileDownloader
  24. import youtube_dl.InfoExtractors
  25. def _file_md5(fn):
  26. with open(fn, 'rb') as f:
  27. return hashlib.md5(f.read()).hexdigest()
  28. def md5_for_file(filename, block_size=2**20):
  29. with open(filename) as f:
  30. md5 = hashlib.md5()
  31. while True:
  32. data = f.read(block_size)
  33. if not data:
  34. break
  35. md5.update(data)
  36. return md5.hexdigest()
  37. _file_md5 = md5_for_file
  38. try:
  39. _skip_unless = unittest.skipUnless
  40. except AttributeError: # Python 2.6
  41. def _skip_unless(cond, reason='No reason given'):
  42. def resfunc(f):
  43. def wfunc(*args, **kwargs):
  44. if cond:
  45. return f(*args, **kwargs)
  46. else:
  47. print('Skipped test')
  48. return
  49. return wfunc
  50. return resfunc
  51. _skip = lambda *args, **kwargs: _skip_unless(False, *args, **kwargs)
  52. class DownloadTest(unittest.TestCase):
  53. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
  54. def setUp(self):
  55. # Clear old files
  56. self.tearDown()
  57. with io.open(self.PARAMETERS_FILE, encoding='utf-8') as pf:
  58. self.parameters = json.load(pf)
  59. '''
  60. FOOTER = u'''
  61. if __name__ == '__main__':
  62. unittest.main()
  63. '''
  64. DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
  65. TEST_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_download.py')
  66. def gentests():
  67. with io.open(DEF_FILE, encoding='utf-8') as deff:
  68. defs = json.load(deff)
  69. with io.open(TEST_FILE, 'w', encoding='utf-8') as testf:
  70. testf.write(HEADER)
  71. spaces = ' ' * 4
  72. write = lambda l: testf.write(spaces + l + '\n')
  73. for d in defs:
  74. name = d['name']
  75. ie = getattr(youtube_dl.InfoExtractors, name + 'IE')
  76. testf.write('\n')
  77. write('@_skip_unless(youtube_dl.InfoExtractors.' + name + 'IE._WORKING, "IE marked as not _WORKING")')
  78. if not d['file']:
  79. write('@_skip("No output file specified")')
  80. elif 'skip' in d:
  81. write('@_skip(' + repr(d['skip']) + ')')
  82. write('def test_' + name + '(self):')
  83. write(' filename = ' + repr(d['file']))
  84. write(' fd = FileDownloader(self.parameters)')
  85. write(' fd.add_info_extractor(youtube_dl.InfoExtractors.' + name + 'IE())')
  86. for ien in d.get('addIEs', []):
  87. write(' fd.add_info_extractor(youtube_dl.InfoExtractors.' + ien + 'IE())')
  88. write(' fd.download([' + repr(d['url']) + '])')
  89. write(' self.assertTrue(os.path.exists(filename))')
  90. if 'size' in d:
  91. write(' self.assertEqual(os.path.getsize(filename), ' + repr(d['size']) + ')')
  92. if 'md5' in d:
  93. write(' md5_for_file = _file_md5(filename)')
  94. write(' self.assertEqual(md5_for_file, ' + repr(d['md5']) + ')')
  95. testf.write('\n\n')
  96. write('def tearDown(self):')
  97. for d in defs:
  98. if d['file']:
  99. write(' if os.path.exists(' + repr(d['file']) + '):')
  100. write(' os.remove(' + repr(d['file']) + ')')
  101. else:
  102. write(' # No file specified for ' + d['name'])
  103. testf.write('\n')
  104. testf.write(FOOTER)
  105. if __name__ == '__main__':
  106. gentests()