logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

test_swfinterp.py (2256B)


  1. #!/usr/bin/env python
  2. from __future__ import unicode_literals
  3. # Allow direct execution
  4. import os
  5. import sys
  6. import unittest
  7. dirn = os.path.dirname
  8. sys.path.insert(0, dirn(dirn(os.path.abspath(__file__))))
  9. import errno
  10. import json
  11. import re
  12. import subprocess
  13. from youtube_dl.swfinterp import SWFInterpreter
  14. from youtube_dl.compat import compat_open as open
  15. TEST_DIR = os.path.join(
  16. os.path.dirname(os.path.abspath(__file__)), 'swftests')
  17. class TestSWFInterpreter(unittest.TestCase):
  18. pass
  19. def _make_testfunc(testfile):
  20. m = re.match(r'^(.*)\.(as)$', testfile)
  21. if not m:
  22. return
  23. test_id = m.group(1)
  24. def test_func(self):
  25. as_file = os.path.join(TEST_DIR, testfile)
  26. swf_file = os.path.join(TEST_DIR, test_id + '.swf')
  27. if ((not os.path.exists(swf_file))
  28. or os.path.getmtime(swf_file) < os.path.getmtime(as_file)):
  29. # Recompile
  30. try:
  31. subprocess.check_call([
  32. 'mxmlc', '-output', swf_file,
  33. '-static-link-runtime-shared-libraries', as_file])
  34. except OSError as ose:
  35. if ose.errno == errno.ENOENT:
  36. self.skipTest('mxmlc not found!')
  37. return
  38. raise
  39. with open(swf_file, 'rb') as swf_f:
  40. swf_content = swf_f.read()
  41. swfi = SWFInterpreter(swf_content)
  42. with open(as_file, 'r', encoding='utf-8') as as_f:
  43. as_content = as_f.read()
  44. def _find_spec(key):
  45. m = re.search(
  46. r'(?m)^//\s*%s:\s*(.*?)\n' % re.escape(key), as_content)
  47. if not m:
  48. raise ValueError('Cannot find %s in %s' % (key, testfile))
  49. return json.loads(m.group(1))
  50. input_args = _find_spec('input')
  51. output = _find_spec('output')
  52. swf_class = swfi.extract_class(test_id)
  53. func = swfi.extract_function(swf_class, 'main')
  54. res = func(input_args)
  55. self.assertEqual(res, output)
  56. test_func.__name__ = str('test_swf_' + test_id)
  57. setattr(TestSWFInterpreter, test_func.__name__, test_func)
  58. for testfile in os.listdir(TEST_DIR):
  59. _make_testfunc(testfile)
  60. if __name__ == '__main__':
  61. unittest.main()