]> jfr.im git - yt-dlp.git/blame - test/helper.py
Merge remote-tracking branch 'origin/tox'
[yt-dlp.git] / test / helper.py
CommitLineData
112da0a0
PH
1import io
2import json
3import os.path
00fcc17a
FV
4import re
5import types
112da0a0 6
fc2c063e 7import youtube_dl.extractor
112da0a0
PH
8from youtube_dl import YoutubeDL, YoutubeDLHandler
9from youtube_dl.utils import (
10 compat_cookiejar,
11 compat_urllib_request,
12)
13
14# General configuration (from __init__, not very elegant...)
15jar = compat_cookiejar.CookieJar()
16cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
17proxy_handler = compat_urllib_request.ProxyHandler()
18opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
19compat_urllib_request.install_opener(opener)
20
21PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
22with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
23 parameters = json.load(pf)
24
25class FakeYDL(YoutubeDL):
26 def __init__(self):
27 self.result = []
28 # Different instances of the downloader can't share the same dictionary
29 # some test set the "sublang" parameter, which would break the md5 checks.
30 self.params = dict(parameters)
31 def to_screen(self, s):
32 print(s)
33 def trouble(self, s, tb=None):
34 raise Exception(s)
35 def download(self, x):
fc2c063e 36 self.result.append(x)
00fcc17a
FV
37 def expect_warning(self, regex):
38 # Silence an expected warning matching a regex
39 old_report_warning = self.report_warning
40 def report_warning(self, message):
41 if re.match(regex, message): return
42 old_report_warning(message)
43 self.report_warning = types.MethodType(report_warning, self)
fc2c063e
PH
44
45def get_testcases():
46 for ie in youtube_dl.extractor.gen_extractors():
47 t = getattr(ie, '_TEST', None)
48 if t:
49 t['name'] = type(ie).__name__[:-len('IE')]
50 yield t
51 for t in getattr(ie, '_TESTS', []):
52 t['name'] = type(ie).__name__[:-len('IE')]
53 yield t