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.

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