helper.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. from __future__ import unicode_literals
  2. import errno
  3. import hashlib
  4. import json
  5. import os.path
  6. import re
  7. import ssl
  8. import sys
  9. import types
  10. import unittest
  11. import youtube_dl.extractor
  12. from youtube_dl import YoutubeDL
  13. from youtube_dl.compat import (
  14. compat_open as open,
  15. compat_os_name,
  16. compat_str,
  17. )
  18. from youtube_dl.utils import (
  19. IDENTITY,
  20. preferredencoding,
  21. write_string,
  22. )
  23. def get_params(override=None):
  24. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
  25. "parameters.json")
  26. LOCAL_PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
  27. "local_parameters.json")
  28. with open(PARAMETERS_FILE, encoding='utf-8') as pf:
  29. parameters = json.load(pf)
  30. if os.path.exists(LOCAL_PARAMETERS_FILE):
  31. with open(LOCAL_PARAMETERS_FILE, encoding='utf-8') as pf:
  32. parameters.update(json.load(pf))
  33. if override:
  34. parameters.update(override)
  35. return parameters
  36. def try_rm(filename):
  37. """ Remove a file if it exists """
  38. try:
  39. os.remove(filename)
  40. except OSError as ose:
  41. if ose.errno != errno.ENOENT:
  42. raise
  43. def report_warning(message):
  44. '''
  45. Print the message to stderr, it will be prefixed with 'WARNING:'
  46. If stderr is a tty file the 'WARNING:' will be colored
  47. '''
  48. if sys.stderr.isatty() and compat_os_name != 'nt':
  49. _msg_header = '\033[0;33mWARNING:\033[0m'
  50. else:
  51. _msg_header = 'WARNING:'
  52. output = '%s %s\n' % (_msg_header, message)
  53. if 'b' in getattr(sys.stderr, 'mode', '') or sys.version_info[0] < 3:
  54. output = output.encode(preferredencoding())
  55. sys.stderr.write(output)
  56. class FakeYDL(YoutubeDL):
  57. def __init__(self, override=None):
  58. # Different instances of the downloader can't share the same dictionary
  59. # some test set the "sublang" parameter, which would break the md5 checks.
  60. params = get_params(override=override)
  61. super(FakeYDL, self).__init__(params, auto_init=False)
  62. self.result = []
  63. def to_screen(self, s, skip_eol=None):
  64. print(s)
  65. def trouble(self, *args, **kwargs):
  66. s = args[0] if len(args) > 0 else kwargs.get('message', 'Missing message')
  67. raise Exception(s)
  68. def download(self, x):
  69. self.result.append(x)
  70. def expect_warning(self, regex):
  71. # Silence an expected warning matching a regex
  72. old_report_warning = self.report_warning
  73. def report_warning(self, message):
  74. if re.match(regex, message):
  75. return
  76. old_report_warning(message)
  77. self.report_warning = types.MethodType(report_warning, self)
  78. class FakeLogger(object):
  79. def debug(self, msg):
  80. pass
  81. def warning(self, msg):
  82. pass
  83. def error(self, msg):
  84. pass
  85. def gettestcases(include_onlymatching=False):
  86. for ie in youtube_dl.extractor.gen_extractors():
  87. for tc in ie.get_testcases(include_onlymatching):
  88. yield tc
  89. md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
  90. def expect_value(self, got, expected, field):
  91. if isinstance(expected, compat_str) and expected.startswith('re:'):
  92. match_str = expected[len('re:'):]
  93. match_rex = re.compile(match_str)
  94. self.assertTrue(
  95. isinstance(got, compat_str),
  96. 'Expected a %s object, but got %s for field %s' % (
  97. compat_str.__name__, type(got).__name__, field))
  98. self.assertTrue(
  99. match_rex.match(got),
  100. 'field %s (value: %r) should match %r' % (field, got, match_str))
  101. elif isinstance(expected, compat_str) and expected.startswith('startswith:'):
  102. start_str = expected[len('startswith:'):]
  103. self.assertTrue(
  104. isinstance(got, compat_str),
  105. 'Expected a %s object, but got %s for field %s' % (
  106. compat_str.__name__, type(got).__name__, field))
  107. self.assertTrue(
  108. got.startswith(start_str),
  109. 'field %s (value: %r) should start with %r' % (field, got, start_str))
  110. elif isinstance(expected, compat_str) and expected.startswith('contains:'):
  111. contains_str = expected[len('contains:'):]
  112. self.assertTrue(
  113. isinstance(got, compat_str),
  114. 'Expected a %s object, but got %s for field %s' % (
  115. compat_str.__name__, type(got).__name__, field))
  116. self.assertTrue(
  117. contains_str in got,
  118. 'field %s (value: %r) should contain %r' % (field, got, contains_str))
  119. elif isinstance(expected, compat_str) and re.match(r'lambda \w+:', expected):
  120. fn = eval(expected)
  121. suite = expected.split(':', 1)[1].strip()
  122. self.assertTrue(
  123. fn(got),
  124. 'Expected field %s to meet condition %s, but value %r failed ' % (field, suite, got))
  125. elif isinstance(expected, type):
  126. self.assertTrue(
  127. isinstance(got, expected),
  128. 'Expected type %r for field %s, but got value %r of type %r' % (expected, field, got, type(got)))
  129. elif isinstance(expected, dict) and isinstance(got, dict):
  130. expect_dict(self, got, expected)
  131. elif isinstance(expected, list) and isinstance(got, list):
  132. self.assertEqual(
  133. len(expected), len(got),
  134. 'Expected a list of length %d, but got a list of length %d for field %s' % (
  135. len(expected), len(got), field))
  136. for index, (item_got, item_expected) in enumerate(zip(got, expected)):
  137. type_got = type(item_got)
  138. type_expected = type(item_expected)
  139. self.assertEqual(
  140. type_expected, type_got,
  141. 'Type mismatch for list item at index %d for field %s, expected %r, got %r' % (
  142. index, field, type_expected, type_got))
  143. expect_value(self, item_got, item_expected, field)
  144. else:
  145. if isinstance(expected, compat_str) and expected.startswith('md5:'):
  146. self.assertTrue(
  147. isinstance(got, compat_str),
  148. 'Expected field %s to be a unicode object, but got value %r of type %r' % (field, got, type(got)))
  149. got = 'md5:' + md5(got)
  150. elif isinstance(expected, compat_str) and re.match(r'^(?:min|max)?count:\d+', expected):
  151. self.assertTrue(
  152. isinstance(got, (list, dict)),
  153. 'Expected field %s to be a list or a dict, but it is of type %s' % (
  154. field, type(got).__name__))
  155. op, _, expected_num = expected.partition(':')
  156. expected_num = int(expected_num)
  157. if op == 'mincount':
  158. assert_func = self.assertGreaterEqual
  159. msg_tmpl = 'Expected %d items in field %s, but only got %d'
  160. elif op == 'maxcount':
  161. assert_func = self.assertLessEqual
  162. msg_tmpl = 'Expected maximum %d items in field %s, but got %d'
  163. elif op == 'count':
  164. assert_func = self.assertEqual
  165. msg_tmpl = 'Expected exactly %d items in field %s, but got %d'
  166. else:
  167. assert False
  168. assert_func(
  169. len(got), expected_num,
  170. msg_tmpl % (expected_num, field, len(got)))
  171. return
  172. self.assertEqual(
  173. expected, got,
  174. 'Invalid value for field %s, expected %r, got %r' % (field, expected, got))
  175. def expect_dict(self, got_dict, expected_dict):
  176. for info_field, expected in expected_dict.items():
  177. got = got_dict.get(info_field)
  178. expect_value(self, got, expected, info_field)
  179. def expect_info_dict(self, got_dict, expected_dict):
  180. expect_dict(self, got_dict, expected_dict)
  181. # Check for the presence of mandatory fields
  182. if got_dict.get('_type') not in ('playlist', 'multi_video'):
  183. for key in ('id', 'url', 'title', 'ext'):
  184. self.assertTrue(got_dict.get(key), 'Missing mandatory field %s' % key)
  185. # Check for mandatory fields that are automatically set by YoutubeDL
  186. for key in ['webpage_url', 'extractor', 'extractor_key']:
  187. self.assertTrue(got_dict.get(key), 'Missing field: %s' % key)
  188. # Are checkable fields missing from the test case definition?
  189. test_info_dict = dict((key, value if not isinstance(value, compat_str) or len(value) < 250 else 'md5:' + md5(value))
  190. for key, value in got_dict.items()
  191. if value and key in ('id', 'title', 'description', 'uploader', 'upload_date', 'timestamp', 'uploader_id', 'location', 'age_limit'))
  192. missing_keys = set(test_info_dict.keys()) - set(expected_dict.keys())
  193. if missing_keys:
  194. def _repr(v):
  195. if isinstance(v, compat_str):
  196. return "'%s'" % v.replace('\\', '\\\\').replace("'", "\\'").replace('\n', '\\n')
  197. else:
  198. return repr(v)
  199. info_dict_str = ''
  200. if len(missing_keys) != len(expected_dict):
  201. info_dict_str += ''.join(
  202. ' %s: %s,\n' % (_repr(k), _repr(v))
  203. for k, v in test_info_dict.items() if k not in missing_keys)
  204. if info_dict_str:
  205. info_dict_str += '\n'
  206. info_dict_str += ''.join(
  207. ' %s: %s,\n' % (_repr(k), _repr(test_info_dict[k]))
  208. for k in missing_keys)
  209. write_string(
  210. '\n\'info_dict\': {\n' + info_dict_str + '},\n', out=sys.stderr)
  211. self.assertFalse(
  212. missing_keys,
  213. 'Missing keys in test definition: %s' % (
  214. ', '.join(sorted(missing_keys))))
  215. def assertRegexpMatches(self, text, regexp, msg=None):
  216. if hasattr(self, 'assertRegexp'):
  217. return self.assertRegexp(text, regexp, msg)
  218. else:
  219. m = re.match(regexp, text)
  220. if not m:
  221. note = 'Regexp didn\'t match: %r not found' % (regexp)
  222. if len(text) < 1000:
  223. note += ' in %r' % text
  224. if msg is None:
  225. msg = note
  226. else:
  227. msg = note + ', ' + msg
  228. self.assertTrue(m, msg)
  229. def expect_warnings(ydl, warnings_re):
  230. real_warning = ydl.report_warning
  231. def _report_warning(w):
  232. if not any(re.search(w_re, w) for w_re in warnings_re):
  233. real_warning(w)
  234. ydl.report_warning = _report_warning
  235. def http_server_port(httpd):
  236. if os.name == 'java' and isinstance(httpd.socket, ssl.SSLSocket):
  237. # In Jython SSLSocket is not a subclass of socket.socket
  238. sock = httpd.socket.sock
  239. else:
  240. sock = httpd.socket
  241. return sock.getsockname()[1]
  242. def expectedFailureIf(cond):
  243. return unittest.expectedFailure if cond else IDENTITY