2021-06-03 11:43:42 +02:00
|
|
|
#!/usr/bin/env python3
|
2022-06-24 13:06:16 +02:00
|
|
|
|
2014-11-02 11:23:40 +01:00
|
|
|
# Allow direct execution
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
2022-04-12 00:32:57 +02:00
|
|
|
|
2014-11-02 11:23:40 +01:00
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
|
|
|
|
2022-06-24 10:10:17 +02:00
|
|
|
import struct
|
|
|
|
|
2022-04-24 18:28:18 +02:00
|
|
|
from yt_dlp import compat
|
2023-07-22 14:26:53 +02:00
|
|
|
from yt_dlp.compat import urllib # isort: split
|
2024-11-17 00:24:11 +01:00
|
|
|
from yt_dlp.compat import compat_etree_fromstring, compat_expanduser
|
2023-07-22 14:26:53 +02:00
|
|
|
from yt_dlp.compat.urllib.request import getproxies
|
2014-11-02 11:23:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestCompat(unittest.TestCase):
|
2022-04-24 18:28:18 +02:00
|
|
|
def test_compat_passthrough(self):
|
|
|
|
with self.assertWarns(DeprecationWarning):
|
2024-06-12 01:09:58 +02:00
|
|
|
_ = compat.compat_basestring
|
2022-04-24 18:28:18 +02:00
|
|
|
|
2022-06-24 12:10:13 +02:00
|
|
|
with self.assertWarns(DeprecationWarning):
|
2024-06-12 01:09:58 +02:00
|
|
|
_ = compat.WINDOWS_VT_MODE
|
2022-06-24 12:10:13 +02:00
|
|
|
|
2023-07-22 14:26:53 +02:00
|
|
|
self.assertEqual(urllib.request.getproxies, getproxies)
|
2022-04-24 18:28:18 +02:00
|
|
|
|
2023-02-06 22:52:29 +01:00
|
|
|
with self.assertWarns(DeprecationWarning):
|
2024-06-12 01:09:58 +02:00
|
|
|
_ = compat.compat_pycrypto_AES # Must not raise error
|
2023-02-06 22:52:29 +01:00
|
|
|
|
2014-11-02 11:23:40 +01:00
|
|
|
def test_compat_expanduser(self):
|
2014-11-18 23:34:46 +01:00
|
|
|
old_home = os.environ.get('HOME')
|
2022-04-17 22:58:28 +02:00
|
|
|
test_str = R'C:\Documents and Settings\тест\Application Data'
|
|
|
|
try:
|
2022-06-24 10:10:17 +02:00
|
|
|
os.environ['HOME'] = test_str
|
2022-04-17 22:58:28 +02:00
|
|
|
self.assertEqual(compat_expanduser('~'), test_str)
|
|
|
|
finally:
|
2022-06-24 10:10:17 +02:00
|
|
|
os.environ['HOME'] = old_home or ''
|
2014-11-02 11:23:40 +01:00
|
|
|
|
2015-10-25 20:04:55 +01:00
|
|
|
def test_compat_etree_fromstring(self):
|
2015-10-26 16:41:24 +01:00
|
|
|
xml = '''
|
|
|
|
<root foo="bar" spam="中文">
|
|
|
|
<normal>foo</normal>
|
|
|
|
<chinese>中文</chinese>
|
|
|
|
<foo><bar>spam</bar></foo>
|
|
|
|
</root>
|
|
|
|
'''
|
2022-05-09 13:54:28 +02:00
|
|
|
doc = compat_etree_fromstring(xml.encode())
|
2022-06-24 12:54:43 +02:00
|
|
|
self.assertTrue(isinstance(doc.attrib['foo'], str))
|
|
|
|
self.assertTrue(isinstance(doc.attrib['spam'], str))
|
|
|
|
self.assertTrue(isinstance(doc.find('normal').text, str))
|
|
|
|
self.assertTrue(isinstance(doc.find('chinese').text, str))
|
|
|
|
self.assertTrue(isinstance(doc.find('foo/bar').text, str))
|
2015-10-25 20:04:55 +01:00
|
|
|
|
2016-05-22 19:34:08 +02:00
|
|
|
def test_compat_etree_fromstring_doctype(self):
|
|
|
|
xml = '''<?xml version="1.0"?>
|
|
|
|
<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">
|
|
|
|
<smil xmlns="http://www.w3.org/2001/SMIL20/Language"></smil>'''
|
|
|
|
compat_etree_fromstring(xml)
|
|
|
|
|
2016-04-23 12:28:49 +02:00
|
|
|
def test_struct_unpack(self):
|
2022-06-24 10:10:17 +02:00
|
|
|
self.assertEqual(struct.unpack('!B', b'\x00'), (0,))
|
2016-04-23 12:28:49 +02:00
|
|
|
|
|
|
|
|
2014-11-02 11:23:40 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|