Skip to content

Commit

Permalink
Handle expected_warnings better
Browse files Browse the repository at this point in the history
* make fake `report_warning()` method signatures correct (per 640d39f)
* support single warning to expect as well as sequence
* don't colour text to be matched
* use `expected_warnings()` function throughout
  • Loading branch information
dirkf authored Feb 21, 2024
1 parent 15b0616 commit 2239666
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from youtube_dl.utils import (
IDENTITY,
preferredencoding,
variadic,
write_string,
)

Expand Down Expand Up @@ -66,7 +67,7 @@ def report_warning(message):
class FakeYDL(YoutubeDL):
def __init__(self, override=None):
# Different instances of the downloader can't share the same dictionary
# some test set the "sublang" parameter, which would break the md5 checks.
# some tests set the "sublang" parameter, which would break the md5 checks.
params = get_params(override=override)
super(FakeYDL, self).__init__(params, auto_init=False)
self.result = []
Expand All @@ -83,14 +84,7 @@ def download(self, x):

def expect_warning(self, regex):
# Silence an expected warning matching a regex
old_report_warning = self.report_warning

def report_warning(self, message):
if re.match(regex, message):
return
old_report_warning(message)
self.report_warning = types.MethodType(report_warning, self)

expect_warnings(self, regex)

class FakeLogger(object):
def debug(self, msg):
Expand Down Expand Up @@ -285,12 +279,14 @@ def assertEqual(self, got, expected, msg=None):

def expect_warnings(ydl, warnings_re):
real_warning = ydl.report_warning
# to facilitate matching, don't prettify messages
ydl.params['no_color'] = True

def _report_warning(w):
if not any(re.search(w_re, w) for w_re in warnings_re):
real_warning(w)
def _report_warning(self, w, *args, **kwargs):
if not any(re.search(w_re, w) for w_re in variadic(warnings_re)):
real_warning(w, *args, **kwargs)

ydl.report_warning = _report_warning
ydl.report_warning = types.MethodType(_report_warning, ydl)


def http_server_port(httpd):
Expand Down

0 comments on commit 2239666

Please sign in to comment.