From 1d8d5a93f7187438587c3a754b53fdf30322cef0 Mon Sep 17 00:00:00 2001 From: dirkf Date: Thu, 13 Jul 2023 20:14:50 +0100 Subject: [test] Fixes for old Pythons --- test/helper.py | 6 ++++++ test/test_http.py | 7 ++++++- test/test_utils.py | 6 +++--- 3 files changed, 15 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/helper.py b/test/helper.py index e3314b03e..aa99001b2 100644 --- a/test/helper.py +++ b/test/helper.py @@ -9,6 +9,7 @@ import re import types import ssl import sys +import unittest import youtube_dl.extractor from youtube_dl import YoutubeDL @@ -17,6 +18,7 @@ from youtube_dl.compat import ( compat_str, ) from youtube_dl.utils import ( + IDENTITY, preferredencoding, write_string, ) @@ -298,3 +300,7 @@ def http_server_port(httpd): else: sock = httpd.socket return sock.getsockname()[1] + + +def expectedFailureIf(cond): + return unittest.expectedFailure if cond else IDENTITY diff --git a/test/test_http.py b/test/test_http.py index cd180b51f..1a6b2e878 100644 --- a/test/test_http.py +++ b/test/test_http.py @@ -8,6 +8,7 @@ import sys import unittest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +import contextlib import gzip import io import ssl @@ -154,7 +155,7 @@ class HTTPTestRequestHandler(compat_http_server.BaseHTTPRequestHandler): def gzip_compress(p): buf = io.BytesIO() - with gzip.GzipFile(fileobj=buf, mode='wb') as f: + with contextlib.closing(gzip.GzipFile(fileobj=buf, mode='wb')) as f: f.write(p) return buf.getvalue() @@ -306,6 +307,10 @@ class TestHTTP(unittest.TestCase): else self.https_port if scheme == 'https' else self.http_port, path) + @unittest.skipUnless( + sys.version_info >= (3, 2) + or (sys.version_info[0] == 2 and sys.version_info[1:] >= (7, 9)), + 'No support for certificate check in SSL') def test_nocheckcertificate(self): with FakeYDL({'logger': FakeLogger()}) as ydl: with self.assertRaises(compat_urllib_error.URLError): diff --git a/test/test_utils.py b/test/test_utils.py index 1fc16ed05..2ee727caf 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1617,7 +1617,7 @@ Line 1 self.assertEqual(traverse_obj(_TEST_DATA, lambda x, y: x == 'urls' and isinstance(y, list)), [_TEST_DATA['urls']], msg='function as query key should perform a filter based on (key, value)') - self.assertCountEqual(traverse_obj(_TEST_DATA, lambda _, x: isinstance(x[0], str)), {'str'}, + self.assertCountEqual(traverse_obj(_TEST_DATA, lambda _, x: isinstance(x[0], str)), set(('str',)), msg='exceptions in the query function should be catched') self.assertEqual(traverse_obj(iter(range(4)), lambda _, x: x % 2 == 0), [0, 2], msg='function key should accept iterables') @@ -1643,7 +1643,7 @@ Line 1 with self.assertRaises(Exception, msg='Sets with length != 1 should raise in debug'): traverse_obj(_TEST_DATA, set()) with self.assertRaises(Exception, msg='Sets with length != 1 should raise in debug'): - traverse_obj(_TEST_DATA, {str.upper, str}) + traverse_obj(_TEST_DATA, set((str.upper, str))) # Test `slice` as a key _SLICE_DATA = [0, 1, 2, 3, 4] @@ -1779,7 +1779,7 @@ Line 1 {0: 100}, msg='type as expected_type should filter dict values') self.assertEqual(traverse_obj(_TEST_DATA, {0: 100, 1: 1.2, 2: 'None'}, expected_type=str_or_none), {0: '100', 1: '1.2'}, msg='function as expected_type should transform dict values') - self.assertEqual(traverse_obj(_TEST_DATA, ({0: 1.2}, 0, {int_or_none}), expected_type=int), + self.assertEqual(traverse_obj(_TEST_DATA, ({0: 1.2}, 0, set((int_or_none,))), expected_type=int), 1, msg='expected_type should not filter non final dict values') self.assertEqual(traverse_obj(_TEST_DATA, {0: {0: 100, 1: 'str'}}, expected_type=int), {0: {0: 100}}, msg='expected_type should transform deep dict values') -- cgit v1.2.3