]> jfr.im git - yt-dlp.git/blame - test/helper.py
Fix test
[yt-dlp.git] / test / helper.py
CommitLineData
f4aac741 1import errno
112da0a0 2import io
44a5f171 3import hashlib
112da0a0
PH
4import json
5import os.path
00fcc17a
FV
6import re
7import types
112da0a0 8
fc2c063e 9import youtube_dl.extractor
44a5f171 10from youtube_dl import YoutubeDL
112da0a0 11
112da0a0 12
44a5f171
PH
13def global_setup():
14 youtube_dl._setup_opener(timeout=10)
15
16
17def get_params(override=None):
18 PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
19 "parameters.json")
20 with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
21 parameters = json.load(pf)
22 if override:
23 parameters.update(override)
24 return parameters
112da0a0 25
f4aac741
PH
26
27def try_rm(filename):
28 """ Remove a file if it exists """
29 try:
30 os.remove(filename)
31 except OSError as ose:
32 if ose.errno != errno.ENOENT:
33 raise
34
35
112da0a0
PH
36class FakeYDL(YoutubeDL):
37 def __init__(self):
112da0a0
PH
38 # Different instances of the downloader can't share the same dictionary
39 # some test set the "sublang" parameter, which would break the md5 checks.
44a5f171 40 params = get_params()
f4aac741
PH
41 super(FakeYDL, self).__init__(params)
42 self.result = []
43
44 def to_screen(self, s, skip_eol=None):
112da0a0 45 print(s)
f4aac741 46
112da0a0
PH
47 def trouble(self, s, tb=None):
48 raise Exception(s)
f4aac741 49
112da0a0 50 def download(self, x):
fc2c063e 51 self.result.append(x)
f4aac741 52
00fcc17a
FV
53 def expect_warning(self, regex):
54 # Silence an expected warning matching a regex
55 old_report_warning = self.report_warning
56 def report_warning(self, message):
57 if re.match(regex, message): return
58 old_report_warning(message)
59 self.report_warning = types.MethodType(report_warning, self)
fc2c063e
PH
60
61def get_testcases():
62 for ie in youtube_dl.extractor.gen_extractors():
63 t = getattr(ie, '_TEST', None)
64 if t:
65 t['name'] = type(ie).__name__[:-len('IE')]
66 yield t
67 for t in getattr(ie, '_TESTS', []):
68 t['name'] = type(ie).__name__[:-len('IE')]
69 yield t
44a5f171
PH
70
71
72md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()