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.

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