helper.py 11 KB

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